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 b918486 commit b50111d
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 193 deletions.
88 changes: 48 additions & 40 deletions TheMiddleman/BusinessLogic/MarketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ public class MarketService
private readonly MiddlemanService _middlemanService;
public Action<Middleman, int> _OnDayStart { get; set; } = delegate { };
public Action<int> _OnDayChange { get; set; } = delegate { };
public Action<Middleman, InsufficientFundsException> _OnBankruptcy { get; set; } = delegate { };
public Action<Middleman> _OnBankruptcy { get; set; } = delegate { };
public Action<List<Middleman>> _OnEndOfGame { get; set; } = delegate { };
public Action _OnStartOfGame { get; set; } = delegate { };
public int _currentDay = 1;
private int _simulationDuration;
private List<Middleman> _middlemen;
private List<Middleman> _bankruptMiddlemen;

public MarketService()

Check warning on line 17 in TheMiddleman/BusinessLogic/MarketService.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_bankruptMiddlemen' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in TheMiddleman/BusinessLogic/MarketService.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_bankruptMiddlemen' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
Expand All @@ -33,69 +34,84 @@ public ProductService ProductService()
public void RunSimulation()
{
_middlemen = _middlemanService.RetrieveMiddlemen();
_bankruptMiddlemen = _middlemanService.RetrieveBankruptMiddlemen();
_productService.CreateProducts();
_OnStartOfGame?.Invoke();
for (_currentDay = 1; _currentDay <= _simulationDuration && _middlemen.Any(); _currentDay++)
while (_currentDay <= _simulationDuration && _middlemen.Count > 0)
{
_OnDayChange?.Invoke(_currentDay);
SimulateDay();
if (_middlemen.Count == 0)
{
break;
}
_currentDay++;
}
EndSimulation();
}

public void SimulateDay()
{
/* CheckForEndOfSimulation(); */
if (_currentDay > 1) { _productService.UpdateProducts(); }
List<Middleman> bankruptMiddlemen = ProcessMiddlemenEachDay();
SaveBankruptMiddlemen(bankruptMiddlemen);
ChangeMiddlemanOrder();
CheckForEndOfSimulation();
ProcessMiddlemenEachDay();
}

private List<Middleman> ProcessMiddlemenEachDay()
private void ProcessMiddlemenEachDay()
{
List<Middleman> bankruptMiddlemen = new List<Middleman>();
foreach (var middleman in _middlemen)
foreach (var middleman in _middlemen.ToList())
{
try
if (middleman.AccountBalance <= 0)
{
/* _middlemanService.ResetDailyReport(middleman); */
_middlemanService.DeductStorageCosts(middleman);
_OnDayStart.Invoke(middleman, _currentDay);
_bankruptMiddlemen.Add(middleman);
continue;
}
catch (InsufficientFundsException ex)
else
{
_OnBankruptcy.Invoke(middleman, ex);
bankruptMiddlemen.Add(middleman);
try
{
_middlemanService.DeductStorageCosts(middleman);
if (middleman.AccountBalance <= 0)
{
_OnBankruptcy.Invoke(middleman);
_bankruptMiddlemen.Add(middleman);
continue;
}
_OnDayStart.Invoke(middleman, _currentDay);
}
catch (InsufficientFundsException)
{
_bankruptMiddlemen.Add(middleman);
}
foreach (var bankruptMiddleman in _bankruptMiddlemen)
{
_middlemen.Remove(bankruptMiddleman);
}
}
}
return bankruptMiddlemen;
SaveBankruptMiddlemen(_bankruptMiddlemen);
ChangeMiddlemanOrder();
CheckForEndOfSimulation();
}

private void SaveBankruptMiddlemen(List<Middleman> bankruptMiddlemen)
{
foreach (var bankruptMiddleman in bankruptMiddlemen)
{
_middlemanService.AddBankruptMiddleman(bankruptMiddleman);
if (!bankruptMiddleman.BankruptcyNotified)
{
bankruptMiddleman.BankruptcyNotified = true;
}
_middlemen.Remove(bankruptMiddleman);
}
}

