Skip to content

Commit

Permalink
update UI
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfeo committed Nov 8, 2023
1 parent 3c32bd4 commit 4521065
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 157 deletions.
10 changes: 5 additions & 5 deletions TheMiddleman/BusinessLogic/MarketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public MarketService()
{
_productService = new ProductService();
_middlemanService = new MiddlemanService();
_middlemen = _middlemanService.GetAllMiddlemen();
_middlemen = _middlemanService.RetrieveAllMiddlemen();
}

public MiddlemanService getMiddlemanService()
public MiddlemanService MiddlemanService()
{
return _middlemanService;
}

public ProductService getProductService()
public ProductService ProductService()
{
return _productService;
}
Expand All @@ -36,11 +36,11 @@ public void SimulateDay()
{
OnDayStart.Invoke(middleman, currentDay);
}
RotateMiddlemen();
ChangeMiddlemanOrder();
currentDay++;
}

private void RotateMiddlemen()
private void ChangeMiddlemanOrder()
{
if (_middlemen.Count > 1)
{
Expand Down
44 changes: 22 additions & 22 deletions TheMiddleman/BusinessLogic/MiddlemanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@ public Middleman CreateAndStoreMiddleman(string name, string company, int initia
return middleman;
}

public void ProcessPurchase(Middleman middleman, Product selectedProduct, int quantity)
public void Purchase(Middleman middleman, Product selectedProduct, int quantity, out string errorLog)
{
errorLog = "";
int totalCost = quantity * selectedProduct.BasePrice;
int totalQuantityAfterPurchase = middleman.Warehouse.Values.Sum() + quantity;
if (totalQuantityAfterPurchase > middleman.MaxStorageCapacity)
{
Console.WriteLine("Kein Platz mehr im Lager.");
errorLog = "Kein Platz mehr im Lager. Verfügbarer Platz: " + (middleman.MaxStorageCapacity - middleman.Warehouse.Values.Sum()) + " Einheiten.";
return;
}
if (middleman.AccountBalance < totalCost)
{
Console.WriteLine("Nicht genügend Geld vorhanden.");
return;
}
if (selectedProduct.AvailableQuantity < quantity)
{
Console.WriteLine("Nicht genügend Ware vorhanden.");
errorLog = "Nicht genügend Geld vorhanden. Verfügbares Guthaben: $" + middleman.AccountBalance;
return;
}
selectedProduct.AvailableQuantity -= quantity;
Expand All @@ -46,11 +42,21 @@ public void ProcessPurchase(Middleman middleman, Product selectedProduct, int qu
{
middleman.Warehouse.Add(selectedProduct, quantity);
}
Console.WriteLine($"Kauf erfolgreich. Neuer Kontostand: ${middleman.AccountBalance}");
}

public void ProcessSale(Middleman middleman, Product product, int quantity)
public void Sale(Middleman middleman, Product product, int quantity, out string errorLog)
{
errorLog = "";
if (!middleman.Warehouse.ContainsKey(product))
{
errorLog = "Produkt ist nicht im Lager vorhanden.";
return;
}
if (middleman.Warehouse[product] < quantity)
{
errorLog = $"Nicht genug {product.Name} im Lager. Verfügbar: {middleman.Warehouse[product]} Einheiten.";
return;
}
middleman.Warehouse[product] -= quantity;
if (middleman.Warehouse[product] == 0)
{
Expand All @@ -59,24 +65,18 @@ public void ProcessSale(Middleman middleman, Product product, int quantity)
middleman.AccountBalance += quantity * product.SellingPrice;
}

private bool CanPurchase(Middleman middleman, Product product, int quantity, int totalCost)
{
return middleman.AccountBalance >= totalCost && product.AvailableQuantity >= quantity && CanStore(middleman, quantity);
}

private bool CanStore(Middleman middleman, int quantity)
public void IncreaseWarehouseCapacity(Middleman middleman, int amount)
{
int currentStorage = middleman.Warehouse?.Sum(entry => entry.Value) ?? 0;
return currentStorage + quantity <= middleman.MaxStorageCapacity;
middleman.MaxStorageCapacity += amount;
}

public void IncreaseWarehouseCapacity(Middleman middleman, int amount)
public List<Middleman> RetrieveAllMiddlemen()
{
middleman.MaxStorageCapacity += amount;
return _middlemanRepository.RetrieveAllMiddlemen();
}

public List<Middleman> GetAllMiddlemen()
internal Middleman RetrieveMiddlemanById(int v)
{
return _middlemanRepository.GetAllMiddlemen();
return _middlemanRepository.RetrieveMiddlemanById(v);
}
}
47 changes: 26 additions & 21 deletions TheMiddleman/BusinessLogic/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,38 @@ private void UpdateProductPrices()
var products = productRepository.GetAllProducts();
foreach (var product in products)
{
double priceChangePercentage;
double newPurchasePrice;
double maxAvailability = product.MaxProductionRate * product.Durability;
double availabilityPercentage = product.AvailableQuantity / maxAvailability;
product.PurchasePrice = (int)CalculateNewProductPrice(product, availabilityPercentage);
//Console.WriteLine($"Produkt: {product.Name} | Verfügbarkeit: {Math.Round(availabilityPercentage * 100, 2)}% ({product.AvailableQuantity} / {maxAvailability})");
if (availabilityPercentage < 0.25)
{
priceChangePercentage = RandomValueBetween(-0.10, 0.30);
newPurchasePrice = product.BasePrice * (1 + priceChangePercentage);
}
else if (availabilityPercentage >= 0.25 && availabilityPercentage <= 0.80)
{
priceChangePercentage = RandomValueBetween(-0.05, 0.05);
newPurchasePrice = product.BasePrice * (1 + priceChangePercentage);
}
else
{
priceChangePercentage = RandomValueBetween(-0.10, 0.06);
newPurchasePrice = product.PurchasePrice * (1 + priceChangePercentage);
}
//Console.WriteLine($"Preisänderung in Prozent: {Math.Round(priceChangePercentage * 100, 2)}%");
newPurchasePrice = Math.Max(newPurchasePrice, product.BasePrice * 0.25);
newPurchasePrice = Math.Min(newPurchasePrice, product.BasePrice * 3);
product.PurchasePrice = (int)newPurchasePrice;
}
}

private double CalculateNewProductPrice(Product product, double availabilityPercentage)
{
double priceChangePercentage;
double newPurchasePrice;
if (availabilityPercentage < 0.25)
{
priceChangePercentage = RandomValueBetween(-0.10, 0.30);
newPurchasePrice = product.BasePrice * (1 + priceChangePercentage);
}
else if (availabilityPercentage >= 0.25 && availabilityPercentage <= 0.80)
{
priceChangePercentage = RandomValueBetween(-0.05, 0.05);
newPurchasePrice = product.BasePrice * (1 + priceChangePercentage);
}
else
{
priceChangePercentage = RandomValueBetween(-0.10, 0.06);
newPurchasePrice = product.BasePrice * (1 + priceChangePercentage);
}
//Console.WriteLine($"Preisänderung in Prozent: {Math.Round(priceChangePercentage * 100, 2)}%");
newPurchasePrice = Math.Max(newPurchasePrice, product.BasePrice * 0.25);
newPurchasePrice = Math.Min(newPurchasePrice, product.BasePrice * 3);
return newPurchasePrice;
}

private double RandomValueBetween(double minValue, double maxValue)
{
Random random = new Random();
Expand Down
5 changes: 3 additions & 2 deletions TheMiddleman/DataAccess/IMiddlemanRespository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ namespace TheMiddleman.DataAccess
{
public interface IMiddlemanRespository
{
List<Middleman> GetAllMiddlemen();
int GetAmountOfMiddlemen();
List<Middleman> RetrieveAllMiddlemen();
int NumberOfParticipatingMiddlemen();
void AddMiddleman(Middleman middleman);
Middleman RetrieveMiddlemanById(int v);
}
}
20 changes: 18 additions & 2 deletions TheMiddleman/DataAccess/MiddlemanRespository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class MiddlemanRespository : IMiddlemanRespository
{
private List<Middleman> middlemen = new List<Middleman>();

public int GetAmountOfMiddlemen()
public int NumberOfParticipatingMiddlemen()
{
return middlemen.Count;
}
Expand All @@ -16,9 +16,25 @@ public void AddMiddleman(Middleman middleman)
middlemen.Add(middleman);
}

public List<Middleman> GetAllMiddlemen()
public List<Middleman> RetrieveAllMiddlemen()
{
return middlemen;
}

public Middleman RetrieveMiddlemanById(int id)
{
if (id < 0)
{
throw new ArgumentException("Id darf nicht negativ sein.");
}
else if (id >= middlemen.Count)
{
throw new ArgumentException("Id darf nicht größer als die Anzahl der Middlemen sein.");
}
else
{
return middlemen[id];
}
}
}
}
1 change: 1 addition & 0 deletions TheMiddleman/Entities/Middleman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public Middleman(string name, string company, int accountBalance)
Company = company;
AccountBalance = accountBalance;
}
public int Id { get; set; }
public string? Name { get; }
public string? Company { get; }
public int AccountBalance { get; set; }
Expand Down
Loading

0 comments on commit 4521065

Please sign in to comment.