c#超市收银系统,多态的使用

using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace _超市收银系统
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建超市对象
            suppermarket sm = new suppermarket();
           
            //跟用户交互
            sm.askbuying();
            Console.ReadKey();












            //guid产生不会重复的编号
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.ReadKey();
        }
    }
    public class father//商品父类
    {
        public double Price
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string Id
        {
            get;
            set;
        }
        public father(string id, double price, string name)
        {
            
            this.Id = id;
            this.Price = price;
            this.Name = name;
        }




    }
    public class sansung : father
    {
        public sansung(string id, double price, string name)
            : base(id, price, name)
        {


        }
    }
    public class Acer : father
    {
        public Acer(string id, double price, string name)
            : base(id, price, name)
        {


        }
    }
    public class jiangyou : father
    {
        public jiangyou(string id, double price, string name)
            : base(id, price, name)
        {




        }


    }


    public class cangku//存货物
    {
        List<List<father>> list = new List<List<father>>();//存父类,list当成货架
      
        public cangku()//创建仓库对象的时候添加货架
        {
            list.Add(new List<father>());//添加的是集合
            list.Add(new List<father>());
            list.Add(new List<father>());
            list.Add(new List<father>());
        }
        /// <summary>
        /// 进货
        /// </summary>
        /// <param name="type">货物的类型</param>
        /// <param name="count">货物的数量</param>
        public void getpros(string type, int count)
    {
        for (int i = 0; i <count; i++)
        {
            switch (type)
            {
                case "Acer": list[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "鸿基"));
                    break;
                case "samsung": list[1].Add(new sansung(Guid.NewGuid().ToString(), 1500,"三星"));
                    break;
                case "jaingyou": list[2].Add(new jiangyou(Guid.NewGuid().ToString(), 10,"三星"));
                    break;
            }
        }
    }


        public father[]  quhuo(string type, int count)//从仓库取货到超市,存到数组中
        {
            father[] pros = new father[count];
            for (int i = 0; i < count; i++)//对货物类型进行判断,通过数组下标拿货
            {
                switch (type)
                {
                        
                    case "acer": pros[i] = list[0][0];//前面数组下标是对应的货架,后面的数组下标用来选择第。。个
                        list[0].RemoveAt(0);            //这样返回一个父类对象,存到pros数组中,拿一个少一个
                        break;
                    case "jaingyou": pros[i] = list[2][0];//酱油在第二个货架
                        list[2].RemoveAt(0);
                        break;
                    case "samsung": pros[i] = list[1][0];
                        list[1].RemoveAt(0);
                        break;
                }
            }
            return pros;//因为不知道它去仓库取什么,所以返回一个父类
 
        }
    }




     class suppermarket
    {
        //创建仓库对象
        cangku ck = new cangku();
        public suppermarket()
        {
            ck.getpros("acer", 1000);//从仓库取1000件鸿基电脑
            ck.getpros("jaingyou", 100);
        }
        /// <summary>
        /// 跟用户交互
        /// </summary>
        public void askbuying()
        {
            Console.WriteLine("买什么");
            Console.WriteLine("我们有手机酱油");
            string type = Console.ReadLine();
            Console.WriteLine("你要多少");
            int count = Convert.ToInt32(Console.ReadLine());
            //取货
            father[] pros = ck.quhuo(type, count);
            //算钱
            double realmoney = getmoney(pros);
            Console.WriteLine("你要付{0}元",realmoney);
            Console.WriteLine("请选择你的打折方式1-不打折,2-九折,3-买三百送五十,4-买五百送一百");
            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 Calmn(300, 50);
                    break;
            }
            return cal;
        }








        //算钱,根据用户买的货物总价


        public double getmoney(father[] pros)
        {
            double realmoney = 0;
            for (int i = 0; i < pros.Length; i++)
            {
                realmoney += pros[i].Price;
            }
            return realmoney;
                
        }
       
    }


    /// <summary>
    /// 打折的父类
    /// </summary>
   abstract class calfather
    {
       /// <summary>
       /// 打折要付多少
       /// </summary>
       /// <param name="realmoney">打折前应付</param>
       /// <returns>打折后应付</returns>
       public abstract double gettotalmoney(double realmoney);
       


    }
   class calnormal : calfather
   {
       public override double gettotalmoney(double realmoney)
       {
           return realmoney;
       }


   }
   class calrate : calfather
   {
       public double Rate//折扣率
       {
           get;
           set;
       }
       public calrate(double rate)
       {
           this.Rate = rate;
       }
       public override double gettotalmoney(double realmoney)
       {
           return realmoney * this.Rate;
       }
   }
    /// <summary>
    /// 买m元,送n元
    /// </summary>
   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 realmoney)
       {
           if (realmoney >= this.M)
           {
               return realmoney - (int)(realmoney/this.M)*this.N;
           }
           else
           {
               return realmoney;
           }
       }
   }


}
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值