private void CheckForEndOfSimulation()
{
if (_currentDay > _simulationDuration || _middlemen.Count == 0) { EndSimulation(); }
if (_currentDay >= _simulationDuration || !_middlemen.Any(m => m.AccountBalance >= 0))
{
EndSimulation();
}
}

private void EndSimulation()
{
if (_middlemen.Count > 0)
{
_middlemen.Sort((x, y) => y.AccountBalance.CompareTo(x.AccountBalance));
}
_middlemen.Sort((x, y) => y.AccountBalance.CompareTo(x.AccountBalance));
_OnEndOfGame.Invoke(_middlemen);
}

Expand All @@ -109,21 +125,13 @@ private void ChangeMiddlemanOrder()
}
}

public bool CheckIfMiddlemanIsLastBankroped(Middleman middleman)
public void SetSimulationDuration(int duration)
{
if (_currentDay > _simulationDuration || _middlemen.Count == 0)
{
EndSimulation();
return true;
}
else
{
return false;
}
_simulationDuration = duration;
}

public void SetSimulationDuration(int duration)
public bool CheckIfMiddlemanIsLastBankrupted(Middleman middleman)
{
_simulationDuration = duration;
return _middlemanService.CheckIfMiddlemanIsLastBankrupted(middleman);
}
}
33 changes: 27 additions & 6 deletions TheMiddleman/BusinessLogic/MiddlemanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public MiddlemanService()
_middlemanRepository = new MiddlemanRepository();
}

