简易版商品管理系统:
提示:最多不超过5种商品(水果,厨具,饼干等等)
输入1,进行添加商品的操作;
通过键盘输入每种商品的信息,并进行存储;
输入2,进入查询商品的操作(显示商品的基本信息);
打印列表:
商品id 名字 数量 价格
1001 苹果 100 1
1002 乐事薯片 20 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)
{
Goods[] goodsList = new Goods[5];
bool flag = true;
Console.WriteLine("-----------------------------------------");
Console.WriteLine("-\t 简易版商品管理系统 \t-");
Console.WriteLine("-\t输入1,进行添加商品的操作\t-");
Console.WriteLine("-\t输入2,进入查询商品的操作\t-");
Console.WriteLine("-\t\t输入0,退出\t\t-");
Console.WriteLine("-----------------------------------------");
while (flag)
{
Console.WriteLine("1--添加\t2--查询\t0--退出");
string value = Console.ReadLine();
int select = 0;
try
{
select = int.Parse(value);
}
catch (Exception)
{
Console.WriteLine("输入错误!请重新输入!");
continue;
}
switch (select)
{
case 0:
flag = false;
break;
case 1:
goodsList = GoodsMessage.AddGoods(goodsList);
break;
case 2:
GoodsMessage.QueryGoods(goodsList);
break;
default:
Console.WriteLine("输入错误!请重新输入!");
break;
}
}
}
}
}
商品管理类:
using System;
namespace 简易版商品管理系统
{
class GoodsMessage
{
//添加商品
public static Goods[] AddGoods(Goods[] goodsList)
{
Console.WriteLine("输入要添加商品的ID、名字、数量和价格(最多不超过5种商品): ");
for (int i = 0; i < goodsList.Length; i++)
{
//当数据为空时,输入数据
if (goodsList[i] == null)
{
int goodsId=0,num=0;
float price=0;
string name = "";
//捕获输入的异常,当输入异常时提示重新输入
try
{
Console.Write("商品ID: ");
goodsId = int.Parse(Console.ReadLine());
Console.Write("商品名字: ");
name = Console.ReadLine();
Console.Write("商品数量: ");
num = int.Parse(Console.ReadLine());
Console.Write("商品价格: ");
price = float.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("输入错误,请重新输入!");
continue;
}
goodsList[i] = new Goods(goodsId, name, num, price);
Console.WriteLine("输入 1 继续添加,其他退出添加 ");
int a = 0;
try
{
a = int.Parse(Console.ReadLine());
if (a == 1)
{
continue;
}
}
catch (Exception)
{
}
break;
}
}
if (goodsList[goodsList.Length-1]!=null)
{
Console.WriteLine("商品已满,无法添加!");
}
return goodsList;
}
public static void QueryGoods(Goods[] goodsList)
{
if (goodsList[0]==null)
{
Console.WriteLine("商品列表为空,请添加商品!");
Console.WriteLine();
}
else
{
Console.WriteLine("-----------------------------------------");
Console.WriteLine("ID\t名字\t数量\t价格");
for (int i = 0; i < goodsList.Length; i++)
{
if (goodsList[i] != null)
{
goodsList[i].ShowGoods();
}
else
{
break;
}
}
Console.WriteLine("-----------------------------------------");
}
}
}
}
商品类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 简易版商品管理系统
{
/*
* 商品id 名字 数量 价格
* 1001 苹果 100 1
* 1002 乐事薯片 20 2
*/
class Goods
{
public Goods(int goodsId, string name,int num,float price){
GoodsId = goodsId;
Name = name;
Num = num;
Price = price;
}
private int _goodsId;
public int GoodsId
{
get { return _goodsId; }
set {
if (value<=0)
{
Console.WriteLine("输入的商品ID <=0, 默认更改为 1 ");
_goodsId = 1;
}
_goodsId = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int _num;
public int Num
{
get { return _num; }
set {
if (value < 0)
{
Console.WriteLine("输入的数量 <0,默认更改为 0 ");
_num = 0;
}
_num = value;
}
}
private float _price;
public float Price
{
get { return _price; }
set {
if (value <= 0)
{
Console.WriteLine("输入的价钱 <=0 ,默认更改为 1");
_price = 1;
}
_price = value;
}
}
public void ShowGoods()
{
Console.WriteLine(GoodsId + "\t" + Name + "\t" + Num + "\t" + Price);
}
}
}
运行结果:
个人能力有限,只能初步这样实现!仅供观看!