C# - Program to manage orders in a Restaurant.


A simple console program to manage orders in a Restaurant.

CODE
using System; using System.Collections.Generic; class Item { private string itemName; private double itemPrice; private int id; public int ID { get => id;} public double ItemPrice { get => itemPrice; } public Item(int id, string itemName, double itemPrice) { this.id = id; this.itemName = itemName; this.itemPrice = itemPrice; } public override string ToString() { string result = ""; result += id + ". " + itemName + "\t: " + itemPrice.ToString("0.00"); return result; } } class Items : List<Item> { private static int ID_COUNTER = 1; public Items() { this.Add(new Item(ID_COUNTER, "Chicken Dlght Pizza", 245.00)); ID_COUNTER++; this.Add(new Item(ID_COUNTER, "Chicken Normal Pizza", 200.00)); ID_COUNTER++; this.Add(new Item(ID_COUNTER, "Veg Normal Pizza", 120.00)); ID_COUNTER++; this.Add(new Item(ID_COUNTER, "Veg Special Pizza", 150.00)); ID_COUNTER++; } public bool IsIDvalid(string userInput, out int id) { bool result = false; bool flag = int.TryParse(userInput, out id); if (flag) { if (id > 0 && id < ID_COUNTER) result = true; } return result; } public override string ToString() { string result = ""; int i = 0; foreach(Item item in this) { result += item.ToString(); if(i < this.Count-1) result += "\n"; i++; } return result; } public Item GetItem(int id) { return this.Find(a => a.ID == id); } } class Order { private Item item; private int quantity; private double total; public double Total { get => total; } public Order(Item item, int quantity) { this.item = item; this.quantity = quantity; this.total = quantity * item.ItemPrice; } public override string ToString() { string result = item.ToString() + "\n"; result += "Quantity: " + quantity + "\n"; result += "Total: " + total; return result; } } class Program { public static void Main(string[] args) { try { Items items = new Items(); List<Order> orders = new List<Order>(); while (true) { string userInput = getMenuInput(items); if (userInput == "x") break; int id = -1; if (items.IsIDvalid(userInput, out id)) { int quantity = getItemCountInput(); if (quantity < 1) Console.WriteLine("Invalid quantity."); else { Order order = new Order(items.GetItem(id), quantity); orders.Add(order); } } else Console.WriteLine("Invalid choice."); Console.WriteLine(""); } getBill(orders); } catch(Exception ex) { Console.WriteLine("Error: " + ex.Message); } Console.WriteLine("Thanks. Good Bye. Press any key to exit."); Console.ReadKey(); } public static string getMenuInput(Items items) { Console.WriteLine(items); Console.WriteLine("x: Exit"); Console.Write("Enter your choice: "); return Console.ReadLine().Trim().ToLower(); } public static int getItemCountInput() { Console.Write("How many items? "); string userInput = Console.ReadLine().Trim(); int quantity = -1; int.TryParse(userInput, out quantity); return quantity; } public static void getBill(List<Order> orders) { double orderTotal = 0.0; Console.WriteLine("\nBILL:"); foreach (Order order in orders) { Console.WriteLine(order); orderTotal += order.Total; } Console.WriteLine("\nORDER TOTAL: " + orderTotal); } }

INPUT & OUTPUT
The below is the output of the program.

Comments