c#超市收银系统

一个超市我们先来准备货物;

在这里我们只准备SamSung,Acer,Banana,JiangYou这四个货物;

运用多态的知识我们先来写出产品的父类(ProductFather类);

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

namespace 超市收银系统
{
    internal class ProductFather
    {
        
        public string Id
        {
            set;get;
        }
        public double Price
        {
            set;get;
        }
        public string Name
        {
            set;get;
        }
        public ProductFather(string id, double price, string name)
        {
            
            this.Id = id;
            this.Price = price;
            this.Name = name;
        }
    }
}

然后四个产品继承父类的属性;

SamSung 

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

namespace 超市收银系统
{
    internal class SamSung:ProductFather
    {
        public SamSung(string id,double price,string name):base(id, price,name)
        {
            
        }
    }
}

Acer 

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

namespace 超市收银系统
{
    internal class Acer : ProductFather
    {
        public Acer(string id, double price, string name) : base(id, price,name)
        {
        }
    }
}

Banana 

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

namespace 超市收银系统
{
    internal class Banana : ProductFather
    {
        public Banana(string id, double price,string name) : base(id, price,name)
        {
        }
    }
}

JIangYou 

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

namespace 超市收银系统
{
    internal class JiangYou : ProductFather
    {
        public JiangYou(string id, double price,string name) : base(id, price,name)
        {
        }
    }
}

1.将商品要放到仓库里;

所以创建仓库类,

但是仓库里还要有货架所以这里用

 List<List<ProductFather>> list = new List<List<ProductFather>>();这个集合,

可以理解为二维集合

2.创建仓库对象时向里面添加货架,用以下构造方法

 public CangKu()
        {
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());

        }

3.然后我们要进货,使用下列方法

   public void JinPro(string type, int count)
        {
            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                    case "SamSung":
                        list[0].Add(new SamSung(Guid.NewGuid().ToString(), 14000, "三星手机"));
                        break;
                    case "Acer":
                        list[1].Add(new Acer(Guid.NewGuid().ToString(), 12000, "Acer电脑"));
                        break;
                    case "Banana":
                        list[2].Add(new Banana(Guid.NewGuid().ToString(), 10, "大香蕉"));
                        break;
                    case "JiangYou":
                        list[3].Add(new JiangYou(Guid.NewGuid().ToString(), 25, "酱油"));
                        break;
                    default:
                        break;
                }
            }
        }

ps:Guid类的NewGuid方法可以给创建一个编号;

4.取货方法

        public ProductFather[] QuHuo(string type, int count)
        {
            ProductFather[] pf = new ProductFather[count];
            for (int i = 0; i < pf.Length; i++)
            {
                switch (type)
                {
                    case "SamSung":
                        pf[i] = list[0][0];
                        list[0].RemoveAt(0);
                        break;
                    case "Acer":
                        pf[i] = list[1][0];
                        list[1].RemoveAt(0);
                        break;
                    case "Banana":
                        pf[i] = list[2][0];
                        list[2].RemoveAt(0);
                        break;
                    case "JiangYou":
                        pf[i] = list[3][0];
                        list[3].RemoveAt(0);
                        break;
                    default:
                        break;
                }
            }

            return pf;
        }

最后再写一个展示自己商品的方法

 public void ShowPro()
        {
            foreach (var item in list)
            {
                Console.WriteLine("我们超市有:" + item[0].Name + ",\t" + "有" + item.Count + "个,\t" + "每个" + item[0].Price + "元");
            }
        }

仓库类就结束了

以下是仓库类的全部代码;

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

