帮我用面对对象c#写一个点菜程序
时间: 2023-11-25 08:07:00 浏览: 37
好的,下面是一个简单的点菜程序,使用了面向对象的C#语言:
```csharp
using System;
using System.Collections.Generic;
namespace OrderSystem
{
class Program
{
static void Main(string[] args)
{
FoodMenu menu = new FoodMenu();
menu.AddFood(new Food("Hamburger", 10.99));
menu.AddFood(new Food("Pizza", 12.99));
menu.AddFood(new Food("Hotdog", 6.99));
menu.AddFood(new Food("French Fries", 4.99));
menu.AddFood(new Food("Soda", 1.99));
Order order = new Order();
Console.WriteLine("Welcome to our restaurant!");
while (true)
{
Console.WriteLine("\nPlease select an option:");
Console.WriteLine("1. View menu");
Console.WriteLine("2. Add item to order");
Console.WriteLine("3. Remove item from order");
Console.WriteLine("4. View order");
Console.WriteLine("5. Exit");
int option = int.Parse(Console.ReadLine());
switch (option)
{
case 1:
menu.DisplayMenu();
break;
case 2:
Console.WriteLine("Enter the name of the item you want to add:");
string itemName = Console.ReadLine();
Food item = menu.FindFood(itemName);
if (item != null)
{
order.AddItem(item);
Console.WriteLine(itemName + " added to order.");
}
else
{
Console.WriteLine("Item not found.");
}
break;
case 3:
Console.WriteLine("Enter the name of the item you want to remove:");
itemName = Console.ReadLine();
item = order.FindItem(itemName);
if (item != null)
{
order.RemoveItem(item);
Console.WriteLine(itemName + " removed from order.");
}
else
{
Console.WriteLine("Item not found.");
}
break;
case 4:
order.DisplayOrder();
break;
case 5:
Console.WriteLine("Thank you for your order!");
return;
default:
Console.WriteLine("Invalid option.");
break;
}
}
}
}
class Food
{
public string Name { get; set; }
public double Price { get; set; }
public Food(string name, double price)
{
Name = name;
Price = price;
}
public override string ToString()
{
return Name + " - $" + Price.ToString("0.00");
}
}
class FoodMenu
{
private List<Food> menu = new List<Food>();
public void AddFood(Food food)
{
menu.Add(food);
}
public void DisplayMenu()
{
Console.WriteLine("Menu:");
foreach (Food food in menu)
{
Console.WriteLine(food);
}
}
public Food FindFood(string name)
{
foreach (Food food in menu)
{
if (food.Name.ToLower() == name.ToLower())
{
return food;
}
}
return null;
}
}
class Order
{
private List<Food> items = new List<Food>();
public void AddItem(Food item)
{
items.Add(item);
}
public void RemoveItem(Food item)
{
items.Remove(item);
}
public void DisplayOrder()
{
Console.WriteLine("Order:");
if (items.Count == 0)
{
Console.WriteLine("No items in order.");
}
else
{
double total = 0;
foreach (Food item in items)
{
Console.WriteLine(item);
total += item.Price;
}
Console.WriteLine("Total: $" + total.ToString("0.00"));
}
}
public Food FindItem(string name)
{
foreach (Food item in items)
{
if (item.Name.ToLower() == name.ToLower())
{
return item;
}
}
return null;
}
}
}
```
在这个程序中,我们定义了三个类:Food、FoodMenu和Order。
Food类表示每个菜品的名称和价格,这里使用了自动属性来简化代码。
FoodMenu类表示菜单,其中包含一个Food对象的列表。我们可以通过AddFood方法向菜单中添加菜品,使用DisplayMenu方法来展示菜单,使用FindFood方法来查找特定名称的菜品。
Order类表示订单,其中包含一个Food对象的列表。我们可以通过AddItem方法向订单中添加菜品,使用RemoveItem方法来删除订单中的菜品,使用DisplayOrder方法来展示订单,使用FindItem方法来查找特定名称的菜品。
在Main方法中,我们首先创建了一个FoodMenu对象,并向其中添加了几个菜品。然后创建了一个Order对象。接下来,使用while循环不断地提供菜单选项,根据用户选择执行相应的操作。这个程序可以让用户查看菜单、向订单中添加或删除菜品、查看订单和退出程序。
希望这个示例程序可以帮助你了解如何使用面向对象的C#来实现一个简单的点菜程序。