发布网友 发布时间:2022-04-21 15:53
共1个回答
热心网友 时间:2022-04-21 17:22
建立储位类名称属性与层位Dictionary再写操作类商品加入找储位层数于5商品放于5找储位循环行给例我写控制台程序 /// /// 储位类 /// class StorageItem { /// /// 名称 /// public string Name { get; set; } /// /// 储存货物相数组 /// public Dictionary Lists { get; set; } /// /// 构造 /// public StorageItem() { Name = ""; Lists = new Dictionary(); } } /// /// 储位处理类 /// class StorageOperation { public List StorageLists {get;set;} public StorageOperation() { StorageLists = new List(); } /// /// 储位加进用几加几 /// /// public void Add(String name) { StorageLists.Add(new StorageItem() { Name = name }); } /// /// 加商品自加储位 /// /// /// public String AddGoods(String name) { string msg = ""; if (StorageLists.Count == 0) { msg = "未建立储位!能加商品!"; } else { foreach (var item in StorageLists) { if (item.Lists.Count < 5) { var sid = (item.Lists.Count + 一).ToString(); //拼层号 item.Lists.Add(sid, name); msg = "[" + name + "]放" + item.Name + sid; break; } } } return msg; } /// /// 返现空白储位信息 /// /// public String StorageEmptyInfo() { string msg = ""; if (StorageLists.Count == 0) { msg = "未建立储位"; } else { foreach (var item in StorageLists) { msg += item.Name + "已使用" + item.Lists.Count.ToString() + "层" + (5 - item.Lists.Count).ToString() + "用\n"; } } return msg; } } Main()调用类 static void Main(string[] args) { StorageOperation operation = new StorageOperation(); //建立四储位 operation.Add("一陆"); operation.Add("一漆"); operation.Add("一吧"); operation.Add("一9"); Console.WriteLine(operation.AddGoods("A商品")); Console.WriteLine(operation.AddGoods("B商品")); Console.WriteLine(operation.AddGoods("C商品")); Console.WriteLine(operation.AddGoods("D商品")); Console.WriteLine(operation.AddGoods("E商品")); Console.WriteLine(operation.AddGoods("F商品")); Console.WriteLine(operation.AddGoods("G商品")); Console.WriteLine(operation.AddGoods("H商品")); Console.WriteLine(operation.AddGoods("一商品")); Console.WriteLine(operation.AddGoods("二商品")); Console.WriteLine(operation.AddGoods("三商品")); Console.WriteLine(operation.AddGoods("四商品")); Console.WriteLine(operation.AddGoods("5商品")); Console.WriteLine("============================"); Console.WriteLine(operation.StorageEmptyInfo()); Console.ReadKey(); }