using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProductsManager
{
class Program
{
static void Main(string[] args)
{
#region
List<Products> products = new List<Products>();
List<ProductType> protype = new List<ProductType>();
ProductType food = new ProductType(1, "食品");
ProductType appliance = new ProductType(2, "家电");
protype.Add(food);
protype.Add(appliance);
Products bread = new Products(1, "面包", 3, "盼盼面包", food);
Products noodles = new Products(2, "方便面", 2.5, "康师傅", food);
Products refrigerator = new Products(3, "冰箱", 2000, "格力", appliance);
Products computer = new Products(4, "电脑", 3000, "戴尔", appliance);
products.Add(bread);
products.Add(noodles);
products.Add(refrigerator);
products.Add(computer);
int prolength = products.Count;
int typelength = protype.Count;
#endregion
Console.Title = "商品管理管理系统";
while (true)
{
Console.Clear();
Console.WriteLine("-----------商品管理系统-----------");
Console.WriteLine(" 1.查询全部商品");
Console.WriteLine(" 2.分类查询商品");
Console.WriteLine(" 3.添加、删除商品类型");
Console.WriteLine(" 4.添加、删除商品");
Console.WriteLine(" 5.修改商品信息");
Console.WriteLine(" 6.退出");
Console.WriteLine("请输入1-6进行操作:");
int num = int.Parse(Console.ReadLine());
switch (num)
{
case 1:
InquiresAll(products);
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
break;
case 2:
InquiresAny(products, protype, typelength);
break;
case 3:
ChangeProType(protype, products, ref typelength);
break;
case 4:
ChangeProduct(products, protype, ref prolength);
break;
case 5:
ModifyProduct(products, protype, prolength);
break;
case 6:
return;
default:
Warnning("请输入数字1-6来进行操作", ConsoleColor.Red);
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
break;
}
}
}
//查询全部商品或部分商品
static void InquiresAll(List<Products> products)
{
Console.Clear();
Console.WriteLine("------------------------查询全部商品------------------------");
foreach (Products product in products)
{
Console.WriteLine("{0,5}.{1,-" + (12 - (Encoding.Default.GetByteCount(product.name) - product.name.Length)).ToString() + "}¥{2,-10}{3,-" + (12 - (Encoding.Default.GetByteCount(product.standard) - product.standard.Length)).ToString() + "}{4,-" + (14 - (Encoding.Default.GetByteCount(product.protype.name) - product.protype.name.Length)).ToString() + "}", product.num, product.name, product.price, product.standard, product.protype.name);
}
if (products.Count == 0)
{
Warnning("没有该类型的商品!", ConsoleColor.Red);
}
}
//分类查询商品
static void InquiresAny(List<Products> products, List<ProductType> protype, int typelength)
{
List<Products> tempPorduct = new List<Products>();
while (true)
{
tempPorduct.Clear();
Console.Clear();
Console.WriteLine("-------------------分类查询商品-------------------");
foreach (ProductType type in protype)
{
Console.Write(" {0,3}.{1,-8}", type.num, type.name);
}
Console.WriteLine(" {0}.返回", typelength + 1);
Console.WriteLine("请输入您想查询的商品类型编号:");
int num = int.Parse(Console.ReadLine());
if (num > typelength + 1)
{
Warnning("请输入数字1-" + (typelength + 1) + ":", ConsoleColor.Red);
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
continue;
}
if (num == typelength + 1)
{
return;
}
foreach (Products product in products)
{
if (product.protype.num == num)
{
tempPorduct.Add(product);
}
}
InquiresAll(tempPorduct);
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
}
}
//添加、删除商品类型
static void ChangeProType(List<ProductType> protype, List<Products> products, ref int typelength)
{
while (true)
{
bool flag = false;
Console.Clear();
Console.WriteLine("---------添加或删除商品类型---------");
Console.WriteLine(" 1.添加商品类型");
Console.WriteLine(" 2.删除商品类型");
Console.WriteLine(" 3.返回");
Console.WriteLine("请输入1-3来进行操作:");
int num = int.Parse(Console.ReadLine());
switch (num)
{
case 1:
bool tempflag = true;
Console.Write("请输入您想要添加的商品的类型名称:");
string name = Console.ReadLine();
foreach (ProductType pt in protype)
{
if (pt.name == name)
{
tempflag = false;
break;
}
}
if (tempflag)
{
int length = typelength + 1;
ProductType type = new ProductType(length, name);
protype.Add(type);
Console.WriteLine("添加商品类型成功");
typelength += 1;
}
else
{
Warnning("该商品类型已存在,无法添加", ConsoleColor.Red);
}
break;
case 2:
Console.Clear();
Console.WriteLine("------------添加或删除商品类型------------");
for (int i = 1; i <= protype.Count; i++)
{
foreach (ProductType pt in protype)
{
if (pt.num == i)
{
Console.Write(" {0}.{1,-5}", pt.num, pt.name);
break;
}
}
}
Console.WriteLine();
Console.Write("请输入您想要删除的商品类型的编号:");
int tempnum = int.Parse(Console.ReadLine());
foreach (ProductType pt in protype)
{
if (tempnum == pt.num)
{
foreach (Products p in products)
{
if (p.protype.num == tempnum)
{
flag = true;
}
}
if (!flag)
{
protype.Remove(pt);
flag = true;
Console.WriteLine("删除商品类型成功");
break;
}
else
{
Warnning("该类型有相应商品存在,无法删除", ConsoleColor.Red);
}
}
}
if (!flag)
{
Warnning("您想要删除的商品类型不存在", ConsoleColor.Red);
}
break;
case 3:
return;
default:
Warnning("请输入数字1-3来进行操作", ConsoleColor.Red);
break;
}
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
}
}
//添加、删除商品
static void ChangeProduct(List<Products> products, List<ProductType> protype, ref int prolength)
{
while (true)
{
Console.Clear();
Console.WriteLine("---------添加或删除商品---------");
Console.WriteLine(" 1.添加商品");
Console.WriteLine(" 2.删除商品");
Console.WriteLine(" 3.返回");
Console.WriteLine("请输入1-3来进行操作:");
int num = int.Parse(Console.ReadLine());
switch (num)
{
case 1:
bool flag1 = false;//判断输入的商品型号是否存在,不存在不让添加
Console.Write("请输入商品的名称:");
string name = Console.ReadLine();
Console.WriteLine("请输入商品的单价");
double price = double.Parse(Console.ReadLine());
Console.WriteLine("请输入商品的规格");
string standard = Console.ReadLine();
Console.WriteLine("请输入商品的类型编号");
int tempnum = int.Parse(Console.ReadLine());
foreach (ProductType pt in protype)
{
if (pt.num == tempnum)
{
Products pro = new Products(prolength + 1, name, price, standard, pt);
products.Add(pro);
Console.WriteLine("添加商品成功");
prolength += 1;
flag1 = true;
break;
}
}
if (!flag1)
{
Warnning("添加商品失败,商品型号不存在", ConsoleColor.Red);
}
break;
case 2:
List<Products> tempPorduct = new List<Products>();
Console.Clear();
bool flag = false;
Console.WriteLine("------------添加或删除商品------------");
InquiresAll(products);
Console.WriteLine();
Console.Write("请输入您想要删除的商品名称:");
string oldname = Console.ReadLine();
foreach (Products pt in products)
{
if (pt.name.Contains(oldname))
{
tempPorduct.Add(pt);
}
}
if (tempPorduct.Count == 0)
{
Warnning("查找不到您想要删除的商品,删除失败", ConsoleColor.Red);
break;
}
InquiresAll(tempPorduct);
Console.WriteLine("请输入您想要删除的商品的编号");
int oldnum = int.Parse(Console.ReadLine());
foreach (Products pt in tempPorduct)
{
if (pt.num == oldnum)
{
products.Remove(pt);
flag = true;
Console.WriteLine("------------添加或删除商品------------");
InquiresAll(products);
Console.WriteLine("删除商品成功");
break;
}
}
if (!flag)
{
Warnning("您想要删除的商品不存在", ConsoleColor.Red);
}
break;
case 3:
return;
default:
Warnning("请输入数字1-3来进行操作", ConsoleColor.Red);
break;
}
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
}
}
//修改商品信息
static void ModifyProduct(List<Products> products, List<ProductType> protype, int prolength)
{
while (true)
{
bool flag1 = false;
bool flag2 = false;
InquiresAll(products);
Console.WriteLine(" {0}.返回", prolength + 1);
Console.WriteLine("请输入要修改的商品的编号:");
int num = int.Parse(Console.ReadLine());
if (num == prolength + 1)
{
return;
}
foreach (Products p in products)
{
if (p.num == num)
{
Console.WriteLine(" 1.{0,-6}2.{1,-6}3.{2,-6}4.{3,-6}", "名称", "单价", "规格", "类型编号");
Console.WriteLine("请选择修改项:");
switch (Console.ReadLine())
{
case "1":
Console.Write("请输入商品的名称:");
p.name = Console.ReadLine();
flag1 = true;
break;
case "2":
Console.WriteLine("请输入商品的单价");
p.price = double.Parse(Console.ReadLine());
flag1 = true;
break;
case "3":
Console.WriteLine("请输入商品的规格");
p.standard = Console.ReadLine();
flag1 = true;
break;
case "4":
for (int j = 1; j <= protype.Count; j++)
{
foreach (ProductType pt in protype)
{
if (pt.num == j)
{
Console.Write(" {0}.{1,-5}", pt.num, pt.name);
break;
}
}
}
Console.WriteLine();
Console.WriteLine("请输入商品的类型编号");
int tempnum = int.Parse(Console.ReadLine());
foreach (ProductType pt in protype)
{
if (pt.num == tempnum)
{
flag1 = true;
p.protype = pt;
break;
}
}
break;
}
flag2 = true;
break;
}
}
if (!flag2)
{
Warnning("该编号对应的商品不存在,无法修改", ConsoleColor.Red);
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
}
if (!flag1)
{
Warnning("修改失败,商品类型不存在", ConsoleColor.Red);
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
continue;
}
Console.WriteLine("修改成功");
Warnning("返回请按任意键", ConsoleColor.Blue);
Console.ReadKey();
}
}
/// <summary>
/// 显示提示
/// </summary>
/// <param name="s">提示的内容</param>
/// <param name="color">设置提示的前景色</param>
static void Warnning(string s, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(s);
Console.ResetColor();
}
}
//商品
class Products
{
public int num;
public string name;
public double price;
public string standard;
public ProductType protype;
public Products(int num, string name, double price, string standard, ProductType protype)
{
this.num = num;
this.name = name;
this.price = price;
this.standard = standard;
this.protype = protype;
}
}
//商品类型
class ProductType
{
public int num;
public string name;
public ProductType(int num, string name)
{
this.num = num;
this.name = name;
}
}
}