执行对象的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 库存管理
{
class Program
{
static void Main(string[] args)
{
//实例化系统类得到系统对象
game g = new game();
g.stare();
//卡顿
Console.ReadKey();
}
}
}
系统类的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 库存管理
{
//系统类
class game
{
//货品的对象数组
//类名称[] 数组名 = new 类名称[长度]
Goods[] goodsList = new Goods[10];
//启动方法
//在此法中明确主流程
public void stare()
{
//加载一些数据
loadData();
//显示欢迎信息
welconme();
//显示功能菜单
showMeny();
//接收用户的选择
string choice = getUserChoice();
//根据用户的选择,调用对应的功能
//如果方法调用的时候传递了实参,那么方法定义的时候有点要有形参接收
runFunc(choice);
}
//打印热度最高的商品
public void prinHotestGoods()
{
//获取当前数组中对象的数量
int num = getGoodsNum();
//冒泡排序(降序)
//轮循环 冒几轮怕泡 取决于内容(获取一下内容)
for (int lun = 0; lun <= num - 1; lun++)
{
Console.WriteLine("第{0}轮冒泡", lun);
//光标游走循环
for (int i = 0; i < num - 1 - lun; i++)
{
//热度降序 左右比较 如果左边小于右边的 那就互换位置
if (goodsList[i].fav < goodsList[i + 1].fav)
{
//互换位置
//让临时变量接收当前光标的对象
Goods temp = goodsList[i];
//让下一个光标的对象赋值给当前的位置
goodsList[i] = goodsList[i + 1];
//让临时变量的值 赋值给i+1的位置
goodsList[i + 1] = temp;
}
}
}
//验证一下
for (int i = 0; i < goodsList.Length; i++)
{
//非空验证
if(goodsList[i]==null){
break;
}
//先拿到货品对象
Goods g = goodsList[i];
Console.WriteLine("货品名称为{0} , 他的热度为{1}", g.name, g.fav);
}
}
//获取对象数组中对象的数量
public int getGoodsNum()
{
int num = 0;
//遍历数组
for (int i = 0; i < goodsList.Length; i++)
{
//判断下标对应的值是否为null
//如果为null的话退出循环
if (goodsList[i] == null)
{
break;
}
num++;
}
return num;
}
//运行所需的功能
public void runFunc(string c)
{
//对接收到的数据进行一次分支操作
//查询系统的功能进行分支
switch (c)
{
case "1":
//搜索商品
searchGood();
break;
case "2":
prinHotestGoods();
break;
default:
Console.WriteLine("非法输入,请重试");
break;
}
}
//搜索商品的方法
public void searchGood()
{
//接收用户输入的商品名称
Console.WriteLine("请输入要搜索的商品名称");
string serachKey = Console.ReadLine();
//在用户数组中查找这个商品
for (int i = 0; i < goodsList.Length; i++)
{
//判断是否为空,如果是的话就终止
if (goodsList[i] == null)
{
break;
}
//如果是商品 就获取商品的名称 得到商品字符串
Goods g = goodsList[i];
string goodsName = g.name;
//拿商品的名称(字符串)与搜索词进行比较
if (goodsName.Equals(serachKey))
{
string place = g.place;
//如果比较结果是一样的 代表这就是我想要的货品 货品.place的得到地址
//输出结果
Console.WriteLine("你搜索的商品是{0},他的位置在{1}", serachKey, place);
break;
}
}
//在商品查找成功后 打印输出商品所在的位置
}
public string getUserChoice()
{
//提示用户输入
Console.WriteLine("请输入需要的功能:");
//接收用户键盘输入
string choice = Console.ReadLine();
//返回用户输入内容
return choice;
}
public void loadData()
{
//实例化货品对象1
Goods g1 = new Goods();
//货品对象.属性=值
g1.name = "洗发水";
g1.place = "1柜3行2列";
g1.fav = 6;
//把商品添加到数组中的方法调用
appendGood(g1);
//实例化货品对象2
Goods g2 = new Goods();
//货品对象.属性=值
g2.name = "汤达人";
g2.place = "2柜4行1列";
g2.fav = 20;
//把商品添加到数组中的方法调用
appendGood(g2);
//实例化货品对象3
//货品对象.属性=值
Goods g3 = new Goods();
g3.name = "洗面奶";
g3.place = "3柜1行6列";
g3.fav = 16;
//把商品添加到数组中的方法调用
appendGood(g3);
}
//定义一个商品的方法
public void appendGood(Goods item)
{
//数组添加元素的算法
for (int i = 0; i < goodsList.Length; i++)
{
if (goodsList[i] == null)
{
//把货品item添加到这个位置
goodsList[i] = item;
break;
}
}
}
//显示功能菜单
public void showMeny()
{
Console.WriteLine("本系统当前具有的功能");
Console.WriteLine("1,查询货品的位置");
Console.WriteLine("2,热度的商品排行榜");
}
//初始页面
public void welconme()
{
Console.WriteLine("===========欢迎使用库存管理系统===========");
}
}
}
货品类的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 库存管理
{
//定义一个货品类
class Goods
{
//名称
public string name;
//位置
public string place;
//满意度
public int fav;
}
}
效果图