namespace 超市收银系统
{
    internal class CangKu
    {
        List<List<ProductFather>> list = new List<List<ProductFather>>();
        public void ShowPro()
        {
            foreach (var item in list)
            {
                Console.WriteLine("我们超市有:" + item[0].Name + ",\t" + "有" + item.Count + "个,\t" + "每个" + item[0].Price + "元");
            }
        }
        //list[0]存三星,list[1]存Acer电脑,list[2]存香蕉,list[3]存酱油
        /// <summary>
        /// 在创建仓库对象的时候向里面添加货架
        /// </summary>
        public CangKu()
        {
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
        }/// <summary>
         /// 进货
         /// </summary>
         /// <param name="type">货物种类</param>
         /// <param name="count">货物的数量</param>
        public void JinPro(string type, int count)
        {
            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                    case "SamSung":
                        list[0].Add(new SamSung(Guid.NewGuid().ToString(), 14000, "三星手机"));
                        break;
                    case "Acer":
                        list[1].Add(new Acer(Guid.NewGuid().ToString(), 12000, "Acer电脑"));
                        break;
                    case "Banana":
                        list[2].Add(new Banana(Guid.NewGuid().ToString(), 10, "大香蕉"));
                        break;
                    case "JiangYou":
                        list[3].Add(new JiangYou(Guid.NewGuid().ToString(), 25, "酱油"));
                        break;
                    default:
                        break;
                }
            }
        }
        /// <summary>
        /// 取货
        /// </summary>
        /// <param name="type">货物种类</param>
        /// <param name="count">货物数量</param>
        /// <returns></returns>
        public ProductFather[] QuHuo(string type, int count)
        {
            ProductFather[] pf = new ProductFather[count];
            for (int i = 0; i < pf.Length; i++)
            {
                switch (type)
                {
                    case "SamSung":
                        pf[i] = list[0][0];
                        list[0].RemoveAt(0);
                        break;
                    case "Acer":
                        pf[i] = list[1][0];
                        list[1].RemoveAt(0);
                        break;
                    case "Banana":
                        pf[i] = list[2][0];
                        list[2].RemoveAt(0);
                        break;
                    case "JiangYou":
                        pf[i] = list[3][0];
                        list[3].RemoveAt(0);
                        break;
                    default:
                        break;
                }
            }

            return pf;
        }


    }
}

 有了货物仓库之后,需要一个超市

所以接下来写超市类

在超市类开始我们应该创建一个仓库类的对象;

CangKu ck = new CangKu();

然后写一个超市的构造方法;

public SuperMarket()
        {
            ck.JinPro("SamSung", 100);
            ck.JinPro("Acer", 100);
            ck.JinPro("Banana", 100);
            ck.JinPro("JiangYou", 100);
        }   

写一个方法来询问买啥;

 public void AskBuy()
        {
            Console.WriteLine("欢迎光临,请问您需要什么");
            Console.WriteLine("我们这里有SamSung,Acer,Banana,JiangYou");
            string type = Console.ReadLine();
            Console.WriteLine("请问您需要多少");
            int count = Convert.ToInt32(Console.ReadLine());
            //从仓库取货物
            ProductFather[] pro= ck.QuHuo(type,count);
            //计算价钱
            double max = GetMoney(pro);
            Console.WriteLine("您总共应付{0}元", max);
            Console.WriteLine("请选择你的打折方案 1.不打折 2.打九折 3.打八五折 4.买300送50");
            string input = Console.ReadLine();
            DaZhe da = GetDaZhe(input);
            double endmoney = da.GetTolalMoney(max);
            Console.WriteLine("打完折您应该付{0}", endmoney);
        }

再写一个得到价钱的方法

  public double GetMoney(ProductFather[] pro)
        {
            double max = 0;
            for (int i = 0; i < pro.Length; i++)
            {
                max += pro[i].Price;

            }
            return max;
        }

因为要打折所以我们这里转去写打折类;

四种打折方法:不打折,几几折,满减折

打折类:

这里用到虚方法:

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

namespace 超市收银系统
{
    abstract class DaZhe
    {
        /// <summary>
        /// 计算打折后应付的钱
        /// </summary>
        /// <param name="max">打折前的钱</param>
        /// <returns>打折后的钱</returns>
        public abstract double GetTolalMoney(double max);
    }
}

不打折

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

