项目–练习题61:超市收银系统
ProductFather:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
class ProductFather
{
public string ID
{
get;
set;
}
public double Price
{
get;
set;
}
public string Name
{
get;
set;
}
public ProductFather(string id, double price, string name)
{
this.ID = id;
this.Price = price;
this.Name = name;
}
}
}
Acer:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
class Acer : ProductFather
{
public Acer(string id, double price, string name) : base(id, price, name)
{
}
}
}
SamSung:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
class SamSung : ProductFather
{
public SamSung(string id, double price, string name) : base(id, price, name)
{
}
}
}
JiangYou:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
class JiangYou : ProductFather
{
public JiangYou(string id, double price, string name) : base(id, price, name)
{
}
}
}
Banana:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
class Banana : ProductFather
{
public Banana(string id, double price, string name) : base(id, price, name)
{
}
}
}
CalFather:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
//多态--抽象类
/// <summary>
/// 打折的父类
/// </summary>
abstract class CalFather
{
/// <summary>
/// 计算打折后应付多少钱
/// </summary>
/// <param name="realMoney">打折前应付的价钱</param>
/// <returns>打折后应付的价钱</returns>
public abstract double GetTotalMoney(double realMoney);
}
}
CalNormal:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
/// <summary>
/// 不打折 该多少钱就多少钱
/// </summary>
class CalNormal : CalFather
{
public override double GetTotalMoney(double realMoney)
{
return realMoney;
}
}
}
CalRate:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
/// <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;
}
}
}
CalMN:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
/// <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
if (realMoney >= this.M)
return realMoney - (int)(realMoney / this.M) * this.N;
else
return realMoney;
}
}
}
CangKu:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
class CangKu
{
/// <summary>
/// 存储货物
/// </summary>
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]存储Acer电脑
//List[2]存储Acer电脑
//List[3]存储Acer电脑
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;
}
}
}
//public ProductFather[] QuPros(string strType, int count)
//{
// ProductFather[] pros = new ProductFather[count];
// for (int i = 0; i < count; 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;
//}
//public ProductFather[] QuPros(string strType, int count)
//{
// ProductFather[] pros = new ProductFather[count];
// for (int i = 0; i < count; i++)
// {
// switch (strType)
// {
// case "Acer":
// if (list[0].Count == 0)
// {
// Console.WriteLine("该货架没有货物,程序异常退出");
// Environment.Exit(0);
// }
// pros[i] = list[0][0];
// list[0].RemoveAt(0);
// break;
// case "SamSung":
// if (list[1].Count == 0)
// {
// Console.WriteLine("该货架没有货物,程序异常退出");
// Environment.Exit(0);
// }
// pros[i] = list[1][0];
// list[1].RemoveAt(0);
// break;
// case "JiangYou":
// if (list[2].Count == 0)
// {
// Console.WriteLine("该货架没有货物,程序异常退出");
// Environment.Exit(0);
// }
// pros[i] = list[2][0];
// list[2].RemoveAt(0);
// break;
// case "Banana":
// if (list[3].Count == 0)
// {
// Console.WriteLine("该货架没有货物,程序异常退出");
// Environment.Exit(0);
// }
// pros[i] = list[3][0];
// list[3].RemoveAt(0);
// break;
// }
// }
// return pros;
//}
public ProductFather[] QuPros(string strType, int count)
{
ProductFather[] pros = new ProductFather[count];
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "Acer":
try
{
pros[i] = list[0][0];
list[0].RemoveAt(0);
}
catch
{
Console.WriteLine("该货架没有货物,程序异常退出");
Environment.Exit(0);
}
break;
case "SamSung":
try
{
pros[i] = list[1][0];
list[1].RemoveAt(0);
}
catch
{
Console.WriteLine("该货架没有货物,程序异常退出");
Environment.Exit(0);
}
break;
case "JiangYou":
try
{
pros[i] = list[2][0];
list[2].RemoveAt(0);
}
catch
{
Console.WriteLine("该货架没有货物,程序异常退出");
Environment.Exit(0);
}
break;
case "Banana":
try
{
pros[i] = list[3][0];
list[3].RemoveAt(0);
}
catch
{
Console.WriteLine("该货架没有货物,程序异常退出");
Environment.Exit(0);
}
break;
}
}
return pros;
}
}
}
SupperMarket:
using System;
using System.Collections.Generic;
using System.Text;
namespace 超市收银系统
{
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;
for (int i = 0; i < pros.Length; i++)
{
realMoney += pros[i].Price;
}
return realMoney;
}
public void ShowPros()
{
ck.ShowPros();
}
}
}
Program:
using System;
namespace 超市收银系统
{
class Program
{
static void Main(string[] args)
{
//创建超市对象
SupperMarket sm = new SupperMarket();
//展示货物
sm.ShowPros();
//跟用户交互
sm.AskBuying();
Console.ReadKey();
}
}
}