父类:
class ProductFather
{
public double Price {
get;
set;
}
public double Count {
get;
set;
}
public string ID {
get;
set;
}
public ProductFather(string id,double price,string name) {
this.ID = id;
this.Price = price;
this.Count = count;
}
}
商品子类:可以有很多个商品子类
class SamSung : ProductFather
{
public SamSung(string id, double price, string name) : base(id, price, name)
{
}
}
创建仓库,显示商品详情
//创建仓库
//List<ProductFather> list = new List<ProductFather>();
List<List<ProductFather>> list2 = new List<List<ProductFather>>();
/// <summary>
/// 有啥商品都展示一下子
/// </summary>
public void ShowPros() {
foreach (var item in list2)
{
Console.WriteLine("我们仓库有:"+item[0].Name+"\t"+"有"+item.Count+"个"+"\t"+"每个"+item[0].Price+"元");
}
}
//list[0]存储电脑
//list[1]存储手机
//list[2]存储酱油
//list[3]存储香蕉
/// <summary>
/// 在创建仓库的时候 向仓库中添加货架
/// </summary>
public CangKu(){
list2.Add(new List<ProductFather>());
list2.Add(new List<ProductFather>());
list2.Add(new List<ProductFather>());
list2.Add(new List<ProductFather>());
}
进货
/// <summary>
/// 进货
/// </summary>
/// <param name="strType">货物类型</param>
/// <param name="count">货物数量</param>
public void GetPors(string strType,int count) {
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "Acer":list2[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏基笔记本"));
break;
case "SamSung":list2[1].Add(new SamSung(Guid.NewGuid().ToString(), 2000, "三星手机"));
break;
case "jiangyou":list2[2].Add(new jiangyou(Guid.NewGuid().ToString(), 20, "酱油"));
break;
case "Banana":list2[3].Add(new Banana(Guid.NewGuid().ToString(), 10, "香蕉"));
break;
}
}
}
出货
/// <summary>
/// 出货
/// </summary>
/// <param name="strType">货物类型</param>
/// <param name="count">货物数量</param>
/// <returns></returns>
public ProductFather[] Outpors(string strType, int count) {
ProductFather[] pors = new ProductFather[count];
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "Acer":pors[i]= list2[0][0];
//list[0]是我们放笔记本的货架 list[0][0]是该货架第一个 单纯的赋值代码
list2[0].RemoveAt(0);
//每一次都删除第一个货物
break;
case "SamSung": pors[i] = list2[1][0];
list2[1].RemoveAt(0);
break;
case "jiangyou":pors[i] = list2[2][0];
list2[2].RemoveAt(0);
break;
case "Banana":pors[i] = list2[3][0];
list2[3].RemoveAt(0);
break;
}
}
return pors;
}
class SupperMarket
{
//创建仓库对象
CangKu ck = new CangKu();
/// <summary>
/// 创建超市对象的时候,给仓库的货架上导入货物
/// </summary>
public SupperMarket() {
ck.GetPors("Acer",50);
ck.GetPors("SamSung", 50);
ck.GetPors("jiangyou", 50);
ck.GetPors("Banana", 50);
}
public void AskBuying() {
Console.WriteLine("我们有,Acer,SamSung,jiangyou,Banana");
string strType = Console.ReadLine();
Console.WriteLine("你要多少");
int count = Convert.ToInt32(Console.ReadLine());
//去仓库取货
ProductFather[] pros = ck.Outpors(strType,count);
//计算价钱
double realMoney = GetMoney(pros);
Console.WriteLine("你总该付{0}",realMoney);
Console.WriteLine("請選擇你的打折方式1--不打折,2--打九折,3--打85折,4--滿300送50,5---滿500送100");
string input = Console.ReadLine();
//通过简单工厂的设计模式根据用户的输入获得一个打折对象
CalFather cal = GetCal(input);
double totalMoney = cal.GetTotalMoney(realMoney);//传入打折前的价格 返回折后价格
Console.WriteLine("打完折后,您应该付{0}",totalMoney);
Console.WriteLine("以下是您的購物信息");
foreach (var item in pros)
{
Console.WriteLine("貨物名稱"+item.Name + "\t"+"貨物單價" +item.Price + "\t" +"貨物編號"+item.ID);
}
}
/// <summary>
/// 根据用户的选择打折方式返回一个打折对象
/// </summary>
/// <param name="input">用户的选择</param>
/// <returns>返回父类的对象 但是里边装的是子类对象</returns>
public CalFather GetCal(string input) {
CalFather cal = null;
switch (input)
{
case "1": cal = new CalNormal();
break;
case "2":cal = new CalRate(0.9);
break;
case "3":cal = new CalRate(0.85);
break;
case "4":cal = new CalMN();
break;
}
return cal;
}
/// <summary>
/// 根据用户买的货物计算总价
/// </summary>
/// <param name="pors"></param>
/// <returns></returns>
public double GetMoney(ProductFather[] pors) {
double realMoney = 0;
for (int i = 0; i < pors.Length; i++)
{
realMoney += pors[i].Price;
}
return realMoney;
}
public void ShowPros() {
ck.ShowPros();
}
}
}
}
/// <summary>
/// 打折的父类
/// </summary>
/// abstract表示这是一个抽象类
abstract class CalFather
{
/// <summary>
/// 打折后该负多少钱
/// </summary>
/// <param name="realMoney">打折前应付价钱</param>
/// <returns>打折后该付的多少钱</returns>
public abstract double GetTotalMoney(double realMoney);
}
/// <summary>
/// 按折扣率打折
/// </summary>
class CalRate : CalFather
{
/// <summary>
///折扣率
/// </summary>
public double Rate
{
get;
set;
}
public CalRate(double rate) {
this.Rate = rate;
}
public override double GetTotalMoney(double realMoney)
{
return realMoney * this.Rate;
}
}
还有别的打折方式类
static void Main(string[] args)
{
//创建超市对象
SupperMarket sm = new SupperMarket();
//展示货物
sm.ShowPros();
sm.AskBuying();
Console.ReadKey();
Console.WriteLine("Hello World!");
}
就是这样就可以了。