当前位置: 首页 > news >正文

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

参考代码:

using System;
using System.Collections.Generic;namespace FoodRobotDemo
{public class FoodInfo{      public string Name { get; set; }     public int Id { get; set; }       public int  Count { get; set; }}public class FoodRobot{private List<FoodInfo> listFood;public FoodRobot(){listFood = new List<FoodInfo>();}public int this[string name,int id]{get{FoodInfo f = listFood.Find(x => x.Name ==name && x.Id == id);if (f != null){return f.Count;}else{Console.WriteLine("不存在 编号为{0}名称为{1}的食品",id,name);return -1;}}set{listFood.Add(new FoodInfo() { Name = name, Id = id, Count = value });}}//索引器重载,根据名字查找所有成绩public List<FoodInfo> this[string name]{get{List<FoodInfo> tempList = listFood.FindAll(x => x.Name == name);return tempList;}}}class Program{static void Main(string[] args){//多参数索引器和索引器重载          FoodRobot foodRobot = new FoodRobot();foodRobot["航天", 1] = 11;foodRobot["航空", 2] = 22;foodRobot["宇航", 3] = 33;foodRobot["宇航", 4] = 44;       Console.WriteLine("编号:2 航空牌  食品数量:{0}",foodRobot["航空", 2]);Console.WriteLine(foodRobot["康师傅", 6]);     List<FoodInfo> listFood = foodRobot["宇航"];if (listFood.Count > 0){foreach (var e in listFood){Console.WriteLine(string.Format("宇航 牌食品 编号:{0} 数量:{1}", e.Id, e.Count));}}else{Console.WriteLine("无该食品");}Console.ReadKey();}}
}

http://www.lryc.cn/news/207459.html

相关文章:

  • docker部署rabbitmq的坑
  • 【python VS vba(系列2)】 python和vba读写EXCEL文件的方式比较 (建设ing)
  • 小程序 swiper滑动 层叠滑动效果
  • 【20年VIO梳理】
  • Java Object类详解
  • Unity 中忽略图片透明度的 Image 组件的修改版本
  • hibernate源码(1)--- schema创建
  • 数学与经济管理
  • 自动化测试系列 —— UI自动化测试
  • 眨个眼就学会了PixiJS
  • WORD中的表格内容回车行距过大无法调整行距
  • MySQL 高级函数整理
  • UG\NX二次开发 连接曲线、连结曲线 UF_CURVE_auto_join_curves
  • python爬虫入门(四)爬取猫眼电影排行(使用requests库和正则表达式)
  • Mybatis-Plus CRUD
  • 【强化学习】08——规划与学习(采样方法|决策时规划)
  • (链表) 25. K 个一组翻转链表 ——【Leetcode每日一题】
  • VisualStudio[WPF/.NET]基于CommunityToolkit.Mvvm架构开发
  • 深度学习_5_模型拟合_梯度下降原理
  • 大模型时代,AI如何成为数实融合的驱动力?
  • MS COCO数据集的评价标准以及不同指标的选择推荐(AP、mAP、MS COCO、AR、@、0.5、0.75、1、目标检测、评价指标)
  • css实现鼠标多样化
  • 21.2 Python 使用Scapy实现端口探测
  • Qt设计一个自定义的登录框窗口
  • 05 MIT线性代数-转置,置换,向量空间Transposes, permutations, spaces
  • [数据结构】二叉树
  • idea 中配置 maven
  • Python---for循环嵌套
  • 189. 轮转数组 --力扣 --JAVA
  • C# 使用waveIn实现声音采集