控制台应用中如何实现分类列表间的商品数据录入与存储?
解决方案
1. 定义商品实体类
先创建Product类封装商品信息,实现统一管理:
public class Product { public string Name { get; set; } public string ProductType { get; set; } public int CategoryNo { get; set; } public Product(string name, string productType, int categoryNo) { Name = name; ProductType = productType; CategoryNo = categoryNo; } }
2. 重构Tools类(核心数据结构)
放弃单一列表或独立数组,改用Dictionary<int, List<Product>>按分类编码分组存储商品,同时维护分类映射字典快速查询分类名称:
public class Tools { // 键:分类编码,值:对应分类的商品列表 private Dictionary<int, List<Product>> _categoryProducts = new Dictionary<int, List<Product>>(); // 固定4个分类的编码与名称映射 private Dictionary<int, string> _categoryMap = new Dictionary<int, string> { { 111, "Electronics" }, { 222, "Crafting" }, { 333, "Cooking" }, { 444, "Construction" } }; public Tools() { // 初始化每个分类的空列表 foreach (var category in _categoryMap) { _categoryProducts.Add(category.Key, new List<Product>()); } } public void Insert(string name, string productType, int categoryNo) { if (_categoryProducts.ContainsKey(categoryNo)) { var newProduct = new Product(name, productType, categoryNo); _categoryProducts[categoryNo].Add(newProduct); } else { Console.WriteLine($"错误:分类编码 {categoryNo} 不存在"); } } public void Display() { foreach (var category in _categoryProducts) { Console.WriteLine($"=== {_categoryMap[category.Key]} (编码:{category.Key}) ==="); if (category.Value.Count == 0) { Console.WriteLine(" 暂无商品"); } else { foreach (var product in category.Value) { Console.WriteLine($" - {product.Name}"); } } Console.WriteLine(); } } // 提供分类映射给主程序使用 public Dictionary<int, string> GetCategoryMap() { return _categoryMap; } }
3. 完善主程序的添加商品逻辑
在选项2中展示分类编码列表,接收用户输入后收集商品信息并添加到对应分类:
static void Main(string[] args) { Tools aTool = new Tools(); // 初始化示例商品 aTool.Insert("Product One", "Electronics", 111); aTool.Insert("Product Two", "Crafting", 222); aTool.Insert("Product Three", "Cooking", 333); aTool.Insert("Product Four", "Construction", 444); while (true) // 循环菜单,无需重复启动程序 { Console.Clear(); Console.WriteLine("Please enter a number based on the following:"); Console.WriteLine("1: View Products"); Console.WriteLine("2: Add Products"); Console.WriteLine("3: Quit"); Console.Write("\nSelect an option: "); string input = Console.ReadLine(); if (input == "1") { Console.Clear(); aTool.Display(); Console.WriteLine("\n按任意键返回菜单..."); Console.ReadKey(); } else if (input == "2") { Console.Clear(); var categoryMap = aTool.GetCategoryMap(); Console.WriteLine("请选择分类编码:"); foreach (var category in categoryMap) { Console.WriteLine($"{category.Key}: {category.Value}"); } Console.Write("\n输入分类编码:"); if (int.TryParse(Console.ReadLine(), out int categoryNo)) { if (categoryMap.ContainsKey(categoryNo)) { Console.Write("输入商品名称:"); string productName = Console.ReadLine(); // 自动获取分类名称,无需用户重复输入 string productType = categoryMap[categoryNo]; aTool.Insert(productName, productType, categoryNo); Console.WriteLine("商品添加成功!"); } else { Console.WriteLine("无效的分类编码!"); } } else { Console.WriteLine("请输入有效的数字编码!"); } Console.WriteLine("\n按任意键返回菜单..."); Console.ReadKey(); } else if (input == "3") { Environment.Exit(0); } else { Console.WriteLine("无效选项,请重新输入!"); Console.WriteLine("\n按任意键返回菜单..."); Console.ReadKey(); } } }
关键说明
- 用
Dictionary<int, List<Product>>实现按分类编码分组存储,既避免多个独立数组的冗余,又能快速定位对应分类的商品列表。 - 分类映射字典固定4个分类,符合你“当前列表长度设为[4]”的要求。
- 添加商品时自动从映射中获取分类名称,减少用户输入错误。
- 主程序加入循环菜单,操作更流畅,无需重复启动程序。
内容的提问来源于stack exchange,提问作者thehelaeper67
相关产品推荐
相关产品推荐

