墨尔本/晴 2021-06-23 15:18 采纳率: 0%
浏览 16

编写c#程序,求解,要求如下

具体要求如下:编写一个商品类,包括货号,商品名称,单价,库存,商品类别等字段,且要封装属性。编写带参的构造函数和空构造函数。编写一个SHOW()方法显示类的所有属性。编写一个饮料类,继承于商品类,饮料类中多出一个容量属性。继承要求构造函数赋值。主函数中,创立一个长度为10的商品类数组。并创建3个以上的对象填充数组。通过循环调用数组每一个对象的show方法显示商品信息。用户输入商品的货号,查找是否存在该商品,存在则再让用户输入购买数量,并将该商品的库存量减去购买数量,同时显示付款总数,注意库存量不可为负数。如果查不到商品货号也提示输入有误。

 

  • 写回答

1条回答

  • 鑫空之眼 2023-03-14 12:49
    关注

    下面是C#程序的示例代码:

    using System;
    
    namespace 商品类
    {
        // 商品类
        class Product
        {
            private string id;  // 商品货号
            private string name;  // 商品名称
            private decimal price;  // 单价
            private int stock;  // 库存
            private string category;  // 商品类别
    
            // 公共访问器和修改器,对属性进行封装
            public string Id
            {
                get { return id; }
                set { id = value; }
            }
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public decimal Price
            {
                get { return price; }
                set { price = value; }
            }
    
            public int Stock
            {
                get { return stock; }
                set { stock = value; }
            }
    
            public string Category
            {
                get { return category; }
                set { category = value; }
            }
    
            // 带参构造函数
            public Product(string id, string name, decimal price, int stock, string category)
            {
                this.id = id;
                this.name = name;
                this.price = price;
                this.stock = stock;
                this.category = category;
            }
    
            // 空构造函数
            public Product()
            {
            }
    
            // 显示类所有属性的方法
            public void Show()
            {
                Console.WriteLine("货号: {0}", id);
                Console.WriteLine("商品名称: {0}", name);
                Console.WriteLine("单价: {0:C}", price);
                Console.WriteLine("库存: {0}", stock);
                Console.WriteLine("商品类别: {0}", category);
                Console.WriteLine();
            }
        }
    
        // 饮料类,继承自商品类
        class Beverage : Product
        {
            private int capacity;  // 容量
    
            // 公共访问器和修改器,对属性进行封装
            public int Capacity
            {
                get { return capacity; }
                set { capacity = value; }
            }
    
            // 带参构造函数
            public Beverage(string id, string name, decimal price, int stock, string category, int capacity) : base(id, name, price, stock, category)
            {
                this.capacity = capacity;
            }
        }
    
        // 主函数
        class Program
        {
            static void Main(string[] args)
            {
                // 创立一个长度为10的商品类数组
                Product[] products = new Product[10];
    
                // 创建3个以上的对象填充数组
                products[0] = new Product("001", "可口可乐", 3.5M, 100, "饮料");
                products[1] = new Product("002", "百事可乐", 3.5M, 100, "饮料");
                products[2] = new Beverage("003", "矿泉水", 2M, 200, "饮料", 500);
    
                // 循环调用数组每一个对象的show方法显示商品信息
                foreach (Product p in products)
                {
                    if (p != null)
                    {
                        p.Show();
                    }
                }
    
                // 用户输入商品的货号
                Console.Write("请输入商品货号: ");
                string id = Console.ReadLine();
    
                int index = -1;
    
                // 遍历数组查找商品
                for (int i = 0; i < products.Length; i++)
                {
                    if (products[i] != null && products[i].Id == id)
                    {
                        index = i;  // 找到商品
                        break;
                    }
                }
    
                if (index == -1)
                {
                    Console.WriteLine("输入有误,无此商品!");
                }
                else
                {
                    // 让用户输入购买数量
                    Console.Write("请输入购买数量: ");
                    int amount = int.Parse(Console.ReadLine());
    
                    // 判断库存量是否足够
                    if (products[index].Stock >= amount)
                    {
                        decimal payment = products[index].Price * amount;  // 计算付款总数
                        Console.WriteLine("您需要支付: {0:C}", payment);
    
                        products[index].Stock -= amount;  // 将库存量减去购买数量
    
                        Console.WriteLine("商品 {0} 库存: {1}", products[index].Name, products[index].Stock);
                    }
                    else
                    {
                        Console.WriteLine("商品 {0} 库存不足,无法购买!", products[index].Name);
                    }
                }
    
                Console.ReadKey();
            }
        }
    }
    
    评论

报告相同问题?