namespace 超市收银系统
{
    /// <summary>
    /// 不打折
    /// </summary>
    internal class BuDaZhe : DaZhe
    {
        public override double GetTolalMoney(double max)
        {
            return max;
        }
    }
}

几几折

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

namespace 超市收银系统
{
    /// <summary>
    /// 按折扣打折
    /// </summary>
    internal class LvZhe : DaZhe
    {
        public LvZhe(double rate)
        {
            this.Rate = rate;
        }

        public double Rate
        {
            get;set;
        }
        public override double GetTolalMoney(double max)
        {
            return max * Rate;
        }
    }
}

满减折

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

namespace 超市收银系统
{
    internal class ManSongZhe : DaZhe//满送折
    {
        public double M
        {
            set; get;
        }
        public double N
        {
            set; get;
        }

        public ManSongZhe(double m, double n)
        {
            this.M = m;
            this.N = n;
        }

        public override double GetTolalMoney(double max)
        {
            if (max >= this.M)
            {
                return max - ((int)(max / (this.M)) )* this.N;
            }
            else
                return max;
        }
    }
}

打折类结束回答超市类;

写一个得到选择折扣的方法

 public DaZhe GetDaZhe(string input)
        {
            DaZhe da = null;
            switch (input)
            {
                case "1":da = new BuDaZhe();
                    break;
                case "2":da = new LvZhe(0.9);
                    break;
                case "3":da = new LvZhe(0.85);
                    break;
                case "4":da = new ManSongZhe(300,50);
                    break;
                default:
                    break;
            }
            return da;
        } 

 

 最后写一个展示货物的方法,不过调用的是在仓库类的时候写的方法;

  public void Showpro()
        {
            ck.ShowPro();
        }

以下为超市类的代码

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

namespace 超市收银系统
{
    internal class SuperMarket
    {
        CangKu ck = new CangKu();
        /// <summary>
        /// 创建超市对象的时候,向仓库添加货架
        /// </summary>
        public SuperMarket()
        {
            ck.JinPro("SamSung", 100);
            ck.JinPro("Acer", 100);
            ck.JinPro("Banana", 100);
            ck.JinPro("JiangYou", 100);
        }   
        public void AskBuy()
        {
            Console.WriteLine("欢迎光临,请问您需要什么");
            Console.WriteLine("我们这里有SamSung,Acer,Banana,JiangYou");
            string type = Console.ReadLine();
            Console.WriteLine("请问您需要多少");
            int count = Convert.ToInt32(Console.ReadLine());
            //从仓库取货物
            ProductFather[] pro= ck.QuHuo(type,count);
            //计算价钱
            double max = GetMoney(pro);
            Console.WriteLine("您总共应付{0}元", max);
            Console.WriteLine("请选择你的打折方案 1.不打折 2.打九折 3.打八五折 4.买300送50");
            string input = Console.ReadLine();
            DaZhe da = GetDaZhe(input);
            double endmoney = da.GetTolalMoney(max);
            Console.WriteLine("打完折您应该付{0}", endmoney);
        }
        public DaZhe GetDaZhe(string input)
        {
            DaZhe da = null;
            switch (input)
            {
                case "1":da = new BuDaZhe();
                    break;
                case "2":da = new LvZhe(0.9);
                    break;
                case "3":da = new LvZhe(0.85);
                    break;
                case "4":da = new ManSongZhe(300,50);
                    break;
                default:
                    break;
            }
            return da;
        }
        public double GetMoney(ProductFather[] pro)
        {
            double max = 0;
            for (int i = 0; i < pro.Length; i++)
            {
                max += pro[i].Price;

            }
            return max;
        }
        public void Showpro()
        {
            ck.ShowPro();
        }
    }
}

最后的最后主函数

namespace 超市收银系统
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SuperMarket sp = new SuperMarket();
            //展现货物
            sp.Showpro();
            //与用户交互
            sp.AskBuy();

        }
    }
}

 

 结束

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值