用c#写复杂的点菜系统
时间: 2024-05-12 07:13:54 浏览: 14
一个复杂的点菜系统需要考虑很多方面,包括菜品的分类、菜品的属性、价格计算、订单管理等等。以下是一个简单的点菜系统的代码示例:
```csharp
using System;
using System.Collections.Generic;
namespace PointOfSale
{
class Program
{
static void Main(string[] args)
{
// 菜单
var menu = new Dictionary<string, MenuItem>()
{
{"1", new MenuItem("辣子鸡丁", "主菜", 38.0)},
{"2", new MenuItem("宫保鸡丁", "主菜", 32.0)},
{"3", new MenuItem("小炒肉", "主菜", 28.0)},
{"4", new MenuItem("清炒时蔬", "素菜", 18.0)},
{"5", new MenuItem("麻婆豆腐", "素菜", 22.0)},
{"6", new MenuItem("米饭", "配菜", 2.0)}
};
// 订单
var order = new Order();
// 用户输入
while (true)
{
Console.WriteLine("欢迎光临!");
Console.WriteLine("菜单:");
foreach (var item in menu)
{
Console.WriteLine($"{item.Key}: {item.Value.Name} ({item.Value.Category}) - {item.Value.Price}元");
}
Console.WriteLine("");
Console.WriteLine("请选择菜品编号(按回车键下单,按q键退出):");
var input = Console.ReadLine();
if (input.ToLower() == "q")
{
break;
}
else if (menu.ContainsKey(input))
{
order.AddItem(menu[input]);
Console.WriteLine($"{menu[input].Name} 已加入订单。");
}
else
{
Console.WriteLine("无效的输入,请重新输入菜品编号。");
}
Console.WriteLine("");
}
// 结账
Console.WriteLine($"总计:{order.TotalPrice}元");
Console.WriteLine("感谢光临,再见!");
}
}
// 菜品类
class MenuItem
{
public string Name { get; set; }
public string Category { get; set; }
public double Price { get; set; }
public MenuItem(string name, string category, double price)
{
Name = name;
Category = category;
Price = price;
}
}
// 订单类
class Order
{
private List<MenuItem> items = new List<MenuItem>();
public void AddItem(MenuItem item)
{
items.Add(item);
}
public double TotalPrice
{
get
{
double totalPrice = 0;
foreach (var item in items)
{
totalPrice += item.Price;
}
return totalPrice;
}
}
}
}
```
这个代码示例中,菜单的信息保存在一个 `Dictionary` 中,菜品类通过 `MenuItem` 类来表示,订单类通过 `Order` 类来表示。程序会循环读取用户的输入,如果输入的是菜品编号,则将该菜品加入订单中;如果输入的是 `q`,则退出程序。在程序结束时,会输出订单的总价。这只是一个简单的示例,实际的点菜系统需要根据实际需求来设计和实现。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)