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 4d7a75d commit 5059e2e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 33 deletions.
172 changes: 144 additions & 28 deletions TheMiddleman/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

class Program
{
static int GetNumMiddlemen()
static int GetAmountOfMiddlemen()
{
Console.WriteLine("Wieviel Zwischenhändler nehmen teil?");
return int.Parse(Console.ReadLine() ?? "0");
}

static string GetName(int index)
static string GetMiddlemanName(int index)
{
Console.WriteLine($"Name von Zwischenhändler {index}:");
return Console.ReadLine() ?? "";
Expand All @@ -24,11 +24,11 @@ static string GetCompany(string name)

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

return new Middleman { Name = name, Company = company, AccountBalance = initialBalance };
return new Middleman(middlemanName, companyName, initialBalance);
}

static int GetInitialBalance()
Expand All @@ -52,20 +52,17 @@ static int GetInitialBalance()
static List<Middleman> InitializeMiddlemen()
{
List<Middleman> middlemen = new List<Middleman>();
int numMiddlemen = GetNumMiddlemen();

for (int i = 1; i <= numMiddlemen; i++)
int amountOfMiddlemen = GetAmountOfMiddlemen();
for (int i = 1; i <= amountOfMiddlemen; i++)
{
middlemen.Add(CreateMiddleman(i));
}

return middlemen;
}

static void DisplayMiddlemanInfo(Middleman middleman, int currentDay)
{
Console.WriteLine($"{middleman.Name} von {middleman.Company} | ${middleman.AccountBalance} | Tag {currentDay}");
Console.WriteLine("b) Runde beenden");
}


Expand All @@ -79,6 +76,11 @@ static int ReadProductDurability(string line)
return int.Parse(line.Substring(14));
}

static int ReadProductBasePrice(string line)
{
return int.Parse(line.Substring(13));
}

static Product CreateProduct(int id, string name, int durability)
{
return new Product { Id = id, Name = name, Durability = durability };
Expand All @@ -88,7 +90,7 @@ static List<Product> ReadProducts()
{
string[] lines = System.IO.File.ReadAllLines("produkte.yml");
List<Product> products = new List<Product>();
Product currentProduct = null;
Product? currentProduct = null;
int idCounter = 1;

foreach (var line in lines)
Expand All @@ -107,35 +109,148 @@ static List<Product> ReadProducts()
products.Add(currentProduct);
}
}
else if (line.StartsWith(" baseprice: "))
{
int basePrice = ReadProductBasePrice(line);
if (currentProduct != null)
{
currentProduct.BasePrice = basePrice;
}
}
}

return products;
}

static void ShowMenuAndTakeAction(Middleman middleman, ref int currentDay)
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() ?? "";

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 ShowShoppingMenu(List<Product> products, Middleman middleman)
{
DisplayMiddlemanInfo(middleman, currentDay);
Console.WriteLine("e) Einkaufen");
Console.WriteLine("b) Runde beenden");
string option = Console.ReadLine() ?? "";
Console.WriteLine("Verfügbare Produkte:");
foreach (Product product in products)
{
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;
}

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;
}

if (option == "b")
int totalCost = quantity * selectedProduct.BasePrice;

if (middleman.AccountBalance < totalCost)
{
//
Console.WriteLine("Nicht genügend Geld vorhanden.");
return;
}
else if (option == "e")
else
{
Console.WriteLine("Verfügbare Produkte:");
foreach (var product in middleman.Products)
middleman.AccountBalance -= totalCost;

if (middleman.OwnedProducts.ContainsKey(selectedProduct))
{
Console.WriteLine($"{product.Id}) {product.Name} ({product.Durability} Tage)");
middleman.OwnedProducts[selectedProduct] += quantity;
}
Console.WriteLine("z) Zurück");
if (Console.ReadLine() == "z")
else
{
// Go back to the main menu
middleman.OwnedProducts.Add(selectedProduct, quantity);
}

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

static void ShowSellingMenu(Middleman middleman)
{
Console.WriteLine("Produkte im Besitz:");
int index = 1;
foreach (var entry in middleman.OwnedProducts)
{
Console.WriteLine($"{index}) {entry.Key.Name} ({entry.Value}) ${entry.Key.SellingPrice}/Stück");
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;
}

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}");
}

static void RotateMiddlemen(List<Middleman> middlemen)
Expand All @@ -148,11 +263,11 @@ static void RotateMiddlemen(List<Middleman> middlemen)
}
}

static void SimulateDay(List<Middleman> middlemen, ref int currentDay)
static void SimulateDay(List<Middleman> middlemen, ref int currentDay, List<Product> products)
{
foreach (var middleman in middlemen)
{
ShowMenuAndTakeAction(middleman, ref currentDay);
ShowMenuAndTakeAction(middleman, ref currentDay, products);
}

RotateMiddlemen(middlemen);
Expand All @@ -163,11 +278,12 @@ static void SimulateDay(List<Middleman> middlemen, ref int currentDay)
static void Main()
{
List<Middleman> middlemen = InitializeMiddlemen();
List<Product> products = ReadProducts();
int currentDay = 1;

while (true)
{
SimulateDay(middlemen, ref currentDay);
SimulateDay(middlemen, ref currentDay, products);
}
}
}
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.
11 changes: 9 additions & 2 deletions TheMiddleman/entity/Middleman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ namespace TheMiddleman.Entity
{
public class Middleman
{
public Middleman(string name, string company, int accountBalance)
{
Name = name;
Company = company;
AccountBalance = accountBalance;
}

public string? Name { get; set; }
public string? Company { get; set; }
public int AccountBalance { get; set; }
public List<Product> Products { get; set; } = new List<Product>();
public Dictionary<Product, int> OwnedProducts { get; set; } = new Dictionary<Product, int>();
}
}
}
6 changes: 4 additions & 2 deletions TheMiddleman/entity/Product.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;

public class Product
{
public int Id { get; set; }

public required string Name { get; set; }

public int Durability { get; set; }

public int BasePrice { get; set; }

public int SellingPrice => (int)Math.Round(BasePrice * 0.8);
}
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.
24 changes: 23 additions & 1 deletion TheMiddleman/produkte.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
- name: Gurke
durability: 21
baseprice: 5
- name: Aprikose
durability: 4
baseprice: 20
- name: Tomate
durability: 10
baseprice: 7
- name: Kartoffel
durability: 30
baseprice: 3
- name: Zwiebel
durability: 40
baseprice: 2
- name: Karotte
durability: 20
baseprice: 4
- name: Apfel
durability: 14
baseprice: 6
- name: Banane
durability: 7
baseprice: 10
- name: Orangensaft
durability: 5
baseprice: 12
- name: Milch
durability: 3
baseprice: 8
- name: Käse
durability: 25
baseprice: 15
- name: Schokolade
durability: 180
baseprice: 20
- name: Brot
durability: 5
baseprice: 5
- name: Wasser
durability: 365
baseprice: 1
- name: Ei
durability: 21
baseprice: 3
- name: Hähnchenbrust
durability: 4
baseprice: 10
- name: Tofu
durability: 10
baseprice: 12
- name: Lachs
durability: 3
baseprice: 25
- name: Erdbeeren
durability: 7
baseprice: 15
- name: Salat
durability: 7
baseprice: 6
- name: Joghurt
durability: 14
baseprice: 8
- name: Olivenöl
durability: 365
durability: 365
baseprice: 30

0 comments on commit 5059e2e

Please sign in to comment.