C# supermarket cashier system - summary of object-oriented learning

The mind map is shown in the figure:

 

1. Commodity category (parent category)

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

namespace 超市收银系统
{
    class ProductFather
    {
        public double Price
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }
        public string ID
        {
            get;
            set;
        }
        //构造函数
        public ProductFather(string id,double price,string name)
        {
            this.ID = id;
            this.Price = price;
            this.Name = name;
        }
    }
}

 2. Subcategory (Acer notebook, soy sauce, banana, Samsung notebook)

(i) Samsung Notebook

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

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

        }
    }
}

(ii) Soy sauce

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

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

        }
    }
}

(iii) Bananas

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

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

        }
    }
}

(IV) Acer Notebook

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

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

        }
    }
}

3. Warehouse

The List<List<ProductFather>> here indicates that a new shelf in the warehouse has been created

Guid.NewGuid().ToString() can get a string of non-repeating sequence codes

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

namespace 超市收银系统
{
    class CangKu
    {
        /*存储货物*/

        //这样写较为麻烦
        //List<SamSung> listSam = new List<SamSung>();
        //List<Acer> listAcer = new List<Acer>();
        //List<JiangYou> listJiangYou = new List<JiangYou>();
        //List<Banana> listBanana = new List<Banana>();

        //一层list表示的是仓库,二层list表示的是仓库里的货架
        List<List<ProductFather>> list = new List<List<ProductFather>>();



        //list[0]存储Acer电脑
        //list[1]存储三星手机
        //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>
        public void ShowPros()
        {
            foreach (var item in list)
            {
                Console.WriteLine("我们超市有:" + item[0].Name + "\t" + "有" + item.Count + "个\t" + "每个" + item[0].Price + "元");
            }
        }

        /// <summary>
        /// 进货
        /// </summary>
        /// <param name="strType">货物的类型</param>
        /// <param name="count">货物的数量</param>
        public void importPros(string strType,int count)
        {
            for (int i = 0; i < count; i++)
            {
                switch (strType)
                {
                    case "Acer":list[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏碁笔记本"));
                        break;
                    case "SamSung":list[1].Add(new SamSung(Guid.NewGuid().ToString(), 2000, "棒子的手机"));
                        break;
                    case "JiangYou":list[2].Add(new JiangYou(Guid.NewGuid().ToString(), 10, "老抽"));
                        break;
                    case "Banana":list[3].Add(new Banana(Guid.NewGuid().ToString(), 50, "香蕉"));
                        break;
                }
            }
        }

        /// <summary>
        /// 从仓库中提取货物
        /// </summary>
        /// <param name="strType">货物的种类</param>
        /// <param name="count">货物的数量</param>
        /// <returns></returns>
        public ProductFather[] OutportPros(string strType, int count)
        {
            ProductFather[] pros = new ProductFather[count];
            for (int i = 0; i < pros.Length; i++)
            {
                switch (strType)
                {
                    case "Acer":
                        if (list[0].Count != 0) 
                        {
                            pros[i] = list[0][0];
                            list[0].RemoveAt(0);
                        }
                        break;
                    case "SamSung":
                        if (list[1].Count != 0)
                        {
                            pros[i] = list[1][0];
                            list[1].RemoveAt(0);
                        }
                        break;
                    case "JiangYou":
                        if (list[2].Count != 0)
                        {
                            pros[i] = list[2][0];
                            list[2].RemoveAt(0);
                        }
                        break;
                    case "Banana":
                        if (list[3].Count != 0)
                        {
                            pros[i] = list[3][0];
                            list[3].RemoveAt(0);
                        }
                        break;
                }
            }
            return pros;

        }
    }
}

4. Discount class (abstract parent class)

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

namespace 超市收银系统
{
    /// <summary>
    /// 打折的父类
    /// </summary>
    abstract class CalFather
    {
        /// <summary>
        /// 计算打折后应付多少钱
        /// </summary>
        /// <param name="realMoney">打折前应付的价钱</param>
        /// <returns>打折后应付的钱</returns>
        public abstract double GetTotalMoney(double realMoney);
    }
}

(i) No discount (inherit the abstract parent class, rewrite the discounted price)

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

namespace 超市收银系统
{
    /// <summary>
    /// 不打折 该多少钱就多少钱
    /// </summary>
    class CalNormal:CalFather
    {
        public override double GetTotalMoney(double realMoney)
        {
            return realMoney;
        }
    }
}

(ii) Discount (inherit the abstract parent class, rewrite the discounted price)

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

namespace 超市收银系统
{
    /// <summary>
    /// 按折扣率打折
    /// </summary>
    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;
        }
    }
}

(iii) Minus N yuan for full M yuan (inherit the abstract parent class, rewrite the discounted price)

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

namespace 超市收银系统
{
    class CalMN:CalFather
    {
        //买500送100
        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; //realMoney/this.M意思为 买M送N,如果买2M则送2N
            }
            else
            {
                return realMoney;
            }
        }
    }
}

4. Supermarket cash register

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

namespace 超市收银系统
{
    class SupperMarket
    {
        CangKu ck = new CangKu();

        /// <summary>
        /// 创建超市对象的时候,给仓库的货架上导入货物
        /// </summary>
        public SupperMarket()
        {
            ck.importPros("SamSung", 100);
            ck.importPros("JiangYou", 50);
            ck.importPros("Banana", 10);
            ck.importPros("Acer", 6);
        }

        /// <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.OutportPros(strType, count);
            //下面该计算价钱了
            double realMoney = GetMoney(pros);
            Console.WriteLine("您总共应付{0}元", realMoney);
            Console.WriteLine("请选择您的打折方式 1——不打折 2——打9折 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 ShowPros()
        {
            ck.ShowPros();
        }
    }
}

5. The main program runs

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

namespace 超市收银系统
{
    class Program
    {
        static void Main(string[] args)
        {
            SupperMarket sm = new SupperMarket();
            //展示货物
            sm.ShowPros();
            //跟用户交互
            sm.AskBuying();
        }
    }
}

operation result

Learning tutorial: [01_C# Beginner to Master] Strongly recommended for novices: C# development course, a complete set of courses_哔哩哔哩_bilibili

The whole case is very suitable for summarizing object-oriented learning, and this programming method is relatively scalable.

At present, it only supports purchasing one product at a time, and the function of purchasing multiple products at one time can be expanded in the future.

 

 

Guess you like

Origin blog.csdn.net/weixin_46711336/article/details/125832500