c#练习之超市收银系统

来源:互联网 发布:乐高ev3编程怎么导出 编辑:程序博客网 时间:2024/05/01 07:39

类的设计

第一种类:商品的类,父类是个抽象类;
第二种类:折扣的类,父类也是个抽象类
类图如下;
商品类
折扣类

使用技能

  1. 用继承抽象类实现多态,用多态来实现工厂模式;
  2. 使用反射机制来实现构造实例的多态和工厂模式;
  3. 工厂模式可以增加系统的可扩展性;
  4. 使用Dictionary中的list集合来减少代码量;

源代码

仓库类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//本程序的优点,当增加货物类型的时候,不需要修改局部代码,只需要调用AddShelf增加一个货架;添加货物的时候根据货物名添加对象;而在添加货物的时候使用到了反射机制;namespace _33_超市收银系统{    class Cangku    {        //在仓库中存货物,需要满足:分类整理的需求,所以需要货架;        public Dictionary<string, List<ProductFather>> list = new Dictionary<string, List<ProductFather>>();        //可以操作的对象货架,货物,操作方式:增删;        /// <summary>        /// 为仓库添加一个货架;        /// </summary>        /// <param name="name">要添加的货架名</param>        /// <returns>如果货架已经存在返回添加失败,否则添加货架成功</returns>        public bool AddShelf(string name)        {            if (list.ContainsKey(name))                return false;            list[name]=new List<ProductFather>();            return true;        }        /// <summary>        /// 删除一个货架;        /// </summary>        /// <param name="name">要删除的货架名</param>        /// <param name="reason">成功则值为成功删除,失败是返回失败原因包括货架还有货物的失败和不存在该货架的失败</param>        /// <returns>表明是否成功删除</returns>        public bool DelShelf(string name,out string reason)        {            if (list.ContainsKey(name))            {                if (list[name].Count==0)                {                    list.Remove(name);                    reason = "成功删除";                    return true;                }                else                {                    reason = "货架还有货物";                    return false;                }            }            else            {                reason = "不存在该货架";                return false;            }        }        /// <summary>        /// 向货架中添加count个proType类型的货物;        /// </summary>        /// <param name="proType">货物类型,通过这个字符串的反射找到对象的构造函数,实现工厂模式</param>        /// <param name="count">需要添加的货物数量</param>        /// <param name="price">货物的价格</param>        /// <returns></returns>        public bool AddProducts(string proType,int count,double price)        {            if (!list.ContainsKey(proType))            {                return false;            }            string str = "_33_超市收银系统."+proType;            for (int i = 0; i < count; i++)            {                //这里用到反射机制哦;                list[proType].Add((ProductFather)Activator.CreateInstance(System.Type.GetType(str), price, proType, Guid.NewGuid().ToString()));            }            return true;        }        /// <summary>        /// 从仓库中取出一些商品;        /// </summary>        /// <param name="proType">要取出的商品的类型</param>        /// <param name="reqCount">需要去除的量</param>        /// <param name="getCount">实际去除的量</param>        /// <returns>返回去除的商品数组</returns>        public ProductFather[] DelProducts(string proType,int reqCount,out int getCount)        {            ProductFather[] pros = new ProductFather[reqCount];//运行时确定大小的数组;            if ( !list.ContainsKey(proType) )//不存在这个货物的货架;            {                getCount = 0;                return null;            }            for (int i = 0; i < reqCount; i++)            {                if (list[proType].Count==0)//如果货物已经取完了;                {                    getCount = i;                    return pros;                }                pros[i] = list[proType][0];                list[proType].RemoveAt(0);            }            getCount = reqCount;            return pros;        }        /// <summary>        /// 获取仓库中的商品信息        /// </summary>        /// <returns>返回格式化字符串数组,一个商品一个字符串,可以在外面通过相同的协议进行解析</returns>        public string [] GetInfo()        {            //使用格式化字符串储存货架信息;            string [] info = new string[list.Count];            int i=0;            foreach (var item in list.Keys)            {                info[i] = item.ToString();                info[i] += "|" + list[item].Count;                info[i] += "|" + list[item][0].Price;            }            return info;        }    }}

商品类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _33_超市收银系统{    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;        }    }}
1 0