Acer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class Acer:ProductFather
{
public Acer(string id, double price, string Name)
: base(id, price, Name)
{
}
}
}
Banana.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class Banana : ProductFather
{
public Banana(string id, double price, string Name)
: base(id, price, Name)
{
}
}
}
CalFather.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
/// <summary>
/// 打折的父类
/// </summary>
abstract class CalFather
{
/// <summary>
/// 计算打折后应付多少钱
/// </summary>
/// <param name="realMoney">打折前应付的价钱</param>
/// <returns>打折后应付的前</returns>
public abstract double GetTotalMoney(double realMoney);
}
}
CalMN.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
/// <summary>
/// 买M元 送N元
/// </summary>
class CalMN:CalFather
{
//买500送100
public double M
{
get;
set;
}
public double N
{
get;
set;
}
public CalMN(double m, double n)
{
this.M = m;
this.N = n;
}
public override double GetTotalMoney(double realMoney)
{
//600 -100
//1000-200
//1200
if (realMoney >= this.M)
{
return realMoney - (int)(realMoney / this.M) * this.N;
}
else
{
return realMoney;
}
}
}
}
CalNormal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
/// <summary>
/// 不打折 该多少钱就多少钱
/// </summary>
class CalNormal:CalFather
{
public override double GetTotalMoney(double realMoney)
{
return realMoney;
}
}
}
CalRate.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
/// <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;
}
}
}
CangKu.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class CangKu
{
List<List<ProductFather>> list = new List<List<ProductFather>>();
/// <summary>
///向用户展示货物
/// </summary>
public void ShowPros()
{
foreach (var item in list)
{
Console.WriteLine("我们超市有:" + item[0].Name + "," + "\t" + "有" + item.Count + "个," + "\t" + "每个" + item[0].Price + "元");
}
}
//list[0]存储Acer电脑
//list[1]存储三星手机
//list[2]存储酱油
//list[3]存储香蕉
/// <summary>
/// 在创建仓库对象的时候 像仓库中添加货架
/// </summary>
public CangKu()
{
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
}
/// <summary>
/// 进货
/// </summary>
/// <param name="strType">货物的类型</param>
/// <param name="count">货物的数量</param>
public void JinPros(string strType, int count)
{
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "Acer": list[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏基笔记本"));
break;
case "SamSung": list[1].Add(new SamSung(Guid.NewGuid().ToString(), 2000, "棒之手机"));
break;
case "JiangYou": list[2].Add(new JiangYou(Guid.NewGuid().ToString(), 10, "老抽酱油"));
break;
case "Banana": list[3].Add(new Banana(Guid.NewGuid().ToString(), 50, "大香蕉"));
break;
}
}
}
/// <summary>
/// 从仓库中提取货物
/// </summary>
/// <param name="strType"></param>
/// <param name="count"></param>
/// <returns></returns>
public ProductFather[] QuPros(string strType, int count)
{
ProductFather[] pros = new ProductFather[count];
for (int i = 0; i < pros.Length; i++)
{
switch (strType)
{
case "Acer":
pros[i] = list[0][0];
list[0].RemoveAt(0);
break;
case "SamSung": pros[i] = list[1][0];
list[1].RemoveAt(0);
break;
case "JiangYou": pros[i] = list[2][0];
list[2].RemoveAt(0);
break;
case "Banana": pros[i] = list[3][0];
list[3].RemoveAt(0);
break;
}
}
return pros;
}
}
}
JiangYou.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class JiangYou:ProductFather
{
public JiangYou(string id, double price, string Name)
: base(id, price, Name)
{
}
}
}
ProductFather.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class ProductFather
{
public double Price
{
get;
set;
}
public string Name
{
get;
set;
}
public string ID
{
get;
set;
}
public ProductFather(string id, double price, string Name)
{
this.ID = id;
this.Price = price;
this.Name = Name;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class Program
{
static void Main(string[] args)
{
//创建超市对象
SupperMarket sm = new SupperMarket();
//展示货物
sm.ShowPros();
//跟用户交互
sm.AskBuying();
Console.ReadKey();
}
}
}
SamSung.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class SamSung:ProductFather
{
public SamSung(string id, double price, string Name)
: base(id, price, Name)
{
}
}
}
SupperMarket.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16超市收银系统
{
class SupperMarket
{
//创建仓库对象
CangKu ck = new CangKu();
/// <summary>
/// 创建超市对象的时候,给仓库的货架上导入货物
/// </summary>
public SupperMarket()
{
ck.JinPros("Acer", 1000);
ck.JinPros("SamSung", 1000);
ck.JinPros("JiangYou", 1000);
ck.JinPros("Banana", 1000);
}
/// <summary>
/// 跟用户交互的过程
/// </summary>
public void AskBuying()
{
Console.WriteLine("欢迎观临,请问您需要些什么?");
Console.WriteLine("我们有 Acer、SamSung、Jiangyou、Banana");
string strType = Console.ReadLine();
Console.WriteLine("您需要多少?");
int count = Convert.ToInt32(Console.ReadLine());
//去仓库取货物
ProductFather[] pros = ck.QuPros(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(300, 50);
break;
case "5": cal = new CalMN(500, 100);
break;
}
return cal;
}
/// <summary>
/// 根据用户买的货物计算总价钱
/// </summary>
/// <param name="pros"></param>
/// <returns></returns>
public double GetMoney(ProductFather[] pros)
{
double realMoney = 0;
//realMoney = pros[0].Price * pros.Length;
for (int i = 0; i < pros.Length; i++)
{
realMoney += pros[i].Price;
// realMoney = pros[i] * pros.Length;
}
return realMoney;
}
public void ShowPros()
{
ck.ShowPros();
}
}
}