- using System;
 - using System.Collections.Generic;
 - using System.Text;
 -  
 - namespace 超市收银系统
 - {
 -     class cangKu
 -     {
 -         List<List<productFather>> list = new List<List<productFather>>();
 -         //这个list只接受装着productFather的list单元,list里放list
 -         //里面的每一个list都相当于仓库里的一个货架
 -         public cangKu()
 -         {
 -             list.Add(new List<productFather>()); //宏碁
 -             list.Add(new List<productFather>()); //棒之手机
 -             list.Add(new List<productFather>()); //酱油
 -             list.Add(new List<productFather>()); //banana
 -         }
 -         //构造四个货架
 -                           //货物名字,数量
 -         public void GetPros(string strType, int count)//将货物放到仓库货架上
 -         {
 -             for (int i = 0; i < count; i++)
 -             {
 -                 switch (strType)
 -                 {                                        //创建一个随机数作为ID,并将它转换为string类型
 -                     case "Acer":list[0].Add(new Acer(1000, "宏碁笔记本电脑", Guid.NewGuid().ToString()));
 -                         break;
 -                     case "sanSing":list[1].Add(new sanSing(2000, "棒之手机", Guid.NewGuid().ToString()));
 -                             break;
 -                     case "jiangYou":list[2].Add(new jiangYou(50, "酱油", Guid.NewGuid().ToString()));
 -                         break;
 -                     case "banana":list[3].Add(new banana(20,"banana",Guid.NewGuid().ToString()));
 -                         break;
 -                 } 
 -             }
 -         }
 -         public productFather[] OutPros(string strType, int count)//取出货物并加入购物车
 -         {
 -             productFather[] pros = new productFather[count];//创建购物车,收集货物
 -             for (int i = 0; i < count; i++)
 -             {
 -                 //这里应该加一个检查是否有货的代码
 -                 switch (strType)
 -                 {
 -                     case "Acer":pros[i] = list[0][0];//取货架上的第一个并给到购物车
 -                         list[0].RemoveAt(0);//删除第一个  
 -                         break;
 -                     case "sanSing":pros[i] = list[1][0];
 -                         list[1].RemoveAt(0);
 -                         break;
 -                     case "jiangYou":pros[i] = list[2][0];
 -                         list[2].RemoveAt(0);
 -                         break;
 -                     case "banana":pros[i] = list[3][0];
 -                         list[3].RemoveAt(0);
 -                         break;
 -                 }
 -             }
 -             return pros;//返回值
 -         }
 -         public void ShowPros()
 -         {
 -             foreach (var item in list)
 -             {
 -                 Console.WriteLine("超市里有商品"+item[0].Name+",\t"+"价格为"+item[0].Price);
 -                 //foreach是一个遍历循环,这一点易忘记
 -             }
 -         }
 -  
 -     }
 - }
 
 
这是仓库类
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 -  
 - namespace 超市收银系统
 - {
 -     class productFather
 -     {
 -         public double Price
 -         {
 -             get;
 -             set;
 -         }
 -         //这里用到了三个属性,但是并没有运用属性的功能对输入输出的值进行约束
 -        public string Name
 -         {
 -             get;
 -             set;
 -         }
 -         public string ID
 -         {
 -             get;
 -             set;
 -         }
 -         public productFather(double price, string name, string id)
 -         {
 -             this.Price = price;
 -             this.Name = name;
 -             this.ID = id;
 -         }
 -     }
 - }
 
 
这是商品父类
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 -  
 - namespace 超市收银系统
 - {
 -     class sanSing : productFather
 -     {
 -         public sanSing(double price,string name,string id)
 -             :base(price,name,id)
 -          {
 -              
 -          } 
 -  
 -     }
 - }
 -  
 
 
其中的一个子类
-  
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 -  
 - namespace 超市收银系统
 - {
 -     class supperMarket
 -     {
 -         cangKu ck = new cangKu();//只有创建了仓库对象才能调用其中的方法,创建一个实例
 -                                  //这个创建对象的过程中,构造函数也给他创建了四个货架
 -         productFather[] pros; 
 -  
 -         public supperMarket()//创建suppermarkket的时候自动导入以下货物
 -         {
 -             ck.GetPros("Acer",1000);
 -             ck.GetPros("sanSing",1000);
 -             ck.GetPros("jiangYou",1000);
 -             ck.GetPros("banana",1000);
 -         }
 -         public void AskBuying()
 -         {
 -             Console.WriteLine("请问客人想买点什么?");
 -             string strType = Console.ReadLine();
 -             Console.WriteLine("请问客人想要多少啊?");
 -             int count = Convert.ToInt32(Console.ReadLine());
 -             //取出货物
 -             pros = ck.OutPros(strType,count);
 -             Console.WriteLine("请选择你的打折方式--1xx,2xx,3xx");
 -             string input = Console.ReadLine();
 -             double money = GetMoney(pros);
 -             //通过简单工厂设计模式得到一个折扣对象
 -             //简单工厂设计模式:根据用户的输入返回一个对象
 -             CalFather cal = GetCal(input);
 -             double realMoney = cal.GetTotalMoney(money);
 -             Console.WriteLine("打折前应付{0}元,打折后您实际应付{1}元",money,realMoney);
 -             Console.WriteLine("");
 -         }
 -         public double GetMoney(productFather[] pros)//计算价钱
 -         {
 -             double money = 0;
 -             foreach (var item in pros)
 -             {
 -                 money += item.Price;
 -             }
 -             return money;
 -         }
 -         public CalFather GetCal(string input)
 -         {
 -             CalFather cal = null;//利用抽象类实现了多态,父类里可以放子类对象
 -             switch (input)
 -             {
 -                 case "1":cal = new CalNormal();
 -                     break;
 -                 case "https://cdn.jxasp.com:9143/image/2":cal = new CalRate(0.88);
 -                     break;
 -                 case "3":cal = new CalMN(500,50);
 -                     break;
 -             }
 -             return cal;
 -         }
 -     }
 - }
 
 
这是超市类
-  abstract class CalFather
 -     {
 -         public abstract double GetTotalMoney(double money);//抽象类中不能有实例成员
 -         
 -     }
 
 
用抽象类实现打折多态
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 -  
 - namespace 超市收银系统
 - {
 -     class CalNormal : CalFather
 -     {
 -         public override double GetTotalMoney(double money)//对父类抽象方法的重写(alt+shift+f10)
 -         {
 -             return money;
 -         }
 -         //不打折类
 -     }
 - }
 
 
其中的一个类

运行结果
仅能进行一种商品的购买,以后再改吧。

                


















