使用C#编辑的简易电子商城小项目
类
在个人设计的项目中总共四款电子商品,他们有共同的父类;五种可供选择的打折方式(书写了两种类),他们亦有共同的父类;除此之外还有一个仓库类,和收银类;与主函数所在类共计11个类。
下面展示 全部代码
。
// A code block
using System;
using System.Collections.Generic;
namespace Elect
{
class Program
{
static void Main(string[] args)
{
//创建收银台
CashRegister cr = new CashRegister();
//展示货物
cr.Show();
//与用户交互
cr.AskBuying();
Console.ReadKey();
}
}
/// <summary>
/// 货物类型的父类
/// </summary>
public class Father
{
double _price;
public double Price
{
get;
set;
}
string _id;
public string ID
{
get;
set;
}
string _name;
public string Name
{
get;
set;
}
public Father(double price,string id, string name)
{
this.Price = price;
this.ID = id;
this.Name = name;
}
}
public class SamSung:Father
{
public SamSung(double price, string id, string name)
: base(price, id,name)
{
}
}
public class Dell : Father
{
public Dell(double price, string id, string name)
: base(price, id, name)
{
}
}
public class Acer : Father
{
public Acer(double price, string id, string name)
: base(price, id, name)
{
}
}
public class Apple : Father
{
public Apple(double price, string id, string name)
: base(price, id, name)
{
}
}
/// <summary>
/// 仓库
/// </summary>
public class Store
{
public static List<List<Father>> list = new List<List<Father>>();
public Store(int NUM=4)
{
for (int i = 0; i < NUM; i++)
list.Add(new List<Father>());
}
/// <summary>
/// 进货
/// </summary>
/// <param name="Type">商品种类</param>
/// <param name="count">数量</param>
public void Purchase(string Type,int count)
{
for(int i = 0; i < count; i++)
{
switch (Type)
{
case "SamSung":
list[0].Add(new SamSung(1500, Guid.NewGuid().ToString(), "三星手机"));
break;
case "Dell":
list[1].Add(new Dell(8000, Guid.NewGuid().ToString(), "戴尔笔记本"));
break;
case "Acer":
list[2].Add(new Dell(5000, Guid.NewGuid().ToString(), "宏基笔记本"));
break;
case "Apple":
list[3].Add(new Dell(13000, Guid.NewGuid().ToString(), "苹果笔记本"));
break;
}
}
}
/// <summary>
/// 取货
/// </summary>
/// <param name="Type"></param>
/// <param name="count"></param>
/// <returns></returns>
public Father[] PickUp(string Type, int count)
{
Father[] products = new Father[count];
for (int i = 0; i < count; i++)
{
switch (Type)
{
case "SamSung":
products[i] = list[0][0];
list[0].RemoveAt(0);
break;
case "Dell":
products[i] = list[1][0];
list[1].RemoveAt(0);
break;
case "Acer":
products[i] = list[2][0];
list[2].RemoveAt(0);
break;
case "Apple":
products[i] = list[3][0];
list[3].RemoveAt(0);
break;
}
}
return products;
}
public void Show()
{
foreach (var item in list)
Console.WriteLine("仓库里面有{0}{1}个,单价为{2}元", item[0].Name, item.Count, item[0].Price);
}
}
/// <summary>
/// 收银台
/// </summary>
public class CashRegister
{
//创建仓库
public Store store = new Store();
public CashRegister()
{
store.Purchase("SamSung", 500);
store.Purchase("Dell", 100);
store.Purchase("Acer", 500);
store.Purchase("Apple", 100);
}
public void AskBuying()
{
Console.WriteLine("我们有:SamSung、Dell、Acer、Apple");
Father[] proctor = null;
List<Father> list = new List<Father>();
double consumption = 0;
Console.WriteLine("你需要哪些电子商品?输入NULL时表示结束");
string Type = Console.ReadLine();
while (Type != "NULL")
{
Console.WriteLine("您需要多少件?");
int count = Convert.ToInt32(Console.ReadLine());
//取货
proctor= store.PickUp(Type, count);
Console.WriteLine("你需要哪些电子商品?");
list.AddRange(proctor);
Type = Console.ReadLine();
consumption = Consumption(proctor,consumption);
}
Console.WriteLine("您实际消费了{0}元", consumption);
Console.WriteLine("a.可供您选择的打折方案:1、打九折(普通会员),2、打八折(超级会员),3、不打折(非会员)");
Console.WriteLine("b.可供您选择的满减方案有:1、满1000减100(普通会员),2、满2000减300(超级会员)");
Console.WriteLine("可选择输入a.1 ...");
string input = Console.ReadLine();
//根据简单工厂的设计模式输出实际金额
DiscountFa dis = GetDis(input);
Console.WriteLine("实际消费金额为{0}.", dis.Discount(consumption));
//出示小票
Console.WriteLine("以下是您的购物清单");
foreach(Father item in list)
Console.WriteLine("货物名称:{0}\t单价:{1}\t编号:{2}", item.Name, item.Price, item.ID);
}
/// <summary>
/// 根据简单工厂的设计模式输出实际金额
/// </summary>
/// <param name="input">选择类型</param>
/// <returns></returns>
public DiscountFa GetDis(string input)
{
DiscountFa dis = null;
switch (input)
{
case "a.1":
dis = new Discount1(0.9);
break;
case "a.2":
dis = new Discount1(0.8);
break;
case "a.3":
dis = new Discount1(1);
break;
case "b.1":
dis = new MN(1000, 100);
break;
case "b.2":
dis = new MN(2000, 300);
break;
}
return dis;
}
/// <summary>
/// 展示货物信息
/// </summary>
public void Show()
{
store.Show();
}
public double Consumption(Father[] proctor, double consumption = 0)
{
foreach (Father item in proctor)
consumption += item.Price;
//consumption += proctor[0].Price*proctor.Length;
return consumption;
}
}
/// <summary>
/// 折扣的父类
/// </summary>
public abstract class DiscountFa
{
public abstract double Discount(double con);
}
public class Discount1:DiscountFa
{
public double Rate
{
get;
set;
}
public Discount1(double rate)
{
this.Rate = rate;
}
public override double Discount(double con)
{
return con*this.Rate;
}
}
/// <summary>
/// 每满M减N
/// </summary>
public class MN:DiscountFa
{
public double M
{
get;
set;
}
public double N
{
get;
set;
}
public MN(double m,double n)
{
this.M = m;
this.N = n;
}
public override double Discount(double con)
{
if (con >= this.M)
con -= this.N*con/this.M;
return con;
}
}
}