image.png

仓库类

image.png

  1. using System;
  2. namespace _097_超市收银系统
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. //创建超市对象
  9. Supermarket sm = new Supermarket();
  10. //展示货物
  11. sm.ShowGoods();
  12. //和用户交互
  13. sm.AskBuying();
  14. Console.ReadKey();
  15. }
  16. }
  17. }
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Text;
  21. namespace _097_超市收银系统
  22. {
  23. class Warehouse
  24. {
  25. //存储货物
  26. //List<ProductFather> list1 = new List<ProductFather>();
  27. //List<list1> list2 = new List<list1>();
  28. List<List<ProductFather>> list = new List<List<ProductFather>>();//(创建商品货架??)
  29. //list[0] 储存...
  30. //list[1] 储存...
  31. //list[2] 储存...
  32. //list[3] 储存...
  33. /// <summary>
  34. /// 在创建仓库对象的时候,向仓库中添加货架
  35. /// </summary>
  36. public Warehouse()
  37. {
  38. list.Add(new List<ProductFather>());
  39. list.Add(new List<ProductFather>());
  40. list.Add(new List<ProductFather>());
  41. list.Add(new List<ProductFather>());
  42. }
  43. /// <summary>
  44. /// 进货
  45. /// </summary>
  46. /// <param name="goodsType">货物的类型</param>
  47. /// <param name="count">货物的数量</param>
  48. public void PurchaseGoods(string goodsType, int count)
  49. {
  50. for (int i = 0; i < count; i++)
  51. {
  52. switch (goodsType)
  53. {
  54. case "Acer":
  55. list[0].Add(new Acer(5500, "鸿基笔记本电脑", Guid.NewGuid().ToString()));
  56. break;
  57. case "SamsungMobilePhone":
  58. list[1].Add(new SamsungMobilePhone(3299, "三星手机", Guid.NewGuid().ToString()));
  59. break;
  60. case "Banana":
  61. list[2].Add(new Banana(1.39, "香蕉", Guid.NewGuid().ToString()));
  62. break;
  63. case "Salt":
  64. list[3].Add(new Salt(1.8, "食盐", Guid.NewGuid().ToString()));
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 从仓库中提取货物
  73. /// </summary>
  74. /// <param name="goodsType">货物类型</param>
  75. /// <param name="count">货物数量</param>
  76. /// <returns>货物数组</returns>
  77. public ProductFather[] FetchGoods(string goodsType, int count)
  78. {
  79. ProductFather[] productFathersArray = new ProductFather[count];
  80. for (int i = 0; i < productFathersArray.Length; i++) //productFathersArray.Length = count;
  81. {
  82. switch (goodsType)
  83. {
  84. case "Acer":
  85. if (list[0].Count == 0)
  86. {
  87. Console.WriteLine("鸿基笔记本电脑没有存货了,请尽快进货");
  88. }
  89. productFathersArray[i] = list[0][0]; //从货架拿走(RemoveAt())第一个商品放入productFathersArray中
  90. list[0].RemoveAt(0); //拿走货物 (移除,这样list[0][]又是从0开始)
  91. break;
  92. case "SamsungMobilePhone":
  93. if (list[1].Count == 0)
  94. {
  95. Console.WriteLine("三星手机没有存货了,请尽快进货");
  96. }
  97. productFathersArray[i] = list[1][0];
  98. list[1].RemoveAt(0);
  99. break;
  100. case "Banana":
  101. if (list[2].Count == 0)
  102. {
  103. Console.WriteLine("香蕉没有存货了,请尽快进货");
  104. }
  105. productFathersArray[i] = list[2][0];
  106. list[2].RemoveAt(0);
  107. break;
  108. case "Salt":
  109. if (list[3].Count == 0)
  110. {
  111. Console.WriteLine("食盐没有存货了,请尽快进货");
  112. }
  113. productFathersArray[i] = list[3][0];
  114. list[3].RemoveAt(0);
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. return productFathersArray;
  121. }
  122. /// <summary>
  123. /// 向用户展示仓库货物
  124. /// </summary>
  125. public void ShowGoods()
  126. {
  127. Console.WriteLine("我们仓库有:");
  128. Console.WriteLine("商品名称\t数量\t单价");
  129. foreach (var item in list)
  130. {
  131. Console.WriteLine("{0}\t{1}\t{2}\t",item[0].Name,item.Count,item[0].Price);
  132. //Console.WriteLine(item[0].Name + "\t"+ item.Count + "\t" + item[0].Price + "元");
  133. //Console.WriteLine(item.Count);
  134. }
  135. }
  136. }
  137. }
  138. using System;
  139. using System.Collections.Generic;
  140. using System.Text;
  141. namespace _097_超市收银系统
  142. {
  143. class Supermarket
  144. {
  145. //创建仓库对象
  146. Warehouse wh = new Warehouse();
  147. /// <summary>
  148. /// 创建超市对象的时候,给仓库的货架上导入货物
  149. /// </summary>
  150. public Supermarket()
  151. {
  152. wh.PurchaseGoods("Acer", 1000);
  153. wh.PurchaseGoods("SamsungMobilePhone", 1000);
  154. wh.PurchaseGoods("Banana", 1000);
  155. wh.PurchaseGoods("Salt", 1000);
  156. }
  157. /// <summary>
  158. /// 和用户交互的过程
  159. /// </summary>
  160. public void AskBuying()
  161. {
  162. Console.WriteLine("欢迎管理 微咲人工小智能 的小超市~~");
  163. Console.WriteLine("微咲人工小智能 的小超市有 Acer、SansungMobilePhone、Banana、Salt");
  164. Console.WriteLine("请问您需要什么?");
  165. string goodsType = Console.ReadLine();
  166. //判断用户输入的是否为商品,若不是则重新输入
  167. while (true)
  168. {
  169. if (goodsType == "Acer" || goodsType == "SumsungMobilePhone" || goodsType == "Banana" || goodsType == "Salt")
  170. {
  171. break;
  172. }
  173. else
  174. {
  175. Console.WriteLine("您输入的不是商品名称请重新输入:");
  176. goodsType = Console.ReadLine();
  177. }
  178. }
  179. Console.WriteLine("请问你需要多少?");
  180. int count = Convert.ToInt32(Console.ReadLine());
  181. //去仓库取货
  182. ProductFather[] productFathersArray = wh.FetchGoods(goodsType, count);
  183. ////询问用户是否还需要其他货物
  184. //while (goodsType != "不需要了")
  185. //{
  186. // Console.WriteLine("请问还需要其他货物吗?如果不需要请回复<不需要了>");
  187. // goodsType = Console.ReadLine();
  188. // if (goodsType=="不需要了")
  189. // {
  190. // break;
  191. // }
  192. // Console.WriteLine("请问你需要多少?");
  193. // count = Convert.ToInt32(Console.ReadLine());
  194. // //去仓库取货
  195. // productFathersArray = wh.FetchGoods(goodsType, count);
  196. //}
  197. //计算价钱
  198. double sumMomey = GetMoney(productFathersArray);
  199. Console.WriteLine("您总共消费{0}元,我们店铺有以下打折方式", sumMomey);
  200. Console.WriteLine("↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓");
  201. Console.WriteLine("请选择你的打折方式 1---不打折");
  202. Console.WriteLine(" 2---打九折");
  203. Console.WriteLine(" 3---打八五折");
  204. Console.WriteLine(" 4---满三百减五十");
  205. Console.WriteLine(" 5---满五百减一百");
  206. string input = Console.ReadLine();
  207. //通过简单工厂的设计模式根据用户的输入获得一个打折对象
  208. DiscountFather discount = GetDiscount(input);
  209. double totalMoney = discount.GetTotalMoney(sumMomey);
  210. Console.WriteLine("现在你的总消费为:{0}", totalMoney);
  211. Console.WriteLine("小票:");
  212. ShoppingList(productFathersArray, count, sumMomey, totalMoney);
  213. }
  214. /// <summary>
  215. /// 根据用户买的货物,计算总价钱
  216. /// </summary>
  217. /// <param name="productFathersArray"></param>
  218. /// <returns></returns>
  219. public double GetMoney(ProductFather[] productFathersArray)
  220. {
  221. double sumMoney = 0;
  222. for (int i = 0; i < productFathersArray.Length; i++)
  223. {
  224. sumMoney += productFathersArray[i].Price;
  225. }
  226. return sumMoney;
  227. }
  228. /// <summary>
  229. /// 根据用户的选择打折方式返回一个打折对象
  230. /// </summary>
  231. /// <param name="input">用户的选择</param>
  232. /// <returns>返回的父类对象,但是里面装的是子类对象</returns>
  233. public DiscountFather GetDiscount(string input)
  234. {
  235. DiscountFather discount = null;
  236. bool flag = false;
  237. while (true)
  238. {
  239. switch (input)
  240. {
  241. case "1":
  242. discount = new DiscountNormal(); flag = true;
  243. break;
  244. case "2":
  245. discount = new DiscountRate(0.9); flag = true;
  246. break;
  247. case "3":
  248. discount = new DiscountRate(0.85); flag = true;
  249. break;
  250. case "4":
  251. discount = new DiscountFull(300, 50); flag = true;
  252. break;
  253. case "5":
  254. discount = new DiscountFull(500, 100); flag = true;
  255. break;
  256. default:
  257. Console.WriteLine("输入错误,请重新输入:");
  258. input = Console.ReadLine();
  259. break;
  260. }
  261. if (flag == true)
  262. {
  263. break;
  264. }
  265. }
  266. return discount;
  267. }
  268. /// <summary>
  269. /// 调用仓库类里的ShowGoods()方法
  270. /// </summary>
  271. public void ShowGoods()
  272. {
  273. wh.ShowGoods();
  274. }
  275. public void ShoppingList(ProductFather[] productFathersArray, int count, double sumMoney, double totalMoney)
  276. {
  277. Console.WriteLine("货物名称\t\t货物编号\t\t货物单价\t\t货物数量\t\t总价\t\t打完折后的价格\t");
  278. foreach (var item in productFathersArray)
  279. {
  280. Console.WriteLine(item.Name + '\t' + item.ID + '\t' + item.Price + '\t' + count + '\t' + sumMoney + '\t' + totalMoney + '\t');
  281. }
  282. }
  283. }
  284. }

商品类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace _097_超市收银系统
  5. {
  6. class ProductFather
  7. {
  8. private double price;
  9. public double Price { get => price; set => price = value; }
  10. private string name;
  11. public string Name { get => name; set => name = value; }
  12. private string iD;
  13. public string ID { get => iD; set => iD = value; }
  14. public ProductFather(double price, string name, string id)
  15. {
  16. this.Price = price;
  17. this.Name = name;
  18. this.ID = id;
  19. }
  20. }
  21. }
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Text;
  25. namespace _097_超市收银系统
  26. {
  27. class Acer : ProductFather
  28. {
  29. public Acer(double price, string name, string id) : base(price, name, id)
  30. {
  31. }
  32. }
  33. }
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Text;
  37. namespace _097_超市收银系统
  38. {
  39. class Banana : ProductFather
  40. {
  41. public Banana(double price, string name, string id) : base(price, name, id)
  42. {
  43. }
  44. }
  45. }
  46. using System;
  47. using System.Collections.Generic;
  48. using System.Text;
  49. namespace _097_超市收银系统
  50. {
  51. class Salt : ProductFather
  52. {
  53. public Salt(double price, string name, string id) : base(price, name, id)
  54. {
  55. }
  56. }
  57. }
  58. using System;
  59. using System.Collections.Generic;
  60. using System.Text;
  61. namespace _097_超市收银系统
  62. {
  63. class SamsungMobilePhone : ProductFather
  64. {
  65. public SamsungMobilePhone(double price, string name, string id) : base(price, name, id)
  66. {
  67. }
  68. }
  69. }

打折类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace _097_超市收银系统
  5. {
  6. /// <summary>
  7. /// “打折”的“父类”
  8. /// </summary>
  9. abstract class DiscountFather
  10. {
  11. /// <summary>
  12. /// 计算打折后应付多少钱
  13. /// </summary>
  14. /// <param name="sumMonye">打折前应该付的价钱</param>
  15. /// <returns>打折后应该付的价钱</returns>
  16. public abstract double GetTotalMoney(double sumMonye);
  17. }
  18. }
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Text;
  22. namespace _097_超市收银系统
  23. {
  24. /// <summary>
  25. /// 不打折
  26. /// </summary>
  27. class DiscountNormal:DiscountFather
  28. {
  29. /// <summary>
  30. /// 不打折
  31. /// </summary>
  32. /// <param name="sumMonye"></param>
  33. /// <returns></returns>
  34. public override double GetTotalMoney(double sumMonye)
  35. {
  36. return sumMonye;
  37. }
  38. }
  39. }
  40. using System;
  41. using System.Collections.Generic;
  42. using System.Text;
  43. namespace _097_超市收银系统
  44. {
  45. /// <summary>
  46. /// 折扣率
  47. /// </summary>
  48. class DiscountRate : DiscountFather
  49. {
  50. /// <summary>
  51. /// 折扣率
  52. /// </summary>
  53. public double Rate { get; set; }
  54. /// <summary>
  55. /// 创建对象实例时传入打折率
  56. /// </summary>
  57. /// <param name="rate">打折率</param>
  58. public DiscountRate(double rate)
  59. {
  60. this.Rate = rate;
  61. }
  62. public override double GetTotalMoney(double sumMonye)
  63. {
  64. return sumMonye * Rate;
  65. }
  66. }
  67. }
  68. using System;
  69. using System.Collections.Generic;
  70. using System.Text;
  71. namespace _097_超市收银系统
  72. {
  73. /// <summary>
  74. /// 满减
  75. /// </summary>
  76. class DiscountFull : DiscountFather
  77. {
  78. public double SufficientMoney { get; set; }
  79. public double DiscountMoney { get; set; }
  80. /// <summary>
  81. /// 创建对象实例时传入满减需要满足的金额,满减优惠
  82. /// </summary>
  83. /// <param name="sufficientMoney">满减需要满足的金额</param>
  84. /// <param name="discountMoney">满减优惠</param>
  85. public DiscountFull(double sufficientMoney, double discountMoney)
  86. {
  87. this.SufficientMoney = sufficientMoney;
  88. this.DiscountMoney = discountMoney;
  89. }
  90. public override double GetTotalMoney(double sumMonye)
  91. {
  92. if (sumMonye >= this.SufficientMoney)
  93. {
  94. //比如满减是500-100,这里实现600-100,1000-200
  95. return sumMonye - (int)(sumMonye / this.SufficientMoney) * this.DiscountMoney;
  96. }
  97. else
  98. {
  99. return sumMonye;
  100. }
  101. }
  102. }
  103. }