Classe Item:
public class Item
{
private int _stock;
private double _price;
public string Name { get; set; }
public int Stock
{
get => _stock;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(Stock), "The stock cannot be negative.");
_stock = value;
}
}
public double Price
{
get => _price;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(Price), "The price cannot be negative.");
_price = value;
}
}
public bool IsAvailable => Stock > 0;
public string DetailedDescription => $"Item: {this.Name} - Price: {this.Price:F2} - Stock: {this.Stock}";
public Item(string name, int stock, double price)
{
Name = name;
Stock = stock;
Price = price;
}
}
Classe Menu:
public class Menu
{
private readonly List<Item> _items = new List<Item>();
public void AddItem(Item item)
{
_items.Add(item);
}
public void ShowItems()
{
Console.WriteLine("Menu items:");
foreach (Item item in _items)
{
Console.WriteLine(item.DetailedDescription);
}
Console.WriteLine();
}
}
Classe Table:
public class Table
{
public int Number { get; set; }
public bool Reserved { get; set; } = false;
public Table(int number)
{
Number = number;
}
public void ReserveTable()
{
Reserved = true;
Console.WriteLine($"Table {Number} successfully reserved!\n");
}
}
Classe Restaurant:
public class Restaurant
{
public string Name { get; set; }
public Menu Menu;
public List<Table> Tables = new List<Table>();
public Restaurant(string name, Menu menu, List<Table> tables)
{
Name = name;
Menu = menu;
Tables = tables;
}
public void ShowAvailableTables()
{
foreach (Table table in Tables)
{
if (!table.Reserved)
{
Console.WriteLine($"Table {table.Number} is available.");
}
}
Console.WriteLine();
}
}
Classe Order:
public class Order
{
public int OrderNumber { get; set; }
public double Price { get; }
public Item Item { get; set; }
public Table Table { get; set; }
public string DetailedDescription => $"Order: {this.OrderNumber} - Item {this.Item.Name} - Table {this.Table.Number}";
public Order(int orderNumber, Item item, Table table)
{
if (!item.IsAvailable)
throw new InvalidOperationException($"Item '{item.Name}' is out of stock.");
OrderNumber = orderNumber;
Item = item;
Table = table;
Price = item.Price;
Item.Stock--;
}
}
Instâncias:
Item burger = new Item("Cheese Burger", 12, 15.90);
Item soda = new Item("Coca-Cola", 25, 6.50);
Item fries = new Item("French Fries", 8, 9.00);
Menu menu = new Menu();
menu.AddItem(burger);
menu.AddItem(soda);
menu.AddItem(fries);
menu.ShowItems();
Table table1 = new Table(1);
Table table2 = new Table(2);
Table table3 = new Table(3);
table1.ReserveTable();
table3.ReserveTable();
Restaurant restaurant = new Restaurant("Burger House", menu, new List<Table> { table1, table2, table3 });
restaurant.ShowAvailableTables();
Order order = new Order(1, burger, table1);
Console.WriteLine(order.DetailedDescription);