GUID:
产生一个不会重复的ID
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(); }
分析:
父类:全用的是自动属性
public class ProductFather { public double Price { get; set; } public string Name { get; set; } public double Count { get; set; } public string ID { get; set; } public ProductFather(string id, double price, double count,string name) { this.ID = id; this.Price = price; this.Count = count; this.Name = name; } }
子类:Acer
class Acer : ProductFather { public Acer(string id, double price, double count, string name) : base(id, price, count, name) { } }
子类:SumSung
class Acer : ProductFather { public Acer(string id, double price, double count, string name) : base(id, price, count, name) { } }
子类:JiangYou
class JiangYou : ProductFather { public JiangYou(string id, double price, double count, string name) : base(id, price, count, name) { } }
子类:Banana
class Banana : ProductFather { public Banana(string id, double price, double count, string name) : base(id, price, count, name) { } }
仓库类:
class CangKu { //Listlist = new List (); //存储货物 //list接收的是List<>,相当于货架 List
> list = new List
>(); /// /// 向用户展示货物 /// public void ShowPros() { foreach (var item in list) { Console.WriteLine("仓库现有:" + item[0].Name + "," + "\t" + item.Count + "个," + "\t" + "每个" + item[0].Price + "元。"); } } //list[0]存储Acer电脑 list[1]存储三星手机 list[2]存储酱油 list[3]存储香蕉 ////// 在创建仓库对象的时候,向仓库中添加货架 /// public CangKu() { list.Add(new List()); list.Add(new List ()); list.Add(new List ()); list.Add(new List ()); } /// /// 进货 /// /// 货物的类型 /// 货物的数量 public void JinPros(string strType, int count) { for (int i = 0; i < count; i++) { switch (strType) { case "Acer": list[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, 100,"宏基笔记本电脑")); break; case "SamSung": list[1].Add(new SamSung(Guid.NewGuid().ToString(), 2000, 300,"三星手机")); break; case "JiangYou": list[2].Add(new JiangYou(Guid.NewGuid().ToString(), 10, 3000,"老抽")); break; case "Banana": list[3].Add(new Banana(Guid.NewGuid().ToString(), 12, 1000,"香蕉")); break; } } } ////// 取货 /// /// 货物的类型 /// 数量 public ProductFather[] QuPros(string strType, int count) { ProductFather[] pros = new ProductFather[count]; for (int i = 0; i < count; i++) { switch (strType) { case "Acer": //判断如果货物数量为0,提示缺货 if (list[0].Count == 0) { Console.WriteLine("缺货"); } else { pros[i] = list[0][0]; list[0].RemoveAt(0); } break; case "SamSung": if (list[1].Count == 0) { Console.WriteLine("缺货"); } else { pros[i] = list[1][0]; list[1].RemoveAt(0); } break; case "JiangYou": if (list[2].Count == 0) { Console.WriteLine("缺货"); } else { pros[i] = list[2][0]; list[2].RemoveAt(0); } break; case "Banana": if (list[3].Count == 0) { Console.WriteLine("缺货"); } else { pros[i] = list[3][0]; list[3].RemoveAt(0); } break; } } return pros; } }