库存管理系统 c# 代码

961 阅读2分钟

入口文件类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 库存管理系统
{
    class Program
    {
        static void Main(string[] args)
        {

            // 实例化系统对象
            StockManage sm = new StockManage();
            sm.start();

        }
    }
}

商品类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 库存管理系统
{
    class Goods
    {
        public string name;
        public int row;
        public int cols;
        public int love;
        public double price;

    }
}

系统管理类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 库存管理系统
{
    class StockManage
    {
        // 定义长度为6的数组
        Goods[] stock_list = new Goods[6];

        // 启动方法
        public void start() {
            // 系统启动。数据初始化
            initGoods();

            bool flag = true;
            while (flag)
            {
            
                // 显示菜能菜单
                Console.WriteLine("功能菜单");

                // 接收键盘输入
                Console.WriteLine("请输入需要的功能");
                string choice = Console.ReadLine();

                // 分支判断
                switch (choice) { 
                    case "1":
                        searchGoods();
                        break;
                    case "2":
                        Console.WriteLine("获得满意度最高的货品");
                        // 冒泡排序
                        mpSort();
                        // 显示结果
                        showGoodsList();

                        break;
                    case "3":
                        Console.WriteLine("退出系统");
                        flag = false;
                        break;
                        // 为了测试写的分支
                    case "4":
                        Console.WriteLine("显示所有的货品");
                        showGoodsList();
                        break;
                    default:
                        Console.WriteLine("非法输入");
                        break;
                }
                Console.WriteLine();

            }
        }

        // 显示所有货品的方法
        public void showGoodsList() { 
            // 遍历对象数组
            foreach (Goods item in stock_list) {
                // 去掉空的内容
                if (item == null) {
                    continue;
                }
                Console.WriteLine("!!");
                Console.WriteLine("商品名{0},价格{1},好评{2}",item.name,item.price,item.love);

            }
        }

        // 初始化货品方法
        public void initGoods() { 
            
            // 第0号商品
            Goods g = new Goods();
            g.name = "水杯";
            g.row = 6;
            g.cols = 2;
            g.love = 9;
            g.price = 16;
            // 添加到数组中
            stock_list[0] = g;

            // 第1号商品
            g = new Goods();
            g.name = "麦片";
            g.row = 6;
            g.cols = 1;
            g.love = 23;
            g.price = 24.6;
            // 添加到数组中
            stock_list[1] = g;


            // 第2号商品
            g = new Goods();
            g.name = "豆奶";
            g.row = 6;
            g.cols = 1;
            g.love = 18;
            g.price = 11;
            // 添加到数组中
            stock_list[2] = g;
        }

        // 查找商品的方法
        public void searchGoods()
        { 
            // 询问查找的商品名称
            Console.WriteLine("要搜索什么商品:");
            string search_name = Console.ReadLine();
            
            // 遍历商品对象数组
            foreach(Goods item in stock_list){
                // 遇空停止
                if (item == null) {
                    break;

                }

                // 判断名字
                if (item.name.Equals(search_name)) {
                    Console.WriteLine("找到了");
                    // 输出商品的信息
                    Console.WriteLine("商品{0}的位置在{1}行,{2}列",item.name,item.row,item.cols);
                    break;
                }
            }

            
        }

        // 冒泡排序对象数组,当前有效数据为3
        public void mpSort() {

            int arr_length = 3;

            // 冒泡几轮  范围是0 1 
            for (int i = 0; i < arr_length - 1; i++) { 
            
                // 这一轮的有效数据长度是多少?
                int useful_lengh = arr_length - i;

                // 这一轮可游走的最大下标是多少?
                int max_idx = useful_lengh - 2;

                // 遍历,让下标游走
                for (int j = 0; j <= max_idx;j++ ) { 
                    // 比较大小,小的冒右边
                    if (stock_list[j].love < stock_list[j + 1].love) {

                        Goods temp = stock_list[j];
                        stock_list[j] = stock_list[j + 1];
                        stock_list[j + 1] = temp;
               
                    }

                }

            }

            Console.WriteLine("冒泡完毕");

       
        }

    }
}