public Middleman CreateAndStoreMiddleman(string name, string company, int initialBalance)
public Middleman CreateAndStoreMiddleman(string name, string company, double initialBalance)
{
Middleman middleman = new Middleman(name, company, initialBalance);
_middlemanRepository.AddMiddleman(middleman);
Expand All @@ -19,15 +19,17 @@ public Middleman CreateAndStoreMiddleman(string name, string company, int initia

public void PurchaseProduct(Middleman middleman, Product selectedProduct, int quantity)
{
var totalCost = quantity * selectedProduct.BasePrice;
double discount = CalculateDiscount(selectedProduct, middleman);
double discountedPrice = selectedProduct.PurchasePrice * (1 - discount);
var totalCost = quantity * discountedPrice;
var totalQuantityAfterPurchase = middleman.Warehouse.Values.Sum() + quantity;
if (totalQuantityAfterPurchase > middleman.MaxStorageCapacity)
{
throw new WarehouseCapacityExceededException("Kein Platz mehr im Lager. Verfügbarer Platz: " + (middleman.MaxStorageCapacity - middleman.Warehouse.Values.Sum()) + " Einheiten.");
}
if (middleman.AccountBalance < totalCost)
{
throw new InsufficientFundsException("Nicht genügend Geld vorhanden. Verfügbares Guthaben: $" + middleman.AccountBalance);
throw new InsufficientFundsException("Nicht genügend Geld vorhanden. Verfügbares Guthaben: " + CurrencyFormatter.FormatPrice(middleman.AccountBalance));
}
selectedProduct.AvailableQuantity -= quantity;
middleman.AccountBalance -= totalCost;
Expand Down Expand Up @@ -64,22 +66,31 @@ public void SellProduct(Middleman middleman, Product product, int quantity)

public void IncreaseWarehouseCapacity(Middleman middleman, int increaseAmount)
{
int costForIncrease = increaseAmount * 50;
double costForIncrease = increaseAmount * 50;
if (middleman.AccountBalance < costForIncrease)
{
throw new InsufficientFundsException($"Nicht genügend Geld für die Erweiterung des Lagers vorhanden.");
throw new InsufficientFundsException("Nicht genügend Geld für die Erweiterung des Lagers vorhanden.");
}
middleman.AccountBalance -= costForIncrease;
middleman.MaxStorageCapacity += increaseAmount;
}

public int CalculateStorageCosts(Middleman middleman)
public double CalculateStorageCosts(Middleman middleman)
{
var occupiedUnits = middleman.Warehouse.Values.Sum();
var emptyUnits = middleman.MaxStorageCapacity - occupiedUnits;
return occupiedUnits * 5 + emptyUnits * 1;
}

public double CalculateDiscount(Product product, Middleman middleman)
{
int quantity = middleman.Warehouse.ContainsKey(product) ? middleman.Warehouse[product] : 0;
if (quantity >= 75) return 0.10;
if (quantity >= 50) return 0.05;
if (quantity >= 25) return 0.02;
return 0;
}

public void DeductStorageCosts(Middleman middleman)
{
var storageCosts = CalculateStorageCosts(middleman);
Expand Down Expand Up @@ -124,4 +135,14 @@ public void AddBankruptMiddleman(Middleman middleman)
_middlemanRepository.RetrieveMiddlemen().Remove(middleman);
_middlemanRepository.AddBankruptMiddleman(middleman);
}

public bool CheckIfMiddlemanIsLastBankrupted(Middleman middleman)
{
var middlemen = _middlemanRepository.RetrieveMiddlemen();
if (middlemen.Count > 0)
{
return middlemen[middlemen.Count - 1].Equals(middleman);
}
return false;
}
}
4 changes: 2 additions & 2 deletions TheMiddleman/BusinessLogic/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void UpdateProductPrices()
{
double maxAvailability = product.MaxProductionRate * product.Durability;
double availabilityPercentage = product.AvailableQuantity / maxAvailability;
product.PurchasePrice = (int)CalculateNewProductPrice(product, availabilityPercentage);
product.PurchasePrice = CalculateNewProductPrice(product, availabilityPercentage);
}
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public List<Product> GetAllProducts()
public Product? FindProductById(int productId)
{
var _products = _productRepository.GetAllProducts();
if(_products.Any(p => p.Id == productId))
if (_products.Any(p => p.Id == productId))
{
return _products.First(p => p.Id == productId);
}
Expand Down
2 changes: 1 addition & 1 deletion TheMiddleman/DataAccess/ProductRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void ProcessProductBasePrice(string line, Product? product)
{
if (product != null)
{
int basePrice = ReadProductBasePrice(line);
double basePrice = ReadProductBasePrice(line);
product.BasePrice = basePrice;
}
}
Expand Down
13 changes: 7 additions & 6 deletions TheMiddleman/Entities/Middleman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ public class Middleman
public int Id { get; set; }
public string? Name { get; }
public string? Company { get; }
public int AccountBalance { get; set; }
public double AccountBalance { get; set; }
public int MaxStorageCapacity { get; set; } = 100;
public Dictionary<Product, int> Warehouse { get; set; } = new Dictionary<Product, int>();
public int PreviousDayBalance { get; set; }
public int DailyExpenses { get; set; }
public int DailyEarnings { get; set; }
public int DailyStorageCosts { get; set; }
public double PreviousDayBalance { get; set; }
public double DailyExpenses { get; set; }
public double DailyEarnings { get; set; }
public double DailyStorageCosts { get; set; }
public bool BankruptcyNotified { get; set; } = false;

public Middleman(string name, string company, int accountBalance)
public Middleman(string name, string company, double accountBalance)
{
Name = name;
Company = company;
Expand Down
6 changes: 3 additions & 3 deletions TheMiddleman/Entities/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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 PurchasePrice { get; set; }
public int SellingPrice => (int) Math.Round(BasePrice * 0.8);
public double BasePrice { get; set; }
public double PurchasePrice { get; set; }
public double SellingPrice => (double)Math.Round(BasePrice * 0.8);
public int MinProductionRate { get; set; }
public int MaxProductionRate { get; set; }
public int AvailableQuantity { get; set; } = 0;
Expand Down
Loading

0 comments on commit b50111d

Please sign in to comment.