Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WHQ7HJ - Éttermi rendeléskezelő #38

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions WHQ7HJ/WHQ7HJ.sln
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions WHQ7HJ/WHQ7HJ/MenuItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace WHQ7HJ
{
public record MenuItem(string Name, int Price);

}
51 changes: 51 additions & 0 deletions WHQ7HJ/WHQ7HJ/MenuService.cs
Original file line number Diff line number Diff line change
@@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ez csak windowson fog működni így



public List<MenuItem> LoadMenu()
{
try
{
string json = File.ReadAllText(_filePath);
return JsonSerializer.Deserialize<List<MenuItem>>(json) ?? new List<MenuItem>();
}
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<MenuItem> menu)
{
Console.WriteLine("");
Console.WriteLine("Menu");
Console.WriteLine("-----");
foreach (var item in menu)
{
Console.WriteLine($"{item.Name} - {item.Price:C}");
}
}


}
}
5 changes: 5 additions & 0 deletions WHQ7HJ/WHQ7HJ/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace WHQ7HJ
{
public record Order(string CustomerName, string OrderDate, List<MenuItem> MenuItems);

}
143 changes: 143 additions & 0 deletions WHQ7HJ/WHQ7HJ/OrderService.cs
Original file line number Diff line number Diff line change
@@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Szintén csak Windows



public List<Order> LoadOrders()
{
try
{
string json = File.ReadAllText(_filePath);
return JsonSerializer.Deserialize<List<Order>>(json) ?? new List<Order>();
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
return new List<Order>();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new List<Order>();
}

}

public Order CreateOrder(List<MenuItem> 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<MenuItem>();

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<Order> 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<Order> 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<Order> 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<Order> orders)
{
Console.WriteLine($"Total revenue: {orders.Sum(order => order.MenuItems.Sum(item => item.Price))} Ft.");
}


}
}
48 changes: 48 additions & 0 deletions WHQ7HJ/WHQ7HJ/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
10 changes: 10 additions & 0 deletions WHQ7HJ/WHQ7HJ/WHQ7HJ.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
38 changes: 38 additions & 0 deletions WHQ7HJ/WHQ7HJ/menu.json
Original file line number Diff line number Diff line change
@@ -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
}
]
Empty file added WHQ7HJ/WHQ7HJ/orders.json
Empty file.