【C#学习记录】九、面向对象概念5(综合训练)
超市收银系统
商品类
- 父类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class ProductFather
{
private double _price;
private string _name;
private string _id;
public double Price { get => _price; set => _price = value; }
public string ID { get => _id; set => _id = value; }
public string Name { get => _name; set => _name = value; }
public ProductFather(string id, double price, string name)
{
this.ID = id;
this.Price = price;
this.Name = name;
}
}
}
- 子类:
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)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class Sanxing : ProductFather
{
public Sanxing(string id, double price, string name) : base(id, price, name)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class JangYou : ProductFather
{
public JangYou(string id, double price, string name) : base(id, price, name)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class Banana : ProductFather
{
public Banana(string id, double price, string name) : base(id, price, name)
{
}
}
}
仓库类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class CangKu
{
//存储货物
//List<ProductFather> list = new List<ProductFather> { };
List<List<ProductFather>> lists = new List<List<ProductFather>> { };
/// <summary>
/// 再创建仓库对象的时候,向仓库中添加货架
/// </summary>
public CangKu()
{
//List[0]存储Acer电脑,1 手机 2 酱油 3 香蕉
lists.Add(new List<ProductFather>());
lists.Add(new List<ProductFather>());
lists.Add(new List<ProductFather>());
lists.Add(new List<ProductFather>());
}
/// <summary>
/// 进货
/// </summary>
/// <param name="strType">货物的类型</param>
/// <param name="count">货物的数量</param>
public void GetPros(string strType, int count)
{
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "Acer": lists[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏基笔记本"));
break;
case "Sanxing": lists[1].Add(new Sanxing(Guid.NewGuid().ToString(), 800, "三星手机"));
break;
case "JangYou": lists[2].Add(new JangYou(Guid.NewGuid().ToString(), 5, "酱油"));
break;
case "Banana": lists[3].Add(new Banana(Guid.NewGuid().ToString(), 2, "香蕉"));
break;
default:
break;
}
}
}
/// <summary>
/// 从仓库里取货
/// </summary>
/// <param name="strType">货物的类型</param>
/// <param name="count">取货的个数</param>
/// <returns></returns>
public ProductFather[] SendPros(string strType, int count)
{
//List<ProductFather> pros = new List<ProductFather> { };
ProductFather[] pros = new ProductFather[count];
for (int i = 0; i < count; i++)
{
switch (strType)
{
case "Acer":
try
{
pros[i] = lists[0][0];
lists[0].RemoveAt(0);
}
catch
{
throw;
}
break;
case "Sanxing":
try
{
pros[i] = lists[1][0];
lists[1].RemoveAt(0);
}
catch
{
throw;
}
break;
case "JangYou":
try
{
pros[i] = lists[2][0];
lists[2].RemoveAt(0);
}
catch
{
throw;
}
break;
case "Banana":
try
{
pros[i] = lists[3][0];
lists[3].RemoveAt(0);
}
catch
{
throw;
}
break;
default:
break;
}
}
return pros;
}
public void ShowPros()
{
foreach (var item in lists)
{
Console.WriteLine("我们仓库有:"+item[0].Name+",\t有"+item.Count+"个,\t每个"+item[0].Price+"元");
}
}
}
}
折扣类
- 父类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal abstract class CalFather
{
/// <summary>
/// 打折
/// </summary>
/// <param name="sumPrice">原来的总价格</param>
/// <returns>打折后的价格</returns>
public abstract double GetTotalMoney(double sumPrice);
}
}
- 子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class CalNormal : CalFather
{
public override double GetTotalMoney(double sumPrice)
{
return sumPrice;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
/// <summary>
/// 按折扣率打折
/// </summary>
internal class CalRate : CalFather
{
private double _rate;
public double Rate { get => _rate; set => _rate = value; }
public override double GetTotalMoney(double sumPrice)
{
return sumPrice * this.Rate;
}
public CalRate(double rate)
{
this.Rate = rate;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
/// <summary>
/// 买M元送N元
/// </summary>
internal class CalMN : CalFather
{
private double _m;
private double _n;
public double M { get => _m; set => _m = value; }
public double N { get => _n; set => _n = value; }
public CalMN(double m,double n)
{
this.M = m;
this.N = n;
}
public override double GetTotalMoney(double sumPrice)
{
if (sumPrice >= this.M)
{
return sumPrice - (int)(sumPrice / this.M) * this.N;
}
else
{
return sumPrice;
}
}
}
}
超市收银类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 超市收银系统
{
internal class Suppermaket
{
//创建仓库对象
CangKu ck = new CangKu();
/// <summary>
/// 创建超市对象的时候,给仓库的货架上添加货物
/// </summary>
public Suppermaket()
{
ck.GetPros("Acer",10);
ck.GetPros("Sanxing",100);
ck.GetPros("JangYou",50);
ck.GetPros("Banana",1000);
}
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.SendPros(strType,count);
//计算价格
double price = CountPrice(pros);
Console.WriteLine("您总共应付{0}元",price);
//打折
Console.WriteLine("请选择您的打折方式 1--不打折 2--打九折 3--打85折 4--买300送50 5--买500送100");
CalFather cf = GetCal(Console.ReadLine());
Console.WriteLine("您应该付{0}",cf.GetTotalMoney(price));
}
/// <summary>
/// 计算商品的价格
/// </summary>
/// <param name="pros"></param>
/// <returns></returns>
public double CountPrice(ProductFather[] pros)
{
double sumPrice = 0;
foreach (var item in pros)
{
sumPrice = sumPrice + item.Price;
}
return sumPrice;
}
public CalFather GetCal(string input)
{
CalFather cal = null;
switch (Convert.ToInt32(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;
default:
break;
}
return cal;
}
public void ShowPros()
{
ck.ShowPros();
}
}
}
主函数
using 超市收银系统;
Suppermaket sm = new Suppermaket();
//展示货物
sm.ShowPros();
//跟用户交互
sm.AskBuying();
Console.ReadKey();
本文来自博客园,作者:翻兽,转载请注明原文链接:https://www.cnblogs.com/xuzhq/p/15980610.html