照着课程敲了一遍代码:实际上有不少坑,再研究
超市类:
class SupperMarket { //创建仓库对象 CangKu ck = new CangKu(); public SupperMarket() { ck.JinPros("Acer", 1000); ck.JinPros("SamSung", 1000); ck.JinPros("JiangYou", 1000); ck.JinPros("Banana", 1000); } /// <summary> /// 跟用户交互的过程 /// </summary> public void AskBuying() { Console.WriteLine("欢迎光临,请问您需要些什么?"); Console.WriteLine("我们有 Acer、SamSung、JiangYou、Banana"); string strType = Console.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"); 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 + "," + "\t" + "货物编号:" + item.ID); } } /// <summary> /// 根据用户选择的打折方式返回一个打折对象 /// </summary> /// <param name="input">用户的选择</param> /// <returns>返回的父类对象 但是里面装的是子类对象</returns> 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; } return cal; } /// <summary> /// 根据用户买的货物计算总价钱 /// </summary> /// <param name="pros"></param> /// <returns></returns> public double GetMoney(ProductFather[] pros) { double realMoney = 0; for (int i = 0; i < pros.Length; i++) { realMoney += pros[i].Price; } return realMoney; } public void ShwoPros() { ck.ShowPros(); } }
打折类的父类:抽象类
/// <summary> /// 打折的父类 /// </summary> abstract class CalFather { /// <summary> /// 计算打折后应付多少钱 /// </summary> /// <param name="realMoney">打折前应付的价钱</param> /// <returns>打折后应付的钱</returns> public abstract double GetTotalMoney(double realMoney); }
不打折:
class CalNormal : CalFather { /// <summary> /// 不打折 /// </summary> /// <param name="realMoney"></param> /// <returns></returns> public override double GetTotalMoney(double realMoney) { return realMoney; } }
打折:折扣的算法
/// <summary> /// rate 折扣率,按折扣率打折 /// </summary> class CalRate : CalFather { /// <summary> /// 折扣率属性 /// </summary> public double Rate { get; set; } public CalRate(double rate) { this.Rate = rate; } public override double GetTotalMoney(double realMoney) { return realMoney * this.Rate; } }
满M减N的打折方式
/// <summary> /// 买M元送N元 /// </summary> class CalMN : CalFather { //买500送100 //买300送50 /// <summary> /// 买的数 /// </summary> public double M { get; set; } /// <summary> /// 送的数 /// </summary> public double N { get; set; } public CalMN(double m, double n) { this.M = m; this.N = n; } public override double GetTotalMoney(double realMoney) { if(realMoney >= this.M) { return realMoney - (int)(realMoney / this.M) * this.N; } else { return realMoney; } } }
Main函数
class Program { static void Main(string[] args) { //创建超市对象 SupperMarket sm = new SupperMarket(); //展示货物 sm.ShwoPros(); //跟用户交互 sm.AskBuying(); Console.ReadKey(); } }