C# 超市收银系统
│ 41_超市收银系统.csproj
│ Acer.cs
│ Banada.cs
│ CalFather.cs
│ CalMN.cs
│ CalNormal.cs
│ CalRate.cs
│ ProductFather.cs
│ Program.cs
│ SamSung.cs
│ SoySauce.cs
│ SuperMarket.cs
│ Warehouse.cs
Program.cs
using System; namespace _41_超市收银系统 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); // 创建对象 SuperMarket superMarket = new SuperMarket(); // 展示货物 superMarket.ShowPros(); // 用户交互 superMarket.AskBuying(); } } }
ProductFather.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class ProductFather { public double Price { get; set; } public string Name { get; set; } public string ID { get; set; } public ProductFather(string id, string name, double price) { this.ID = id; this.Name = name; this.Price = price; } } }
Acer.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class Acer: ProductFather { public Acer(string id, double price, string name) : base(id, name, price) { } } }
SamSung.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class SamSung:ProductFather { public SamSung(string id, double price, string name) : base(id, name, price) { } } }
SoySauce.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class SoySauce:ProductFather { public SoySauce(string id, double price, string name) : base(id, name, price) { } } }
Banada.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class Banada: ProductFather { public Banada(string id, double price, string name) : base(id, name, price) { } } }
Warehouse.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class Warehouse { //// 存储货物 /////List<Acer> listAcer = new List<Acer>(); //List<SamSung> listSam = new List<SamSung>(); //List<SoySauce> listSoySauce = new List<SoySauce>(); //List<Banada> listBanada = new List<Banada>(); List<List<ProductFather>> listProduct = new List<List<ProductFather>>(); /// <summary> /// 在创建仓库的时候,向仓库中添加货架 /// </summary> public Warehouse() { for (int i = 0; i < 4; i++) { listProduct.Add(new List<ProductFather>()); } } /// <summary> /// 进货 /// </summary> /// <param name="strType">货物的类型</param> /// <param name="count">货物的数量</param> public void GetPros(string strType, int count) { for (int i = 0; i < count; i++) { switch (strType) { case "Acer": listProduct[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏基笔记本")); break; case "SamSung": listProduct[1].Add(new SamSung(Guid.NewGuid().ToString(), 800, "三星")); break; case "SoySauce": listProduct[2].Add(new SoySauce(Guid.NewGuid().ToString(), 50, "酱油")); break; case "Banaba": listProduct[3].Add(new Banada(Guid.NewGuid().ToString(), 5, "香蕉")); break; } } } /// <summary> /// 从仓库中提取货物 /// </summary> /// <param name="strType">货物的类型</param> /// <param name="count">货物的数量</param> /// <returns></returns> public ProductFather[] PopPros(string strType, int count) { ProductFather[] pros = new ProductFather[count]; for (int i = 0,length = pros.Length; i < length; i++) { switch (strType) { case "Acer": pros[i] = listProduct[0][0]; listProduct[0].RemoveAt(0); break; case "SamSung": pros[i] = listProduct[1][0]; listProduct[1].RemoveAt(0); break; case "SoySauce": pros[i] = listProduct[2][0]; listProduct[2].RemoveAt(0); break; case "Banaba": pros[i] = listProduct[3][0]; listProduct[3].RemoveAt(0); break; } } return pros; } /// <summary> /// 显示仓库货物 /// </summary> public void ShowPros() { foreach (var item in listProduct) { if (item.Count !=0) { Console.WriteLine("we have 【{0}】 is 【{1}】,one price is 【{2}】", item[0].Name, item.Count,item[0].Price); } } } } }
SuperMarket.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class SuperMarket { Warehouse warehouse = new Warehouse(); public SuperMarket() { warehouse.GetPros("Acer", 10); warehouse.GetPros("SamSung", 10); warehouse.GetPros("SoySauce", 10); warehouse.GetPros("Banaba", 10); } public void AskBuying() { Console.WriteLine("What do you want to buy?"); Console.WriteLine("We have Acer、 SamSung、 SoySaue and Banaba"); string strType = Console.ReadLine(); Console.WriteLine("How much do you want?"); int count = Convert.ToInt32(Console.ReadLine()); ProductFather[] pros = warehouse.PopPros(strType, count); double money = GetMoney(pros); Console.WriteLine("您总共应付款{0}元", money); Console.WriteLine("请选择打折方式:\n 1、不打折\n 2、打九折\n 3、打85折\n 4、满300送50\n 5、满500送100\n"); string calType = Console.ReadLine(); CalFather cal = GetCal(calType); double totalMoney = cal.GetTotalMoney(money); Console.WriteLine("你的折后价格为{0}", totalMoney); Console.WriteLine("以下是你的货物信息"); SmallTicket(pros); } /// <summary> /// 根据货物计算总价格 /// </summary> /// <param name="pros"></param> /// <returns></returns> public double GetMoney(ProductFather[] pros) { double money = 0; foreach (var item in pros) { money += item.Price; } return money; } /// <summary> /// 根据打折的类型获取打折对象 /// </summary> /// <param name="calType">打折类型</param> /// <returns>返回一个打折的父类对象,里面装着一个子类对象 </returns> public CalFather GetCal(string calType) { CalFather cal = null; switch (calType) { case "1": cal = new CalNormal(); break; case "2": cal = new CalRate(0.9); break; case "3": cal = new CalRate(0.85); break; case "4": cal = new CalMN(300, 50); break; case "5": cal = new CalMN(500, 100); break; } return cal; } public void ShowPros() { warehouse.ShowPros(); } public void SmallTicket(ProductFather[] pros) { foreach (var item in pros) { Console.WriteLine("{0} * 1 {1}", item.Name, item.Price); } } } }
CalFather.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { /// <summary> /// 打折的父类 /// </summary> abstract class CalFather { /// <summary> /// 计算打折后应付多少前 /// </summary> /// <param name="money">折前价格</param> /// <returns>折后价格</returns> public abstract double GetTotalMoney(double money); } }
CalNormal.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { /// <summary> /// 不打折类 /// </summary> class CalNormal:CalFather { public override double GetTotalMoney(double money) { return money; } } }
CalRate.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { /// <summary> /// 按折扣率打折 /// </summary> class CalRate : CalFather { public double Rate { get; set; } public CalRate(double rate) { this.Rate = rate; } public override double GetTotalMoney(double money) { return money*this.Rate; } } }
CalMN.cs
using System; using System.Collections.Generic; using System.Text; namespace _41_超市收银系统 { class CalMN: CalFather { public double M { get; set; } public double N { get; set; } public CalMN(double m, double n) { this.M = m; this.N = n; } public override double GetTotalMoney(double money) { if (money > this.M) { money -= (int)(money/this.M) * N; } return money; } } }