1、C#简单超市收银系统源码C#简单超市收银系统源码using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class ProductFather public double Price get; set; public string Name get; set; public string ID get; set; public ProductFather(string id, double
2、price, string Name) this.ID = id; this.Price = price; this.Name = Name; using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class SamSung:ProductFather public SamSung(string id, double price, string Name) : base(id, price
3、, Name) using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class Acer:ProductFather public Acer(string id, double price, string Name) : base(id, price, Name) using System;using System.Collections.Generic;using System.Lin
4、q;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class Banana : ProductFather public Banana(string id, double price, string Name) : base(id, price, Name) using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市
5、收银系统 class JiangYou:ProductFather public JiangYou(string id, double price, string Name) : base(id, price, Name) using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class SupperMarket /创建仓库对象 CangKu ck = new CangKu(); / /
6、创建超市对象的时候,给仓库的货架上导入货物 / public SupperMarket() ck.JinPros(Acer, 1000); ck.JinPros(SamSung, 1000); ck.JinPros(JiangYou, 1000); ck.JinPros(Banana, 1000); / / 跟用户交互的过程 / public void AskBuying() Console.WriteLine(欢迎观临,请问您需要些什么?); Console.WriteLine(我们有 Acer、SamSung、Jiangyou、Banana); string strType = Conso
7、le.ReadLine(); Console.WriteLine(您需要多少?); int count = Convert.ToInt32(Console.ReadLine(); /去仓库取货物 ProductFather pros = ck.QuPros(strType, count); /下面该计算价钱了 double realMoney = GetMoney(pros); Console.WriteLine(您总共应付0元, realMoney); Console.WriteLine(请选择您的打折方式 1-不打折 2-打九折 3-打85 折 4-买300送50 5-买500送100);
8、 string input = Console.ReadLine(); /通过简单工厂的设计模式根据用户的舒服获得一个打折对象 CalFather cal = GetCal(input); double totalMoney = cal.GetTotalMoney(realMoney); Console.WriteLine(打完折后,您应付0元, totalMoney); Console.WriteLine(以下是您的购物信息); foreach (var item in pros) Console.WriteLine(货物名称:+item.Name+,+t+货物单价:+item.Price+
9、,+t+货物编号:+item.ID); / / 根据用户的选择打折方式返回一个打折对象 / / 用户的选择 / 返回的父类对象 但是里面装的是子类对象 public CalFather GetCal(string input) CalFather cal = null; switch (input) 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)
10、; break; case 5: cal = new CalMN(500, 100); break; return cal; / / 根据用户买的货物计算总价钱 / / / public double GetMoney(ProductFather pros) double realMoney = 0; /realMoney = pros0.Price * pros.Length; for (int i = 0; i pros.Length; i+) realMoney += prosi.Price; / realMoney = prosi * pros.Length; return realM
11、oney; public void ShowPros() ck.ShowPros(); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 / / 不打折 该多少钱就多少钱 / class CalNormal:CalFather public override double GetTotalMoney(double realMoney) return realMoney; using S
12、ystem;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 / / 按折扣率打折 / class CalRate:CalFather / / 折扣率 / public double Rate get; set; public CalRate(double rate) this.Rate = rate; public override double GetTotalMoney(double realMoney)
13、 return realMoney * this.Rate; using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 / / 买M元 送N元 / class CalMN:CalFather /买500送100 public double M get; set; public double N get; set; public CalMN(double m, double n) this.M
14、= m; this.N = n; public override double GetTotalMoney(double realMoney) /600 -100 /1000-200 /1200 if (realMoney = this.M) return realMoney - (int)(realMoney / this.M) * this.N; else return realMoney; using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa
15、ding.Tasks;namespace _16超市收银系统 class CangKu ListList list = new ListList(); / /向用户展示货物 / public void ShowPros() foreach (var item in list) Console.WriteLine(我们超市有: + item0.Name + , + t + 有 + item.Count + 个, + t + 每个 + item0.Price + 元); /list0存储Acer电脑 /list1存储三星手机 /list2存储酱油 /list3存储香蕉 / / 在创建仓库对象的时候
16、 像仓库中添加货架 / public CangKu() list.Add(new List(); list.Add(new List(); list.Add(new List(); list.Add(new List(); / / 进货 / / 货物的类型 / 货物的数量 public void JinPros(string strType, int count) for (int i = 0; i count; i+) switch (strType) case Acer: list0.Add(new Acer(Guid.NewGuid().ToString(), 1000, 宏基笔记本);
17、 break; case SamSung: list1.Add(new SamSung(Guid.NewGuid().ToString(), 2000, 棒之手机); break; case JiangYou: list2.Add(new JiangYou(Guid.NewGuid().ToString(), 10, 老抽酱油); break; case Banana: list3.Add(new Banana(Guid.NewGuid().ToString(), 50, 大香蕉); break; / / 从仓库中提取货物 / / / / public ProductFather QuPros
18、(string strType, int count) ProductFather pros = new ProductFathercount; for (int i = 0; i pros.Length; i+) switch (strType) case Acer: prosi = list00; list0.RemoveAt(0); break; case SamSung: prosi = list10; list1.RemoveAt(0); break; case JiangYou: prosi = list20; list2.RemoveAt(0); break; case Bana
19、na: prosi = list30; list3.RemoveAt(0); break; return pros; using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class SupperMarket /创建仓库对象 CangKu ck = new CangKu(); / / 创建超市对象的时候,给仓库的货架上导入货物 / public SupperMarket() ck.JinP
20、ros(Acer, 1000); ck.JinPros(SamSung, 1000); ck.JinPros(JiangYou, 1000); ck.JinPros(Banana, 1000); / / 跟用户交互的过程 / public void AskBuying() Console.WriteLine(欢迎观临,请问您需要些什么?); Console.WriteLine(我们有 Acer、SamSung、Jiangyou、Banana); string strType = Console.ReadLine(); Console.WriteLine(您需要多少?); int count =
21、 Convert.ToInt32(Console.ReadLine(); /去仓库取货物 ProductFather pros = ck.QuPros(strType, count); /下面该计算价钱了 double realMoney = GetMoney(pros); Console.WriteLine(您总共应付0元, realMoney); Console.WriteLine(请选择您的打折方式 1-不打折 2-打九折 3-打85 折 4-买300送50 5-买500送100); string input = Console.ReadLine(); /通过简单工厂的设计模式根据用户的
22、舒服获得一个打折对象 CalFather cal = GetCal(input); double totalMoney = cal.GetTotalMoney(realMoney); Console.WriteLine(打完折后,您应付0元, totalMoney); Console.WriteLine(以下是您的购物信息); foreach (var item in pros) Console.WriteLine(货物名称:+item.Name+,+t+货物单价:+item.Price+,+t+货物编号:+item.ID); / / 根据用户的选择打折方式返回一个打折对象 / / 用户的选择
23、 / 返回的父类对象 但是里面装的是子类对象 public CalFather GetCal(string input) CalFather cal = null; switch (input) 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; re
24、turn cal; / / 根据用户买的货物计算总价钱 / / / public double GetMoney(ProductFather pros) double realMoney = 0; /realMoney = pros0.Price * pros.Length; for (int i = 0; i pros.Length; i+) realMoney += prosi.Price; / realMoney = prosi * pros.Length; return realMoney; public void ShowPros() ck.ShowPros(); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _16超市收银系统 class Program static void Main(string args) /创建超市对象 SupperMarket sm = new SupperMarket(); /展示货物 sm.ShowPros(); /跟用户交互 sm.AskBuying(); Console.ReadKey();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1