Skip to content

Commit

Permalink
update iteration 2
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfeo committed Oct 22, 2023
1 parent 5059e2e commit 6939590
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 69 deletions.
112 changes: 46 additions & 66 deletions TheMiddleman/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,10 @@ static string GetCompany(string name)
return Console.ReadLine() ?? "";
}

static Middleman CreateMiddleman(int index)
{
string middlemanName = GetMiddlemanName(index);
string companyName = GetCompany(middlemanName);
int initialBalance = GetInitialBalance();

return new Middleman(middlemanName, companyName, initialBalance);
}

static int GetInitialBalance()
{
Console.WriteLine("Schwierigkeitsgrad auswählen (Einfach, Normal, Schwer):");
string difficulty = Console.ReadLine()?.ToLower() ?? "normal";

switch (difficulty)
{
case "einfach":
Expand All @@ -49,13 +39,21 @@ static int GetInitialBalance()
}
}

static List<Middleman> InitializeMiddlemen()
static Middleman InitializeSingleMiddleman(int index)
{
string middlemanName = GetMiddlemanName(index);
string companyName = GetCompany(middlemanName);
int initialBalance = GetInitialBalance();
return new Middleman(middlemanName, companyName, initialBalance);
}

static List<Middleman> InitializeAllMiddlemen()
{
List<Middleman> middlemen = new List<Middleman>();
int amountOfMiddlemen = GetAmountOfMiddlemen();
List<Middleman> middlemen = new List<Middleman>();
for (int i = 1; i <= amountOfMiddlemen; i++)
{
middlemen.Add(CreateMiddleman(i));
middlemen.Add(InitializeSingleMiddleman(i));
}
return middlemen;
}
Expand All @@ -65,7 +63,6 @@ static void DisplayMiddlemanInfo(Middleman middleman, int currentDay)
Console.WriteLine($"{middleman.Name} von {middleman.Company} | ${middleman.AccountBalance} | Tag {currentDay}");
}


static string ReadProductName(string line)
{
return line.Substring(8);
Expand All @@ -92,7 +89,6 @@ static List<Product> ReadProducts()
List<Product> products = new List<Product>();
Product? currentProduct = null;
int idCounter = 1;

foreach (var line in lines)
{
if (line.StartsWith("- name: "))
Expand All @@ -118,39 +114,39 @@ static List<Product> ReadProducts()
}
}
}

return products;
}

static void ShowMenuAndTakeAction(Middleman middleman, ref int currentDay, List<Product> products)
{
bool endRound = false;

while (!endRound)
{
DisplayMiddlemanInfo(middleman, currentDay);
Console.WriteLine("e) Einkaufen");
Console.WriteLine("v) Verkaufen");
Console.WriteLine("b) Runde beenden");

string userChoice = Console.ReadLine() ?? "";
HandleUserChoice(userChoice, middleman, products, ref endRound);
}
}

switch (userChoice)
{
case "b":
endRound = true;
break;
case "e":
ShowShoppingMenu(products, middleman);
endRound = false;
break;
case "v":
ShowSellingMenu(middleman);
break;
default:
Console.WriteLine("Ungültige Auswahl. Bitte erneut versuchen.");
break;
}
static void HandleUserChoice(string choice, Middleman middleman, List<Product> products, ref bool endRound)
{
switch (choice)
{
case "b":
endRound = true;
break;
case "e":
ShowShoppingMenu(products, middleman);
break;
case "v":
ShowSellingMenu(middleman);
break;
default:
Console.WriteLine("Ungültige Auswahl. Bitte erneut versuchen.");
break;
}
}

Expand All @@ -162,53 +158,42 @@ static void ShowShoppingMenu(List<Product> products, Middleman middleman)
Console.WriteLine($"{product.Id} {product.Name} ({product.Durability} Tage) ${product.BasePrice}/Stück");
}
Console.WriteLine("z) Zurück");

string? userChoice = Console.ReadLine();

if (userChoice == "z")
{
return;
}
SelectProductAndPurchase(middleman, userChoice, products);
}

static void SelectProductAndPurchase(Middleman middleman ,string? userChoice, List<Product> products)
{
int selectedProductId;

if (!int.TryParse(userChoice, out selectedProductId) || int.Parse(userChoice) <= 0)
{
return;
}

Product selectedProduct = products.Find(p => p.Id == selectedProductId);

Console.WriteLine($"Wieviel von {selectedProduct.Name} kaufen?");
int quantity = int.Parse(Console.ReadLine());

if (quantity <= 0)
{
return;
}
ExecutePurchase(middleman, selectedProduct, quantity);
}

