day05 c#简单的购物系统(day04忘了写了就不写了。。。)

    public class Goods
    {
        public string name;
        public float price;
        public string type;
        public string information;
        public Goods(string name, float price, string type, string information)
        {
            this.name = name;
            this.price = price;
            this.type = type;
            this.information = information;
        }
    }
    public class Seller
    {
        #region
        public string name;
        public int goodsNumber;
        public void Show(Goods goods)
        {
            if (goodsNumber < 5)
            {
                Console.WriteLine("填写商品名称:");
                string name = Console.ReadLine();
                Console.WriteLine("填写商品价格:");
                float price = float.Parse(Console.ReadLine());
                Console.WriteLine("填写商品类型:");
                string type = Console.ReadLine();
                Console.WriteLine("填写商品描述:");
                string information = Console.ReadLine();
                goods = new Goods(name,price,type,information);
                goodsNumber += 1;
            }
            else 
            {
                Console.WriteLine("商场放不下了!!!");
                Console.WriteLine("/黑人?????/");
            }
        }
        public void ChangeGoods(Goods[] goods,string str)
        {
            for (int i = 0; i < goods.Length; i++)
            {
                if (goods[i].name.Contains(str))
                {
                    Console.Write("商品名称:");
                    Console.WriteLine(goods[i].name);
                    Console.Write("商品价格:");
                    Console.WriteLine(goods[i].price);
                    Console.Write("商品类型:");
                    Console.WriteLine(goods[i].type);
                    Console.Write("商品描述:");
                    Console.WriteLine(goods[i].information);
                }
            }

        }

        #endregion
    }

    public class Buyer
    {
        public string name;
        public float money;
        public ShoppingCar shoppingCar;
        int i = 0;
        public void AddShoppingCar(Buyer buyer,string str,ShoppingCar shoppingCar)
        {
            shoppingCar.buyerName = buyer.name;

            for (int i = 0; i < shoppingCar.goodsName.Length; i++)
            {
                if (shoppingCar.goodsName[i] == str)
                {
                    Console.WriteLine("已经在购物车了老弟,你没法买更多这个物品了!!");
                    return;
                }
            }
            if (i < shoppingCar.goodsName.Length)
            {
                shoppingCar.goodsName[i] = str;
                i++;
                return;
            }
        }
        public void EndBuy(Buyer buyer,Goods[] goods)
        {
            buyer.shoppingCar.EndBuy(buyer,goods);
        }
    }
    public class TaoBao 
    {
        public int[] seekList;
        public void SeekGoods(string str,Goods[] goods)
        {
            int i = 0;
            seekList = new int[goods.Length];
            while ( i < goods.Length)
            {
                
                if (goods[i].name.Contains(str))
                {
                    for (int a = 0; a < seekList.Length; a++)
                    {
                        if (seekList[a] == 0)
                        {
                            seekList[a] = i+1;
                        }
                    }
                    Console.Write("商品名称:");
                    Console.WriteLine(goods[i].name); 
                    Console.Write("商品价格:");
                    Console.WriteLine(goods[i].price);
                    Console.Write("商品类型:");
                    Console.WriteLine(goods[i].type); 
                    Console.Write("商品描述:");
                    Console.WriteLine(goods[i].information);
                    break;
                }
                i++;
            }
        }
        public void ShowGoods(string str, Goods[] goods)
        {
            for (int i = 0; i < goods.Length; i++)
            {
                if (goods[i].name != null)
                {
                    Console.Write("商品名称:");
                    Console.WriteLine(goods[i].name);
                    Console.Write("商品价格:");
                    Console.WriteLine(goods[i].price);
                    Console.Write("商品类型:");
                    Console.WriteLine(goods[i].type);
                    Console.Write("商品描述:");
                    Console.WriteLine(goods[i].information);
                }
            }
        }
    }
    public class ShoppingCar
    {
        public string[] goodsName=new string[5];
        public string buyerName;
        public void ShowGoods(Goods[] goods)
        {        
            for (int i = 0; i < goodsName.Length; i++)
            {
                for (int a = 0; a < goods.Length; a++)
                {
                    if (goodsName[i] == goods[a].name)
                    {
                        Console.Write("商品名称:");
                        Console.WriteLine(goods[a].name);
                        Console.Write("商品价格:");
                        Console.WriteLine(goods[a].price);
                        Console.Write("商品类型:");
                        Console.WriteLine(goods[a].type);
                        Console.Write("商品描述:");
                        Console.WriteLine(goods[a].information);
                        Console.WriteLine("------------------------");
                    }
                }
            }
        }
        public void EndBuy(Buyer buyer,Goods[] goods)
        {
            for (int i = 0; i < goodsName.Length; i++)
            {
                for (int a = 0; a < goods.Length; a++)
                {
                    while (goods[a] == null)
                    {
                        a += 1;
                    }
                    if (goodsName[i] == goods[a].name)
                    {
                        if (buyer.money - goods[a].price < 0)
                        {
                            Console.WriteLine("你的钱不够了老弟");
                        }
                        else 
                        {
                            buyer.money -= goods[a].price;
                            Console.WriteLine("你购买了" + goods[a].name);
                            goods[a] = null;
                            break;
                        }
                        
                    }
                }
                    
            }
            Console.WriteLine("本次购物结束,剩余"+buyer.money+"元钱");
        }
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            //Tool tool = new Tool ();
            //tool.Sheep(2);
            //Console.WriteLine("请输入想知道的斐波那契的第几个数字");
            //tool.count = int.Parse(Console.ReadLine())-1;
            //tool.fib() ; 
            bool start = true;
            Seller seller = new Seller();
            Goods[] goods = new Goods[5];
            //Goods[] goods1 = new Goods[5];
            //for (int i = 0; i < goods.Length; i++)
            //{
            //    seller.Show(goods1[i]);
            //}
            goods[0] = new Goods("手机", 1000f, " "," ");
            goods[1] = new Goods("手1机", 1000f, " ", " ");
            goods[2] = new Goods("手2机", 1000f, " ", " ");
            goods[3] = new Goods("手3机", 1000f, " ", " ");
            goods[4] = new Goods("手4机", 1000f, " ", " ");
            Console.WriteLine("商品有");
            for (int i = 0; i < goods.Length; i++)
            {
                Console.WriteLine(goods[i].name);
            }

            Buyer buyer = new Buyer();
            Console.WriteLine("今天带了多少钱呢?");
            buyer.money = float.Parse(Console.ReadLine());


            TaoBao taoBao = new TaoBao();
            ShoppingCar shoppingCar = new ShoppingCar();
            buyer.shoppingCar = shoppingCar;
            
            while (start)
            {
                Console.WriteLine("请输入命令:");
                Console.WriteLine("0-返回菜单,1-搜索,2=加入购物车,3-清空购物车(结账),4-查看购物车,5-退出程序");
                switch (Console.ReadLine())
                {
                    case "0":
                        break ;
                    case "1":
                        Console.Clear();
                        Console.WriteLine("请输入关键词");
                        taoBao.SeekGoods(Console.ReadLine( ),goods);
                        break;
                    case "2":
                        Console.Clear();
                        Console.WriteLine("把找到的全部加入购物车?");
                        Console.WriteLine("1-Yes;2-No;3-选择部分");
                        switch (Console.ReadLine())
                        {

                            case "1":
                                Console.Clear();
                                int m = 0;
                                if ( m < taoBao.seekList.Length)
                                {
                                    buyer.AddShoppingCar(buyer, goods[taoBao.seekList[m]-1].name, shoppingCar);
                                    m++;
                                }
                                break;
                            case "2":
                                break;
                            case "3":
                                Console.Clear();
                                Console.WriteLine("要加入购物车的名字");
                                Console.WriteLine("要加入购物车的型号");
                                for (int i = 0;i<goods.Length+1;i++) 
                                {
                                    if (i == goods.Length)
                                    {
                                        Console.WriteLine("输入错误信息,请重新开始");
                                        break;
                                    }
                                    if (goods[i].name == Console.ReadLine() && goods[i].type == Console.ReadLine())
                                    {
                                        buyer.AddShoppingCar(buyer, goods[i].name, shoppingCar);
                                    }
                                    
                                }
                                break;
                            case "5":
                                return;
                            default:
                                Console.WriteLine("输入错误信息,请重新开始");
                                break;
                                
                        }   
                        break;
                    case "3":
                        Console.Clear();
                        shoppingCar.EndBuy(buyer,goods);
                        return;
                    case "4":
                        Console.Clear();
                        shoppingCar.ShowGoods(goods);
                        break;
                    default:
                        Console.Clear();
                        Console.WriteLine("输入错误信息,请重新开始");
                        break;

                }
                
            }
        }
    }
发布了15 篇原创文章 · 获赞 0 · 访问量 191

猜你喜欢

转载自blog.csdn.net/qq_43119892/article/details/104441837