超市购物小票C#程序代码
时间: 2023-12-13 13:34:05 浏览: 34
根据提供的引用内容,我们可以为您提供一个简单的超市购物小票C#程序代码,代码如下:
```csharp
using System;
using System.Collections.Generic;
namespace Supermarket
{
class Program
{
// 商品项类
class GoodsItem
{
public string Name { get; set; } // 商品名称
public string Id { get; set; } // 商品编号
public double Price { get; set; } // 商品单价
public int Number { get; set; } // 商品数量
public string Unit { get; set; } // 商品单位
public double Money { get; set; } // 商品总价
public GoodsItem(string name, string id, double price, int number, string unit, double money)
{
Name = name;
Id = id;
Price = price;
Number = number;
Unit = unit;
Money = money;
}
}
// 购物车类
class ShoppingCar
{
private List<GoodsItem> items = new List<GoodsItem>(); // 商品项集合
// 添加商品到购物车
public void AddItemToShoppingCar(GoodsItem item)
{
// 遍历购物车中的所有商品项
for (int index = 0; index < items.Count; index++)
{
// 获取每一个商品项
GoodsItem goodsItem = items[index];
if (goodsItem.Id.Equals(item.Id))
{
goodsItem.Number += item.Number;
goodsItem.Money = goodsItem.Price * goodsItem.Number;
return;
}
}
// 将要购买的商品项添加到集合中
items.Add(item);
}
// 计算购物车中商品的总价
public double CalculateTotalMoney()
{
double totalMoney = 0;
foreach (GoodsItem item in items)
{
totalMoney += item.Money;
}
return totalMoney; }
// 打印购物小票
public void PrintShoppingTicket()
{
Console.WriteLine("**********欢迎光临超市**********");
Console.WriteLine("名称\t编号\t单价\t数量\t单位\t金额");
foreach (GoodsItem item in items)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", item.Name, item.Id, item.Price, item.Number, item.Unit, item.Money);
}
Console.WriteLine("**********************************");
Console.WriteLine("总金额:{0}", CalculateTotalMoney());
Console.WriteLine("**********************************");
}
}
static void Main(string[] args)
{
// 初始化商品
GoodsItem item1 = new GoodsItem("卫龙", "001", 15.5, 0, "斤", 0);
GoodsItem item2 = new GoodsItem("火爆鸡筋", "002", 14.5, 0, "包", 0);
GoodsItem item3 = new GoodsItem("唐僧肉", "003", 345, 0, "个", 0);
GoodsItem item4 = new GoodsItem("话梅糖", "004", 199, 0, "G", 0);
// 将商品添加到购物车中
ShoppingCar shoppingCar = new ShoppingCar();
shoppingCar.AddItemToShoppingCar(item1);
shoppingCar.AddItemToShoppingCar(item2);
shoppingCar.AddItemToShoppingCar(item3);
shoppingCar.AddItemToShoppingCar(item4);
// 打印购物小票
shoppingCar.PrintShoppingTicket();
}
}
}
```