仓库类
using System;
namespace _097_超市收银系统
{
class Program
{
static void Main(string[] args)
{
//创建超市对象
Supermarket sm = new Supermarket();
//展示货物
sm.ShowGoods();
//和用户交互
sm.AskBuying();
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class Warehouse
{
//存储货物
//List<ProductFather> list1 = new List<ProductFather>();
//List<list1> list2 = new List<list1>();
List<List<ProductFather>> list = new List<List<ProductFather>>();//(创建商品货架??)
//list[0] 储存...
//list[1] 储存...
//list[2] 储存...
//list[3] 储存...
/// <summary>
/// 在创建仓库对象的时候,向仓库中添加货架
/// </summary>
public Warehouse()
{
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
list.Add(new List<ProductFather>());
}
/// <summary>
/// 进货
/// </summary>
/// <param name="goodsType">货物的类型</param>
/// <param name="count">货物的数量</param>
public void PurchaseGoods(string goodsType, int count)
{
for (int i = 0; i < count; i++)
{
switch (goodsType)
{
case "Acer":
list[0].Add(new Acer(5500, "鸿基笔记本电脑", Guid.NewGuid().ToString()));
break;
case "SamsungMobilePhone":
list[1].Add(new SamsungMobilePhone(3299, "三星手机", Guid.NewGuid().ToString()));
break;
case "Banana":
list[2].Add(new Banana(1.39, "香蕉", Guid.NewGuid().ToString()));
break;
case "Salt":
list[3].Add(new Salt(1.8, "食盐", Guid.NewGuid().ToString()));
break;
default:
break;
}
}
}
/// <summary>
/// 从仓库中提取货物
/// </summary>
/// <param name="goodsType">货物类型</param>
/// <param name="count">货物数量</param>
/// <returns>货物数组</returns>
public ProductFather[] FetchGoods(string goodsType, int count)
{
ProductFather[] productFathersArray = new ProductFather[count];
for (int i = 0; i < productFathersArray.Length; i++) //productFathersArray.Length = count;
{
switch (goodsType)
{
case "Acer":
if (list[0].Count == 0)
{
Console.WriteLine("鸿基笔记本电脑没有存货了,请尽快进货");
}
productFathersArray[i] = list[0][0]; //从货架拿走(RemoveAt())第一个商品放入productFathersArray中
list[0].RemoveAt(0); //拿走货物 (移除,这样list[0][]又是从0开始)
break;
case "SamsungMobilePhone":
if (list[1].Count == 0)
{
Console.WriteLine("三星手机没有存货了,请尽快进货");
}
productFathersArray[i] = list[1][0];
list[1].RemoveAt(0);
break;
case "Banana":
if (list[2].Count == 0)
{
Console.WriteLine("香蕉没有存货了,请尽快进货");
}
productFathersArray[i] = list[2][0];
list[2].RemoveAt(0);
break;
case "Salt":
if (list[3].Count == 0)
{
Console.WriteLine("食盐没有存货了,请尽快进货");
}
productFathersArray[i] = list[3][0];
list[3].RemoveAt(0);
break;
default:
break;
}
}
return productFathersArray;
}
/// <summary>
/// 向用户展示仓库货物
/// </summary>
public void ShowGoods()
{
Console.WriteLine("我们仓库有:");
Console.WriteLine("商品名称\t数量\t单价");
foreach (var item in list)
{
Console.WriteLine("{0}\t{1}\t{2}\t",item[0].Name,item.Count,item[0].Price);
//Console.WriteLine(item[0].Name + "\t"+ item.Count + "\t" + item[0].Price + "元");
//Console.WriteLine(item.Count);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class Supermarket
{
//创建仓库对象
Warehouse wh = new Warehouse();
/// <summary>
/// 创建超市对象的时候,给仓库的货架上导入货物
/// </summary>
public Supermarket()
{
wh.PurchaseGoods("Acer", 1000);
wh.PurchaseGoods("SamsungMobilePhone", 1000);
wh.PurchaseGoods("Banana", 1000);
wh.PurchaseGoods("Salt", 1000);
}
/// <summary>
/// 和用户交互的过程
/// </summary>
public void AskBuying()
{
Console.WriteLine("欢迎管理 微咲人工小智能 的小超市~~");
Console.WriteLine("微咲人工小智能 的小超市有 Acer、SansungMobilePhone、Banana、Salt");
Console.WriteLine("请问您需要什么?");
string goodsType = Console.ReadLine();
//判断用户输入的是否为商品,若不是则重新输入
while (true)
{
if (goodsType == "Acer" || goodsType == "SumsungMobilePhone" || goodsType == "Banana" || goodsType == "Salt")
{
break;
}
else
{
Console.WriteLine("您输入的不是商品名称请重新输入:");
goodsType = Console.ReadLine();
}
}
Console.WriteLine("请问你需要多少?");
int count = Convert.ToInt32(Console.ReadLine());
//去仓库取货
ProductFather[] productFathersArray = wh.FetchGoods(goodsType, count);
////询问用户是否还需要其他货物
//while (goodsType != "不需要了")
//{
// Console.WriteLine("请问还需要其他货物吗?如果不需要请回复<不需要了>");
// goodsType = Console.ReadLine();
// if (goodsType=="不需要了")
// {
// break;
// }
// Console.WriteLine("请问你需要多少?");
// count = Convert.ToInt32(Console.ReadLine());
// //去仓库取货
// productFathersArray = wh.FetchGoods(goodsType, count);
//}
//计算价钱
double sumMomey = GetMoney(productFathersArray);
Console.WriteLine("您总共消费{0}元,我们店铺有以下打折方式", sumMomey);
Console.WriteLine("↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓");
Console.WriteLine("请选择你的打折方式 1---不打折");
Console.WriteLine(" 2---打九折");
Console.WriteLine(" 3---打八五折");
Console.WriteLine(" 4---满三百减五十");
Console.WriteLine(" 5---满五百减一百");
string input = Console.ReadLine();
//通过简单工厂的设计模式根据用户的输入获得一个打折对象
DiscountFather discount = GetDiscount(input);
double totalMoney = discount.GetTotalMoney(sumMomey);
Console.WriteLine("现在你的总消费为:{0}", totalMoney);
Console.WriteLine("小票:");
ShoppingList(productFathersArray, count, sumMomey, totalMoney);
}
/// <summary>
/// 根据用户买的货物,计算总价钱
/// </summary>
/// <param name="productFathersArray"></param>
/// <returns></returns>
public double GetMoney(ProductFather[] productFathersArray)
{
double sumMoney = 0;
for (int i = 0; i < productFathersArray.Length; i++)
{
sumMoney += productFathersArray[i].Price;
}
return sumMoney;
}
/// <summary>
/// 根据用户的选择打折方式返回一个打折对象
/// </summary>
/// <param name="input">用户的选择</param>
/// <returns>返回的父类对象,但是里面装的是子类对象</returns>
public DiscountFather GetDiscount(string input)
{
DiscountFather discount = null;
bool flag = false;
while (true)
{
switch (input)
{
case "1":
discount = new DiscountNormal(); flag = true;
break;
case "2":
discount = new DiscountRate(0.9); flag = true;
break;
case "3":
discount = new DiscountRate(0.85); flag = true;
break;
case "4":
discount = new DiscountFull(300, 50); flag = true;
break;
case "5":
discount = new DiscountFull(500, 100); flag = true;
break;
default:
Console.WriteLine("输入错误,请重新输入:");
input = Console.ReadLine();
break;
}
if (flag == true)
{
break;
}
}
return discount;
}
/// <summary>
/// 调用仓库类里的ShowGoods()方法
/// </summary>
public void ShowGoods()
{
wh.ShowGoods();
}
public void ShoppingList(ProductFather[] productFathersArray, int count, double sumMoney, double totalMoney)
{
Console.WriteLine("货物名称\t\t货物编号\t\t货物单价\t\t货物数量\t\t总价\t\t打完折后的价格\t");
foreach (var item in productFathersArray)
{
Console.WriteLine(item.Name + '\t' + item.ID + '\t' + item.Price + '\t' + count + '\t' + sumMoney + '\t' + totalMoney + '\t');
}
}
}
}
商品类
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class ProductFather
{
private double price;
public double Price { get => price; set => price = value; }
private string name;
public string Name { get => name; set => name = value; }
private string iD;
public string ID { get => iD; set => iD = value; }
public ProductFather(double price, string name, string id)
{
this.Price = price;
this.Name = name;
this.ID = id;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class Acer : ProductFather
{
public Acer(double price, string name, string id) : base(price, name, id)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class Banana : ProductFather
{
public Banana(double price, string name, string id) : base(price, name, id)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class Salt : ProductFather
{
public Salt(double price, string name, string id) : base(price, name, id)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
class SamsungMobilePhone : ProductFather
{
public SamsungMobilePhone(double price, string name, string id) : base(price, name, id)
{
}
}
}
打折类
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
/// <summary>
/// “打折”的“父类”
/// </summary>
abstract class DiscountFather
{
/// <summary>
/// 计算打折后应付多少钱
/// </summary>
/// <param name="sumMonye">打折前应该付的价钱</param>
/// <returns>打折后应该付的价钱</returns>
public abstract double GetTotalMoney(double sumMonye);
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
/// <summary>
/// 不打折
/// </summary>
class DiscountNormal:DiscountFather
{
/// <summary>
/// 不打折
/// </summary>
/// <param name="sumMonye"></param>
/// <returns></returns>
public override double GetTotalMoney(double sumMonye)
{
return sumMonye;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
/// <summary>
/// 折扣率
/// </summary>
class DiscountRate : DiscountFather
{
/// <summary>
/// 折扣率
/// </summary>
public double Rate { get; set; }
/// <summary>
/// 创建对象实例时传入打折率
/// </summary>
/// <param name="rate">打折率</param>
public DiscountRate(double rate)
{
this.Rate = rate;
}
public override double GetTotalMoney(double sumMonye)
{
return sumMonye * Rate;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _097_超市收银系统
{
/// <summary>
/// 满减
/// </summary>
class DiscountFull : DiscountFather
{
public double SufficientMoney { get; set; }
public double DiscountMoney { get; set; }
/// <summary>
/// 创建对象实例时传入满减需要满足的金额,满减优惠
/// </summary>
/// <param name="sufficientMoney">满减需要满足的金额</param>
/// <param name="discountMoney">满减优惠</param>
public DiscountFull(double sufficientMoney, double discountMoney)
{
this.SufficientMoney = sufficientMoney;
this.DiscountMoney = discountMoney;
}
public override double GetTotalMoney(double sumMonye)
{
if (sumMonye >= this.SufficientMoney)
{
//比如满减是500-100,这里实现600-100,1000-200
return sumMonye - (int)(sumMonye / this.SufficientMoney) * this.DiscountMoney;
}
else
{
return sumMonye;
}
}
}
}