diff --git a/WHQ7HJ/WHQ7HJ.sln b/WHQ7HJ/WHQ7HJ.sln new file mode 100644 index 0000000..45d05ed --- /dev/null +++ b/WHQ7HJ/WHQ7HJ.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35514.174 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WHQ7HJ", "WHQ7HJ\WHQ7HJ.csproj", "{FA2AA53F-5B1C-4B82-8499-D1F2973F734C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FA2AA53F-5B1C-4B82-8499-D1F2973F734C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FA2AA53F-5B1C-4B82-8499-D1F2973F734C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FA2AA53F-5B1C-4B82-8499-D1F2973F734C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FA2AA53F-5B1C-4B82-8499-D1F2973F734C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/WHQ7HJ/WHQ7HJ/MenuItem.cs b/WHQ7HJ/WHQ7HJ/MenuItem.cs new file mode 100644 index 0000000..045c267 --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/MenuItem.cs @@ -0,0 +1,5 @@ +namespace WHQ7HJ +{ + public record MenuItem(string Name, int Price); + +} diff --git a/WHQ7HJ/WHQ7HJ/MenuService.cs b/WHQ7HJ/WHQ7HJ/MenuService.cs new file mode 100644 index 0000000..ad44009 --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/MenuService.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace WHQ7HJ +{ + internal class MenuService + { + private readonly string _filePath = "..\\..\\..\\menu.json"; + + + public List LoadMenu() + { + try + { + string json = File.ReadAllText(_filePath); + return JsonSerializer.Deserialize>(json) ?? new List(); + } + catch(FileNotFoundException ex) + { + Console.WriteLine(ex.Message); + Environment.Exit(1); + return []; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + Environment.Exit(1); + return []; + } + + + } + + public void DisplayMenu(List menu) + { + Console.WriteLine(""); + Console.WriteLine("Menu"); + Console.WriteLine("-----"); + foreach (var item in menu) + { + Console.WriteLine($"{item.Name} - {item.Price:C}"); + } + } + + + } +} diff --git a/WHQ7HJ/WHQ7HJ/Order.cs b/WHQ7HJ/WHQ7HJ/Order.cs new file mode 100644 index 0000000..456873d --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/Order.cs @@ -0,0 +1,5 @@ +namespace WHQ7HJ +{ + public record Order(string CustomerName, string OrderDate, List MenuItems); + +} diff --git a/WHQ7HJ/WHQ7HJ/OrderService.cs b/WHQ7HJ/WHQ7HJ/OrderService.cs new file mode 100644 index 0000000..d2a5768 --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/OrderService.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace WHQ7HJ +{ + internal class OrderService + { + + private readonly string _filePath = "..\\..\\..\\orders.json"; + + + public List LoadOrders() + { + try + { + string json = File.ReadAllText(_filePath); + return JsonSerializer.Deserialize>(json) ?? new List(); + } + catch (FileNotFoundException ex) + { + Console.WriteLine(ex.Message); + return new List(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return new List(); + } + + } + + public Order CreateOrder(List menu) + { + Console.Write("Enter your name: "); + string customerName = Console.ReadLine(); + if (string.IsNullOrEmpty(customerName)){ customerName = "Anonymous customer"; } + + string orderdate = DateTime.Today.ToString("dd/MM/yyyy"); + + var orderItems = new List(); + + while (true) + { + Console.Clear(); + + Console.WriteLine("Enter the name of the chosen dish (or 'OK' to finish): "); + string input = Console.ReadLine(); + + if (input?.ToLower() == "ok") + { + break; + } + + var menuItem = menu.FirstOrDefault(item => item.Name.Equals(input, StringComparison.OrdinalIgnoreCase)); + + if (menuItem != null) + { + orderItems.Add(menuItem); + Console.WriteLine($"{menuItem.Name} added to the order."); + } + else + { + Console.WriteLine("There is no such dish on the menu!"); + } + + Console.WriteLine("Press any key to continue..."); + Console.ReadKey(); + } + + return new Order(customerName, orderdate, orderItems); + } + + public void SaveOrders(List orders) + { + try + { + string json = JsonSerializer.Serialize(orders, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(_filePath, json); + } + catch(IOException ex) + { + Console.WriteLine(ex.Message); + Environment.Exit(1); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + Environment.Exit(1); + } + } + + public void DisplayOrders(List orders) + { + Console.WriteLine(""); + Console.WriteLine("Orders"); + Console.WriteLine("-----"); + if(orders.Count == 0) + { + Console.WriteLine("No order has been received yet."); + } + else + { + foreach (var order in orders) + { + Console.WriteLine($"Name: {order.CustomerName}, Date: {order.OrderDate}, Order: {string.Join(", ", order.MenuItems.Select(item => item.Name))}"); + } + } + } + + public void DisplayPopularItems(List orders) + { + if(orders.Count == 0) + { + Console.WriteLine("No order has been received yet."); + } + else + { + var popularItems = orders.SelectMany(order => order.MenuItems) + .GroupBy(item => item.Name) + .OrderByDescending(group => group.Count()) + .Select(group => new { Item = group.Key, Count = group.Count() }); + + Console.WriteLine("Popular dishes"); + Console.WriteLine("--------------"); + foreach (var item in popularItems) + { + Console.WriteLine($"{item.Item}: {item.Count} orders"); + } + } + } + + public void CalculateTotal(List orders) + { + Console.WriteLine($"Total revenue: {orders.Sum(order => order.MenuItems.Sum(item => item.Price))} Ft."); + } + + + } +} diff --git a/WHQ7HJ/WHQ7HJ/Program.cs b/WHQ7HJ/WHQ7HJ/Program.cs new file mode 100644 index 0000000..c4251e7 --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/Program.cs @@ -0,0 +1,48 @@ +using WHQ7HJ; + +var menuService = new MenuService(); +var orderService = new OrderService(); + +var menu = menuService.LoadMenu(); +var orders = orderService.LoadOrders(); + +while (true) +{ + Console.Clear(); + Console.WriteLine("1. View menu"); + Console.WriteLine("2. Place an order"); + Console.WriteLine("3. View orders"); + Console.WriteLine("4. Summary of orders"); + Console.WriteLine("5. Exit"); + + Console.Write("Choose an option: "); + string choice = Console.ReadLine(); + + switch (choice) + { + case "1": + menuService.DisplayMenu(menu); + break; + case "2": + Order order = orderService.CreateOrder(menu); + orders.Add(order); + orderService.SaveOrders(orders); + Console.WriteLine("Order recorded!"); + break; + case "3": + orderService.DisplayOrders(orders); + break; + case "4": + orderService.CalculateTotal(orders); + orderService.DisplayPopularItems(orders); + break; + case "5": + return; + default: + Console.WriteLine("Invalid choice!"); + break; + } + + Console.WriteLine("Press any key to continue..."); + Console.ReadKey(); +} \ No newline at end of file diff --git a/WHQ7HJ/WHQ7HJ/WHQ7HJ.csproj b/WHQ7HJ/WHQ7HJ/WHQ7HJ.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/WHQ7HJ.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/WHQ7HJ/WHQ7HJ/menu.json b/WHQ7HJ/WHQ7HJ/menu.json new file mode 100644 index 0000000..1174114 --- /dev/null +++ b/WHQ7HJ/WHQ7HJ/menu.json @@ -0,0 +1,38 @@ +[ + { + "Name": "Fried cheese", + "Price": 1990 + }, + { + "Name": "Forest fruit soup", + "Price": 880 + }, + { + "Name": "Bolognese pork chops", + "Price": 2940 + }, + { + "Name": "Bean goulash soup", + "Price": 1490 + }, + { + "Name": "Beef stew", + "Price": 2350 + }, + { + "Name": "Spaghetti carbonara", + "Price": 1990 + }, + { + "Name": "Fried catfish fillet", + "Price": 2740 + }, + { + "Name": "Cabbage salad", + "Price": 650 + }, + { + "Name": "Gundel pancakes", + "Price": 450 + } +] diff --git a/WHQ7HJ/WHQ7HJ/orders.json b/WHQ7HJ/WHQ7HJ/orders.json new file mode 100644 index 0000000..e69de29