二十七、基础语法之超市案例

1.foreach 迭代循环

//foreach 迭代循环(只读类型)
string str = "abcdef";
foreach (var item in str)//var 弱类型 会自动转换成在str中读取到的内容的类型
{
    Console.WriteLine(item);
    Console.WriteLine((int)item);//将char类型转换成int类型,其实也就是字符对应的ASCI码值
}

int[] nums = { 1, 2, 3, 4, 5, 6 };
foreach (int ShuZi in nums)
{
    Console.WriteLine(ShuZi);
}

 

2.环境变量

Environment.Exit(0);//关闭整个应用程序

return;//只能关闭单个方法功能

 

3.超市案例

某超市为了快速占有市场,要求做库存管理系统,功能有:

1.输入货品名称找到货品位置

2.快速找到客户满意度最高的商品

3.退出系统

实现的结果如下:

//定义商品属性类
namespace Supermarket
{
    public class Product
    {
        /// <summary>
        /// 商品名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 单价
        /// </summary>
        public double Price { get; set; }
        /// <summary>
        /// 满意度
        /// </summary>
        public int Love { get; set; }
        /// <summary>
        /// 商品位置
        /// </summary>
        public string Position { get; set; }
    }
}
//超市功能类
namespace Supermarket
{
    public class MarketManager
    {
        public void Init()
        {
            Console.WriteLine("===============================库存货品清单列表===============================");
            Product[] products = GetData();//获取数据
            
            foreach (Product item in products)
            {
                Console.WriteLine($"货品名称:{item.Name}");
            }
            Console.WriteLine("==============================================================================");

        }

        /// <summary>
        /// 获取货品信息
        /// </summary>
        /// <returns></returns>
        public Product[] GetData()
        {
            //Product[] products = new Product[3];
            //products[0] = new Product() { Name = "杯子", Love = 99, Position = "第一排第三个" };
            //products[1] = new Product() { Name = "花瓶", Love = 50, Position = "第二排第五个" };
            //products[2] = new Product() { Name = "热水器", Love = 99, Position = "第三排第一个" };

            //查询数据库,从数据库中把信息读取出来
            Product[] products =
            {
                new Product() { Name = "杯子", Love = 99, Position = "第一排第三个" },
                new Product() { Name = "花瓶", Love = 50, Position = "第二排第五个" },
                new Product() { Name = "热水器", Love = 70, Position = "第三排第一个" }
            };//初始化器
            return products;

        }

        public void ShowMenus()
        {
            do
            {
                Console.WriteLine("===========================欢迎使用库存管理系统================================");
                Console.WriteLine("1、根据货品名称获取货品位置 2、获取客户满意度最高的货品 3、退出");
                Console.WriteLine("请选择:");
                int menuId ;
                int.TryParse(Console.ReadLine(), out menuId);
                switch (menuId)
                {
                    case 1:
                        //获取货品位置
                        GetProductByName();
                        break;
                    case 2:
                        //获取满意度最高的物品
                        GetTopOne();
                        break;
                    case 3:
                        //退出
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("输入了错误的菜单编号!");
                        break;
                }
            } while (true);
        }


        /// <summary>
        /// 根据产品名称获取产品位置
        /// </summary>
        public void GetProductByName()
        {
            Console.WriteLine("请输入货品名称:");
            string name = Console.ReadLine();
            Product[] products = GetData();
            foreach (Product item in products)
            {
                if (item.Name.Equals(name))
                {
                    Console.WriteLine($"{item.Position}");
                    break;
                }
            }
        }
        /// <summary>
        /// 获取客户满意度最高的货品
        /// </summary>
        public void GetTopOne()
        {
            Product[] products = GetData();//调用、获取产品数组
            //冒泡排序 比较多少伦 比较多少次
            for (int i = 0; i < products.Length-1; i++)//比较两轮
            {
                for (int j = 0; j < products.Length - 1 - i; j++)//每轮比较次数
                {
                    //小于 判断出来 最大值下下标为0处;大于 判断出来 最大值在下表为2处
                    if (products[j].Love < products[j+1].Love)
                    {
                        //临时变量
                        Product temp = products[j];
                        products[j] = products[j + 1];
                        products[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine($"客户满意度最高的商品是{products[0].Name},满意度为:{products[0].Love}");
        }
    }
}
//在main()方法中调用
namespace Supermarket
{
    class Program
    {
        static void Main(string[] args)
        {
            MarketManager marketManager = new MarketManager();
            marketManager.Init();
            marketManager.ShowMenus();

            Console.ReadLine();
        }
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值