• [C#基础训练]FoodRobot食品管理部分代码-2


    参考代码:

    1. using System;
    2. using System.Collections.Generic;
    3. namespace FoodRobotDemo
    4. {
    5. public class FoodInfo
    6. {
    7. public string Name { get; set; }
    8. public int Id { get; set; }
    9. public int Count { get; set; }
    10. }
    11. public class FoodRobot
    12. {
    13. private List listFood;
    14. public FoodRobot()
    15. {
    16. listFood = new List();
    17. }
    18. public int this[string name,int id]
    19. {
    20. get
    21. {
    22. FoodInfo f = listFood.Find(x => x.Name ==name && x.Id == id);
    23. if (f != null)
    24. {
    25. return f.Count;
    26. }
    27. else
    28. {
    29. Console.WriteLine("不存在 编号为{0}名称为{1}的食品",id,name);
    30. return -1;
    31. }
    32. }
    33. set
    34. {
    35. listFood.Add(new FoodInfo() { Name = name, Id = id, Count = value });
    36. }
    37. }
    38. //索引器重载,根据名字查找所有成绩
    39. public List this[string name]
    40. {
    41. get
    42. {
    43. List tempList = listFood.FindAll(x => x.Name == name);
    44. return tempList;
    45. }
    46. }
    47. }
    48. class Program
    49. {
    50. static void Main(string[] args)
    51. {
    52. //多参数索引器和索引器重载
    53. FoodRobot foodRobot = new FoodRobot();
    54. foodRobot["航天", 1] = 11;
    55. foodRobot["航空", 2] = 22;
    56. foodRobot["宇航", 3] = 33;
    57. foodRobot["宇航", 4] = 44;
    58. Console.WriteLine("编号:2 航空牌 食品数量:{0}",foodRobot["航空", 2]);
    59. Console.WriteLine(foodRobot["康师傅", 6]);
    60. List listFood = foodRobot["宇航"];
    61. if (listFood.Count > 0)
    62. {
    63. foreach (var e in listFood)
    64. {
    65. Console.WriteLine(string.Format("宇航 牌食品 编号:{0} 数量:{1}", e.Id, e.Count));
    66. }
    67. }
    68. else
    69. {
    70. Console.WriteLine("无该食品");
    71. }
    72. Console.ReadKey();
    73. }
    74. }
    75. }

  • 相关阅读:
    软件测试/测试开发丨App自动化—高级控件交互方法
    云计算四层介绍
    MySQL的安装
    halcon 算子set_display_font
    大数据之kafka应用
    Leaflet加载天地图
    python安装
    Java网络编程、TCP、UDP、Socket通信---初识版
    抖音小程序开发教学系列(8)- 抖音小程序的案例分析
    关于三维布尔运算的几点思考
  • 原文地址:https://blog.csdn.net/vinglemar/article/details/134065847