简单的商品操作

namespace 简单的商品操作
{
    class Program
    {
        static void Main(string[] args)
        {
            Operation op = new Operation();
            op.InitM();
        }
    }
    //创建一个操作类
    class Operation
    {
        private List<Good> gList;
        //初始化
        public void InitM()
        {
            gList = new List<Good>();
            gList.Add(new Good(10001, "<商品名称>", 0, 0.0));
            gList.Add(new Good(10002, "ipad", 20, 320.0));
            gList.Add(new Good(10003, "iphone", 30, 120.0));
            gList.Add(new Good(10004, "Mac", 40, 20.0));
            OperationM();
        }
        //操作界面
        public void OperationM()
        {
            while (true)
            {
                InformationM("0:添加商品\n1:删除商品\n2:修改商品\n3:查询商品\ne:退出系统\n请输入:");
                int choose = 4;//选择的序号
                string s = Console.ReadLine();
                if (s.Equals("e")) break;
                if (int.TryParse(s, out choose))
                {
                    switch (choose)
                    {
                        //调用添加商品
                        case 0: InformationM("----------添加商品----------");
                            while (true)
                            {
                                InsertM();
                                InformationM("是否继续添加Y/N?");
                                if (!UnBreakM()) break;
                            }
                            break;
                        //调用删除商品
                        case 1: InformationM("----------删除商品----------");
                            while (true)
                            {
                                DeleteM();
                                InformationM("是否继续删除Y/N?");
                                if (!UnBreakM()) break;
                            }
                            break;
                        //调用修改商品
                        case 2: InformationM("----------修改商品----------");
                            while (true)
                            {
                                ModificationM();
                                InformationM("是否继续修改Y/N?");
                                if (!UnBreakM()) break;
                            }
                            break;
                        //调用查询商品
                        case 3: InformationM("----------查询商品----------");
                            while (true)
                            {
                                FindM();
                                InformationM("是否继续查询Y/N?");
                                if (!UnBreakM()) break;
                            }
                            break;
                        default: InformationM("提示!没有此类选项!"); break;


                    }
                }
                else
                {
                    InformationM("提示!没有此类选项!");
                }
            }
        }
        //添加元素
        public void InsertM()
        {
                Good g = gList[gList.Count-1];//获取最后一个商品的id
                int id = g.id + 1;
                InformationM("请输入商品名称:");
                string name = Console.ReadLine();
                int count = 0;
                double price = 0.0;
                InformationM("请输入商品数量:");
                while (true)
                {
                    if (int.TryParse(Console.ReadLine(), out count))
                    {
                        break;
                    }
                    else
                    {
                        InformationM("输入的数量不正确,请重新输入");
                    }
                }
                InformationM("请输入商品单价");
                while (true)
                {
                    if (double.TryParse(Console.ReadLine(), out price))
                    {
                        break;
                    }
                    else
                    {
                        InformationM("输入的单价不正确,请重新输入");
                    }
                }
                gList.Add(new Good(id, name, count, price));
        }
        //删除元素
        public void DeleteM() {
                InformationM("请输入你要删除的商品编号,输入All则全部删除");
                string s = Console.ReadLine();
                s = s.ToUpper();
                int id = 0;
                int countBL = 0;//遍历的次数
                if (s.Equals("ALL"))
                {
                    gList.Clear();
                }else if (int.TryParse(s, out id))
                {
                    foreach (Good g in gList)
                    {
                        if (g.id == id)
                        {
                            Console.WriteLine("商品编号:{0};商品名称:{1};商品数量:{2};商品单价:{3};-->此条信息已经删除", g.id, g.name, g.count, g.price);
                            gList.Remove(g);
                            break;
                        }
                        else
                        {
                            countBL++;
                        }
                    }
                    if (countBL == gList.Count) {
                        InformationM("未检测到此商品编号");
                    }
                }
                else
                {
                    InformationM("ID输入有误");
                }
        }       
        //修改元素
        public void ModificationM()
        {
                InformationM("输入商品编号修改商品信息:");
                string s = Console.ReadLine();
                int id = 0;
                int countBL = 0;
                if (int.TryParse(s, out id))
                {
                    foreach (Good g in gList)
                    {
                        if (g.id == id)
                        {
                            while (true)
                            {
                                InformationM("修改商品数量");
                                int count = 0;
                                if (int.TryParse(Console.ReadLine(), out count))
                                {
                                    g.count = count;
                                    break;
                                }
                                else
                                {
                                    InformationM("输入有误!请重新输入");
                                }
                            }
                            while (true)
                            {
                                InformationM("修改商品单价");
                                double price = 0;
                                if (double.TryParse(Console.ReadLine(), out price))
                                {
                                    g.price = price;
                                    break;
                                }
                                else
                                {
                                    InformationM("输入有误!请重新输入");
                                }
                            }
                            Console.WriteLine("修改后的商品信息\n商品编号:{0};商品名称:{1};商品数量:{2};商品单价:{3};", g.id, g.name, g.count, g.price);
                        }
                        else
                        {
                            countBL++;
                        }
                    }
                    if (countBL == gList.Count)
                    {
                        InformationM("未检测到此编号商品信息");
                    }
                }
                else
                {
                    InformationM("提示!你输入的商品编号有误!");
                }
        }
        //查询元素
        public void FindM()
        {
                InformationM("输入商品编号查询单条信息\n输入All显示全部信息:");
                string s = Console.ReadLine();
                s = s.ToUpper();
                int id = 0;
                int countBL = 0;
                if (s.Equals("ALL"))
                {
                    foreach (Good g in gList)
                    {
                        Console.WriteLine("商品编号:{0};商品名称:{1};商品数量:{2};商品单价:{3};", g.id, g.name, g.count, g.price);
                    }
                }else if (int.TryParse(s, out id))
                {
                    foreach (Good g in gList)
                    {
                        if (g.id == id)
                        {
                            Console.WriteLine("商品编号:{0};商品名称:{1};商品数量:{2};商品单价:{3};", g.id, g.name, g.count, g.price);
                        }
                        else {
                            countBL++;
                        }
                    }
                    if (countBL == gList.Count)
                    {
                        InformationM("未查询到此编号商品信息");
                    }
                }
                else
                {
                    InformationM("提示!你输入的商品编号有误!");
                }
        }
        //跳出循环方法
        public bool UnBreakM()
        {
            while (true)
            {
                string s = Console.ReadLine();
                s = s.ToUpper();
                if (s.Equals("Y"))
                {
                    return true;
                }
                else if (s.Equals("N"))
                {
                    return false;
                }
                else
                {
                    InformationM("请输入Y or N");
                }
            }
        }
        //输入ID
        public int InputID() { 
            int id = 0;
            if (int.TryParse(Console.ReadLine(), out id))
            {
            }
            else {
                InformationM("输入错误");
            }
            return id;
        }
        //打印提示信息
        public void InformationM(string s){
            Console.WriteLine(s);
        }
    }
    //创建一个商品类
    class Good
    {
        public int id;
        public string name;
        public int count;
        public double price;
        public Good(int id, string name, int count, double price)
        {
            this.id = id;
            this.name = name;
            this.count = count;
            this.price = price;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值