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

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

参考代码:

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.kler.cn/a/105018.html

相关文章:

  • UI 自动化测试框架:PO模式+数据驱动
  • Redis数据类型
  • vueDay03——可灵活变动的class样式
  • 不希望你的数据在云中?关闭iPhone或Mac上的iCloud
  • 对长度为n的顺序表L,编写一个时间复杂度为O(n),空间复杂度为O(1)的算法,该算法删除线性表中的所有值为x的数据元素
  • 启动内核ip转发和其他优化
  • 深入探究Selenium定位技巧及最佳实践
  • 完美解决configure: error: APR not found. Please read the documentation.
  • threejs(6)-操控物体实现家居编辑器
  • AD9371 官方例程HDL详解之JESD204B TX侧时钟生成 (一)
  • 【Qt样式(qss)-5】qss局部渲染混乱,错乱,不生效的一种原因
  • 【Linux】线程同步
  • ShardingSphere-JDBC分库分表快速入门实战
  • 【Spring Boot系列】- Spring Boot事务应用详解
  • Jupyter使用技巧-环境篇
  • ES 11 新特性
  • FPGA从入门到精通(二十)SignalTapII
  • useGeneratedKeys=“true“ keyProperty=“id“
  • 经验风险最小化与结构风险最小化:优化机器学习模型的两种方法
  • 2022年12月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试