这里主要是将代码分享给大家
主体代码
program.cs
using 超市收银系统;
namespace _超市收银系统
{
class Program
{
static void Main(string[] args)
{
//创建超市对象
SuperMarket sm = new SuperMarket();
//展示货物
sm.ShowPros();
//跟用户交互
sm.AskBuying();
Console.ReadKey();
}
}
}
商品主体父类
ProductFather
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
class ProductFather
{
public double Price
{
get;
set;
}
public string Name
{
get;
set;
}
public double Count
{
get;
set;
}
public string ID
{
set;
get;
}
public ProductFather(string id,double price, string name)
{
this.ID = id;
this.Price = price;
this.Name = name;
}
}
}
子类
Acre.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class Acer:ProductFather
{
public Acer(String id, double price, string name) : base(id, price, name)
{
}
}
}
SamSung.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
class SamSung:ProductFather
{
public SamSung(String id,double price,string name):base(id,price,name)
{
}
}
}
Fruit.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class Fruit:ProductFather
{
public Fruit(String id, double price, string name) : base(id, price, name)
{
}
}
}
LaoChou.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class LaoChou:ProductFather
{
public LaoChou(String id, double price, string name) : base(id, price, name)
{
}
}
}
仓库主体
CangKu.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal 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]SumSung
//list[2]Fruit
//lsit[3]LaoChou
/// <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 "Fruit": list[2].Add(new Fruit(Guid.NewGuid().ToString(), 10, "水果"));
break;
case "LaoChou": list[3].Add(new LaoChou(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 < count;i++)
{
switch (strType)
{
case "Acer":
if (list[0].Count == 0)
{
Console.WriteLine("宏基笔记本电脑库存不足");
}
else
{
pros[i] = list[0][0];//list[0]指Acer这个货架再加一个[]便成为了父类数组
list[0].RemoveAt(0);
}
break;
case "SamSung":
if (list[1].Count == 0)
{
Console.WriteLine("三星笔记本电脑库存不足");
}
else
{
pros[i] = list[1][0];//这是将货架上的货物号号绑定在pros[i]上,并没有移除货架上的货物
list[1].RemoveAt(0);//这个才是真正的一处货物
}
break;
case "Fruit":
if (list[2].Count == 0)
{
Console.WriteLine("水果库存不足");
}
else
{
pros[i] = list[2][0];
list[2].RemoveAt(0);
}
break;
case "LaoChou":
if (list[3].Count == 0)
{
Console.WriteLine("老抽库存不足");
}
else
{
pros[i] = list[3][0];
list[3].RemoveAt(0);
}
break;
}
}
return pros;
}
//存储货物
//List<SamSung> listSam = new List<SamSung>();
//List<Acer> listAcer = new List<Acer>();
//List<Fruit> listFruit = new List<Fruit>();
//List<LaoChou> listLaoChou = new List<LaoChou>();
}
}
收银主体
SuperMarket.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class SuperMarket
{
//创建仓库对象
CangKu ck = new CangKu();
/// <summary>
/// 创建超市对象的时候,给仓库的货架上导入货物
/// </summary>
public SuperMarket()
{
ck.JinPros("Acer", 1000);
ck.JinPros("SamSung", 1000);
ck.JinPros("Fruit", 1000);
ck.JinPros("LaoChou", 1000);
}
/// <summary>
/// 跟用户交互的过程
/// </summary>
public void AskBuying()
{
Console.WriteLine("欢迎光临,请问您需要些什么?");
Console.WriteLine("我们有Acer、SamSung、Fruit、LaoChou");
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、打九折 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+"\t");
}
}
/// <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.95);
break;
case "3":cal = new CalRate(0.9);
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();
}
}
}
打折父类
CalFather.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
/// <summary>
/// 打折的父类
/// </summary>
abstract class CalFather
{
/// <summary>
/// 计算打折后应付多少钱
/// </summary>
/// <param name="realMoney">打折前应付的钱</param>
/// <returns>打折后应付的钱</returns>
public abstract double GetTotalMoney(double realMoney);
}
}
子类
CalNormal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
/// <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 超市收银系统
{
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.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
class CalMN:CalFather
{
public double M
{
get;
set;
}
public double N
{
get;
set;
}
public CalMN(double m, double n)
{
this.M = m;
this.N = n;
}
/// <summary>
/// 买M送N
/// </summary>
public override double GetTotalMoney(double realMoney)
{
//
if(realMoney >=this.M)
{
return realMoney - (int)(realMoney/this.M)*this.N;
}
else
{
return realMoney;
}
}
}
}
实现结果显示
我们超市有:宏基笔记本, 有1000个, 每个1000元
我们超市有:三星之手机, 有1000个, 每个2000元
我们超市有:水果, 有1000个, 每个10元
我们超市有:老抽, 有1000个, 每个50元
欢迎光临,请问您需要些什么?
我们有Acer、SamSung、Fruit、LaoChou
Fruit
您需要多少?
50
您总共应付500元
今天的打折方式:1、不打折 2、打九五折 3、打九折 4、买300送50 5、买500送100
4
打完折后,您应付450元
以下是您的购物清单
货物名称水果, 货物单价:10, 货物编号:4cd85496-34ce-4677-a017-f56e24909bc5
货物名称水果, 货物单价:10, 货物编号:e2c22db1-987f-4077-bf52-8aa43f19da60
货物名称水果, 货物单价:10, 货物编号:6d482de2-3d4a-4e72-90b9-f0c470c767b7
货物名称水果, 货物单价:10, 货物编号:aa2ec3ec-7872-4c19-af56-ffed5375c4e5
货物名称水果, 货物单价:10, 货物编号:31abd539-4f67-41c7-9013-93b286d8bd19
货物名称水果, 货物单价:10, 货物编号:9130d232-1622-4360-a73b-ef92a9d69e0c
货物名称水果, 货物单价:10, 货物编号:b40d3c49-a984-4f2e-bb0b-7d67a42da275
货物名称水果, 货物单价:10, 货物编号:acb546e9-4917-4af9-8680-1ff771eb2cdf
货物名称水果, 货物单价:10, 货物编号:0d06dd50-0359-484b-85bb-00319b07614d
货物名称水果, 货物单价:10, 货物编号:cf5bc71f-96de-4f11-9215-ccc942d2d521
货物名称水果, 货物单价:10, 货物编号:36cddcb5-efa4-49c2-a979-9fc456bb4e55
货物名称水果, 货物单价:10, 货物编号:9829ca4a-b6fa-433d-a21a-a38746769c0c
货物名称水果, 货物单价:10, 货物编号:3ea74e27-4857-4aa0-912d-530c49b0d6e2
货物名称水果, 货物单价:10, 货物编号:d9141250-33b1-4078-86dc-80a0e08170a0
货物名称水果, 货物单价:10, 货物编号:796f3fd5-e262-49cf-9be2-1bb464c467a1
货物名称水果, 货物单价:10, 货物编号:9f26329e-e8cc-4be6-a60b-f69508c6df0f
货物名称水果, 货物单价:10, 货物编号:4a12940d-d56c-4d9c-9050-264094363339
货物名称水果, 货物单价:10, 货物编号:bac71cbc-b7ad-4b4a-b1b7-fd1d39643beb
货物名称水果, 货物单价:10, 货物编号:8d821a00-afc9-459a-ba8d-d3388856c785
货物名称水果, 货物单价:10, 货物编号:3a47bceb-7de4-4792-83a6-dafab7cab301
货物名称水果, 货物单价:10, 货物编号:81555aaa-41dc-4ee0-bf03-ab0b19a2ce1f
货物名称水果, 货物单价:10, 货物编号:ce00cb1b-913c-42c7-8905-1347fc5c6576
货物名称水果, 货物单价:10, 货物编号:88eaec1f-9208-4478-a5fd-a25395135cee
货物名称水果, 货物单价:10, 货物编号:699f6431-4210-452f-973d-7450aaa0ba0c
货物名称水果, 货物单价:10, 货物编号:b15290b5-1e1b-4df3-9f0f-06ba29d2bb89
货物名称水果, 货物单价:10, 货物编号:3c56d281-a1cb-4341-83ab-d4e9916a95b6
货物名称水果, 货物单价:10, 货物编号:78b1d2fb-a384-44e0-88c8-d7c06150cef7
货物名称水果, 货物单价:10, 货物编号:65e07fe3-3c7c-427f-a834-839493a04df9
货物名称水果, 货物单价:10, 货物编号:a97e14c0-e1d0-443b-853c-b86aeeeaa244
货物名称水果, 货物单价:10, 货物编号:8c671c15-d4da-4db9-94d5-f85840fa70e4
货物名称水果, 货物单价:10, 货物编号:2399ef10-7236-419c-a988-c57a7e23345d
货物名称水果, 货物单价:10, 货物编号:abd590c3-967b-4e1e-ac09-eb2cb9f3c3b5
货物名称水果, 货物单价:10, 货物编号:c088ddaa-7b3b-4a43-80c2-d65342074e78
货物名称水果, 货物单价:10, 货物编号:7540d5d4-8026-466b-b932-229c3625ba0b
货物名称水果, 货物单价:10, 货物编号:5b2a3393-7064-48ef-ab59-cc7250838522
货物名称水果, 货物单价:10, 货物编号:25321703-f67d-45f9-9535-87b7018cabf1
货物名称水果, 货物单价:10, 货物编号:b996869f-10b8-48f2-8c42-e29ca33a14eb
货物名称水果, 货物单价:10, 货物编号:7ed79f58-c7aa-435b-aa8f-fc5017356812
货物名称水果, 货物单价:10, 货物编号:a100a15d-81a1-426a-bfac-24b644575aed
货物名称水果, 货物单价:10, 货物编号:7d86a7e4-a0c9-4a0d-a070-2dff5a191703
货物名称水果, 货物单价:10, 货物编号:3b2c3af1-4af2-499b-ad69-f19ae12e5851
货物名称水果, 货物单价:10, 货物编号:1b839af1-ac59-42a5-b507-2d593ff249e3
货物名称水果, 货物单价:10, 货物编号:f8a468e4-1cb9-4306-99e0-ba29875bee2f
货物名称水果, 货物单价:10, 货物编号:9b9d6093-8bd7-4e97-b625-1a370eb17070
货物名称水果, 货物单价:10, 货物编号:69563bb5-08ea-44f2-b7dc-1f2726c38404
货物名称水果, 货物单价:10, 货物编号:c9c44081-251d-42b2-86fb-c9b4eb101d1a
货物名称水果, 货物单价:10, 货物编号:a0a03ea8-bf92-4e27-aec7-954f89095787
货物名称水果, 货物单价:10, 货物编号:b7dd3c16-8abd-4b8d-8fa5-366f73dccf8d
货物名称水果, 货物单价:10, 货物编号:f734d4e7-3180-4a4e-bef6-673e1ec6daea
货物名称水果, 货物单价:10, 货物编号:41db2963-a501-47c5-b9b8-da74e70268b2