超市系统

生成一个GUID码,是全世界独一无二的码

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

namespace _GUID
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.WriteLine(Guid.NewGuid().ToString());
            Console.ReadKey();
        }
    }
}
View Code

 先创建一个商品的父类,包含商品的属性:价格、名称、编码

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

namespace SuperMarketSystem
{
    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;
            
        }
    }
}
View Code

再创建对应的子类商品,我们这用了一个宏基电脑、酱油、香蕉、三星手机作为4个商品子类继承于商品父类。这个项目并不是实战意义上的超市管理系统,只是形式上的超市管理系统,然后用于复习多态的知识、简单工厂设计模式

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

namespace SuperMarketSystem
{
    class Acer : ProductFather
    {
        public Acer(string id, double price, string name)
            : base(id, price, name)
        {

        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMarketSystem
{
    class Banana:ProductFather 
    {
        public Banana(string id, double price, string name)
            : base(id, price, name)
        {

        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMarketSystem
{
    class JiangYou:ProductFather 
    {
        public JiangYou (string id,double price,string name)
            :base(id,price,name)
        {

        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMarketSystem
{
    class SamSung : ProductFather
    {
        public SamSung(string id, double price, string name) 
            :base(id, price, name )
        {

        }
    }
}
View Code

四个模拟的商品子类创建好之后,开始创建仓库负责入库。仓库的三个功能:存货、取货、展示库存数据

这里我们存的是上面创建的商品,我们用商品的父类来创建对象,因为不知道存的是哪一种商品。建立一个商品的集合代表存储的货物。但是我们要将存储的货物进行基本分类,所以就是在集合里面再加一层集合,在整体上再进行分类。用List<List<ProductFather>> list = new List<List<ProductFather>>();来创建一个仓库的存储货物的集合,像是一个二维的库。在在这个集合里创建4个集合,代表我们使用的四种商品,相当于加入了四个货架。我们再往这四个货架里分别放不同的东西。

第一步存货,传入要存的商品名称跟数量,通过循环存入对应的货架里

第二部展示,展示出仓库里所包含的东西,通过foreach循环遍历键值对集合,把仓库里的商品属性展示出来

第三步取货,将客人要买的东西取出来,需要提供名字跟数量传入方法中,取出一个里面就去掉一个

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

namespace SuperMarketSystem
{
    class WareHouse
    {
        //存储货物
        List<List<ProductFather>> list = new List<List<ProductFather>>();



        /// <summary>
        /// 在创建仓库对象的时候,向仓库里添加货架
        /// 比如说
        /// list[0]存储电脑
        /// list[1]手机
        /// list[2]酱油
        /// list[3]香蕉
        /// </summary>
        public WareHouse()
        {
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
            list.Add(new List<ProductFather>());
        }
        /// <summary>
        /// 进货
        /// </summary>
        /// <param name="strType">货物类型</param>
        /// <param name="count">货物的数量</param>
        public void SetPros(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(), 500, "三星手机"));
                        break;
                    case "Banana":
                        list[2].Add(new Banana(Guid.NewGuid().ToString(), 50,"香蕉"));
                        break;
                    case "JiangYou":
                        list[3].Add(new JiangYou(Guid.NewGuid().ToString(), 10, "酱油"));
                        break;
                }
            }
        }
        public void ShowPros()
        {
            foreach (var item in list)
            {
                Console.WriteLine("我们仓库有:" + item[0].Name + "," + "\t" + item.Count + "" + "\t每个" + item[0].Price + "");
            }
            Console.WriteLine("请输入 Acer Banana JiangYou Samsung中的一个");
        }
        /// <summary>
        /// 取货
        /// </summary>
        /// <param name="strType">货物类型</param>
        /// <param name="count">货物的数量</param>
        public ProductFather[] GetPros(string strType, int count)
        {
            ProductFather[] pros = new ProductFather[count];
            for (int i = 0; i < pros.Length; i++)
            {
                switch (strType)
                {
                    case "Acer":
                        pros[i] = list[0][0];
                        list[0].RemoveAt(0);
                        break;
                    case "Samsung":
                        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;
        }
    }
}
View Code

 创建一个超市类,负责跟用户沟通,开始进行交互

首先要先往仓库里存货,输入要存的名字跟数量

然后进行交互,买卖

算钱,算钱里面加个折扣系统

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

namespace SuperMarketSystem
{
    abstract  class CutFather
    {
        /// <summary>
        /// 计算打折后应该多少钱
        /// </summary>
        /// <param name="AllMoney">打折钱应付的价钱</param>
        /// <returns>返回打折后应付的价钱</returns>
        public abstract double GetCutMoney(double AllMoney);
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMarketSystem
{
    class CutMS : CutFather
    {
        /// <summary>
        /// 买500 送100
        /// </summary>
        /// <param name="AllMoney"></param>
        /// <returns></returns>
        public double M
        {
            get;
            set;
        }
        public double N
        {
            get;
            set;
        }
        public CutMS(double m,double n)
        {
            this.M = m;
            this.N = n;
        }
        public override double GetCutMoney(double AllMoney)
        {
            if(AllMoney >=this.M)
            {
                return AllMoney - (int)(AllMoney / M) * N;
            }
            else
            {
                return AllMoney;
            }
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMarketSystem
{
    /// <summary>
    /// 不打折
    /// </summary>
    class CutNormal : CutFather
    {
        public override double GetCutMoney(double AllMoney)
        {
            return AllMoney;
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMarketSystem
{
    class CutRate : CutFather
    {
        /// <summary>
        /// 按照折扣率打折
        /// </summary>
        /// <param name="AllMoney"></param>
        /// <returns></returns>
        public double Rate
        {
            get;
            set;
        }
        public CutRate (double rate)
        {
            this.Rate = rate;
        }
        public override double GetCutMoney(double AllMoney)
        {
            return AllMoney *this.Rate ;
        }
    }
}
View Code

 

结完打完折后的钱给出小票,这个小票显示所购买的信息包括对应商品的Guid

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

namespace SuperMarketSystem
{
    class SuperMarket
    {
        WareHouse wh = new WareHouse();

        /// <summary>
        /// 创建超市对象的时候,给仓库的货架上导入货物
        /// </summary>
        public SuperMarket()
        {
            wh.SetPros("Acer", 10000);
            wh.SetPros("JiangYou", 10001);
            wh.SetPros("Samsung", 10002);
            wh.SetPros("Banana", 10003);
        }
        public void AskAndBuy()
        {
            Console.WriteLine("你好,请问要买什么东西?");
            string strType = Console.ReadLine();
            Console.WriteLine("你要买多少?");
            int count = Convert.ToInt32(Console.ReadLine());
            //去仓库取货
            ProductFather[] pros = wh.GetPros(strType, count);
            double AllMoney = GetMoney(pros);
            Console.WriteLine("您总共应该付{0}元", AllMoney);
            Console.WriteLine("请选择您的打折方式 1---不打折 2---打九折 3---打85折 4---满500减100");
            string input = Console.ReadLine();
            //通过简单工厂设计模式根据用户的输入获得一个打折对象
            CutFather cut = GetCut(input);
            double realMoney = cut.GetCutMoney(AllMoney);
            Console.WriteLine("打完折后你应该付的钱是{0}", realMoney);
            Console.WriteLine("以下是您的购物信息");
            XiaoPiao(pros);


            Console.ReadKey();
        }
        //这里把写小票的功能加进了一个方法 以便于以后买多种商品的时候可以叠加使用这个出小票的方法
        public void XiaoPiao(ProductFather[] pro)
        {
            ProductFather[] pros = pro;
            foreach (var item in pros)
            {
                Console.WriteLine("货物名称:" + item.Name + "\t" + "价格:" + item.Price + "\t" + "货物编号" + item.ID);
            }
        }

        /// <summary>
        /// 简单工厂模式,根据用户输入,返回一个对象
        /// </summary>
        /// <param name="input">用户选择的折扣</param>
        /// <returns>返回的是父类对象 但是里面装的是子类对象</returns>
        public CutFather GetCut(string input)
        {
            CutFather cut = null;
            switch (input)
            {
                case "1":
                    cut = new CutNormal();
                    break;
                case "2":
                    cut = new CutRate(0.9);
                    break;
                case "3":
                    cut = new CutRate(0.85);
                    break;
                case "4":
                    cut = new CutMS(500, 100);
                    break;
            }
            return cut;
        }

        /// <summary>
        /// 根据用户买的货物计算总价钱
        /// </summary>
        /// <param name="pros">要买的货物的集合</param>
        /// <returns>总共要付的钱</returns>
        public double GetMoney(ProductFather[] pros)
        {
            double AllMoney = 0;
            for (int i = 0; i < pros.Length; i++)
            {
                AllMoney += pros[i].Price;
            }
            return AllMoney;
        }
        public void ShowProducts()
        {
            wh.ShowPros();
        }
    }
}
View Code

最后主程序里面调用各种方法

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

namespace SuperMarketSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建超市对象,第一步展示超市货物
            SuperMarket sp = new SuperMarket();
            sp.ShowProducts();
            //用户交互,进行买卖
            sp.AskAndBuy();
            Console.ReadKey();

        }
    }
}
View Code

 

posted @ 2021-08-11 22:28  静态类  阅读(186)  评论(0编辑  收藏  举报