static void ExecutePurchase(Middleman middleman, Product selectedProduct, int quantity)
{
int totalCost = quantity * selectedProduct.BasePrice;

if (middleman.AccountBalance < totalCost)
{
Console.WriteLine("Nicht genügend Geld vorhanden.");
return;
}
else
{
middleman.AccountBalance -= totalCost;

if (middleman.OwnedProducts.ContainsKey(selectedProduct))
{
middleman.OwnedProducts[selectedProduct] += quantity;
}
else
{
middleman.OwnedProducts.Add(selectedProduct, quantity);
}

Console.WriteLine($"Kauf erfolgreich. Neuer Kontostand: ${middleman.AccountBalance}");
}
middleman.AccountBalance -= totalCost;
middleman.OwnedProducts.Add(selectedProduct, quantity);
Console.WriteLine($"Kauf erfolgreich. Neuer Kontostand: ${middleman.AccountBalance}");
}

static void ShowSellingMenu(Middleman middleman)
Expand All @@ -221,35 +206,33 @@ static void ShowSellingMenu(Middleman middleman)
index++;
}
Console.WriteLine("z) Zurück");

string userChoice = Console.ReadLine() ?? "";
if (userChoice == "z")
{
return;
}

int selectedProductIndex = int.Parse(userChoice);
var selectedEntry = middleman.OwnedProducts.ElementAt(selectedProductIndex - 1);
var selectedProduct = selectedEntry.Key;
var availableQuantity = selectedEntry.Value;

Console.WriteLine($"Wieviel von {selectedProduct.Name} verkaufen (max. {availableQuantity})?");
int quantityToSell = int.Parse(Console.ReadLine() ?? "0");

if (quantityToSell <= 0 || quantityToSell > availableQuantity)
{
Console.WriteLine("Ungültige Menge.");
return;
}
ExecuteSale(middleman, selectedProduct, quantityToSell);
}

static void ExecuteSale(Middleman middleman, Product selectedProduct, int quantityToSell)
{
middleman.AccountBalance += quantityToSell * selectedProduct.SellingPrice;
middleman.OwnedProducts[selectedProduct] -= quantityToSell;

if (middleman.OwnedProducts[selectedProduct] == 0)
{
middleman.OwnedProducts.Remove(selectedProduct);
}

Console.WriteLine($"Verkauf erfolgreich. Neuer Kontostand: ${middleman.AccountBalance}");
}

Expand All @@ -269,18 +252,15 @@ static void SimulateDay(List<Middleman> middlemen, ref int currentDay, List<Prod
{
ShowMenuAndTakeAction(middleman, ref currentDay, products);
}

RotateMiddlemen(middlemen);

currentDay++;
}

static void Main()
{
List<Middleman> middlemen = InitializeMiddlemen();
List<Middleman> middlemen = InitializeAllMiddlemen();
List<Product> products = ReadProducts();
int currentDay = 1;

while (true)
{
SimulateDay(middlemen, ref currentDay, products);
Expand Down
Binary file modified TheMiddleman/bin/Debug/net7.0/TheMiddleman.dll
Binary file not shown.
Binary file modified TheMiddleman/bin/Debug/net7.0/TheMiddleman.pdb
Binary file not shown.
6 changes: 3 additions & 3 deletions TheMiddleman/entity/Middleman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public Middleman(string name, string company, int accountBalance)
Company = company;
AccountBalance = accountBalance;
}
public string? Name { get; set; }
public string? Company { get; set; }

public string? Name { get; }
public string? Company { get; }
public int AccountBalance { get; set; }
public Dictionary<Product, int> OwnedProducts { get; set; } = new Dictionary<Product, int>();
}
Expand Down
Binary file modified TheMiddleman/obj/Debug/net7.0/TheMiddleman.dll
Binary file not shown.
Binary file modified TheMiddleman/obj/Debug/net7.0/TheMiddleman.pdb
Binary file not shown.
Binary file modified TheMiddleman/obj/Debug/net7.0/ref/TheMiddleman.dll
Binary file not shown.
Binary file modified TheMiddleman/obj/Debug/net7.0/refint/TheMiddleman.dll
Binary file not shown.

0 comments on commit 6939590

Please sign in to comment.