|
Posted on 2017-09-17 21:27 viery 阅读(123) 评论(0) 编辑 收藏 所属分类: .net
1.仓库类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SupperMarket 8 { 9 //创建仓库类 10 class Store 11 { 12 13 //创建货架集合 14 List<List<GoodsBase>> ShelvesList = new List<List<GoodsBase>>(); 15 16 //创建购物车 17 List<List<GoodsBase>> ShopCar = new List<List<GoodsBase>>(); 18 19 //初始化仓库时,添加商品的货架 20 21 public Store() 22 { 23 ShelvesList.Add(new List<GoodsBase>()); //Apple 24 ShelvesList.Add(new List<GoodsBase>()); //Book 25 ShelvesList.Add(new List<GoodsBase>()); //Pen 26 27 ShopCar.Add(new List<GoodsBase>()); //Apple 28 ShopCar.Add(new List<GoodsBase>()); //book 29 ShopCar.Add(new List<GoodsBase>()); //pen 30 31 this.AddGoods("苹果", 20); 32 this.AddGoods("书籍", 30); 33 this.AddGoods("钢笔", 60); 34 } 35 36 37 38 //展示仓库商品 39 public void showGoods() 40 { 41 Console.WriteLine("*********************************库存展示*********************************"); 42 foreach (var item in ShelvesList) 43 { 44 45 Console.WriteLine("商品:{0} 数量:{1}个 单价:{2}" 46 , item[0].Name, item.Count, item[0].Price); 47 48 49 50 } 51 Console.WriteLine("*********************************展示完毕*********************************"); 52 } 53 54 55 56 //创建货品方法 57 58 public void AddGoods(String goodsType, int number) 59 { 60 switch (goodsType) 61 { 62 case "苹果": 63 GoodsBase applegoods; 64 for (int i = 0; i < number; i++) 65 { 66 applegoods = new Apple("苹果", 600.00m, Guid.NewGuid().ToString()); 67 ShelvesList[0].Add(applegoods); 68 } 69 //Console.WriteLine("成功添加 {0} 类商品,共{1}个。", applegoods.Name, number); 70 71 break; 72 73 case "书籍": GoodsBase bookgoods; 74 for (int i = 0; i < number; i++) 75 { 76 bookgoods = new Book("书籍", 160.00m, Guid.NewGuid().ToString()); 77 ShelvesList[1].Add(bookgoods); 78 } 79 // Console.WriteLine("成功添加 {0} 类商品,共{1}个。", bookgoods.Name, number); 80 81 break; 82 83 case "钢笔": GoodsBase pengoods; 84 { 85 86 for (int i = 0; i < number; i++) 87 { 88 pengoods = new Pen("钢笔", 60.00m, Guid.NewGuid().ToString()); 89 ShelvesList[2].Add(pengoods); 90 } 91 92 } 93 // Console.WriteLine("成功添加 {0} 类商品,共{1}个。", pengoods.Name, number); 94 95 break; 96 } 97 } 98 99 100 101 //货品出库 即拿到装满货物的购物车List,同时减少库存 102 103 public List<List<GoodsBase>> OutGoods(String goodsType, int number) 104 { 105 106 107 switch (goodsType) 108 { 109 //将苹果装入购物车第1层 110 case "苹果": 111 //GoodsBase applegoods; 112 if (ShelvesList[0].Count >= number) //剩余库存>0 113 { 114 for (int i = 0; i < number; i++) 115 { 116 this.ShopCar[0].Add(ShelvesList[0][0]); 117 ShelvesList[0].RemoveAt(0); 118 } 119 } 120 else 121 { 122 Console.WriteLine(" 商品出库数量大于库存数,不能出库,请重新输入."); 123 break; 124 } 125 126 break; 127 128 //将书籍装入购物车第2层 129 case "书籍": 130 //GoodsBase Book; 131 if (ShelvesList[1].Count >= number) //剩余库存>0 132 { 133 for (int i = 0; i < number; i++) 134 { 135 this.ShopCar[1].Add(ShelvesList[1][0]); 136 ShelvesList[1].RemoveAt(0); //从货架出库 137 } 138 } 139 else 140 { 141 Console.WriteLine(" 商品出库数量大于库存数,不能出库,请重新输入."); 142 } 143 break; 144 145 //将钢笔装入购物车第3层 146 case "钢笔": 147 //GoodsBase Pen; 148 if (ShelvesList[2].Count >= number) //剩余库存>0 149 { 150 for (int i = 0; i < number; i++) 151 { 152 this.ShopCar[2].Add(ShelvesList[2][0]); 153 ShelvesList[2].RemoveAt(0); 154 } 155 } 156 else 157 { 158 Console.WriteLine(" 商品出库数量大于库存数,不能出库,请重新输入."); 159 } 160 break; 161 162 } 163 return this.ShopCar; 164 165 } 166 167 168 } 169 } 170 2.货物基类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SupperMarket 8 { 9 /// <summary> 10 /// 商品基类 11 /// </summary> 12 public class GoodsBase 13 { 14 public string Name { get; set; } 15 public decimal Price { get; set; } 16 public string Id { get; set; } 17 18 19 //构造方法中构建商品对象 20 21 public GoodsBase(string name,decimal price,string id) 22 { 23 this.Name = name; 24 this.Price = price; 25 this.Id = id; 26 } 27 } 28 }3.结算类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SupperMarket 8 { 9 10 11 public class SettleMent 12 { 13 public decimal getTotalMoney(List<List<GoodsBase>> ShopCar) //计算总价方法 14 { 15 decimal total = 0m; 16 foreach (var carCode in ShopCar)//购物车每一层 17 { 18 foreach (var item in carCode) //每一层的每个货物 19 { 20 total += item.Price; 21 } 22 23 } 24 return total; 25 } 26 27 28 29 30 public void Settle() //结算过程 31 { 32 Store s = new Store(); 33 s.showGoods(); 34 Console.WriteLine("******************请选择你的商品**********************"); 35 36 37 List<List<GoodsBase>> ShopCar; 38 while (true) 39 { 40 Console.WriteLine("请输入要结算的商品名称 (请输入:苹果 | 书籍 | 钢笔):"); 41 string name = Console.ReadLine(); 42 43 Console.WriteLine("请输入要添加的商品名称的数量:"); 44 int number = Convert.ToInt32(Console.ReadLine()); 45 46 ShopCar= s.OutGoods(name,number); 47 Console.WriteLine("继续添加? Y | N"); 48 string flag = Console.ReadLine(); 49 if (flag == "N") 50 { 51 break; 52 } 53 54 } 55 56 57 //计算购物车总金额 58 decimal total= this.getTotalMoney(ShopCar); 59 60 double t = Convert.ToDouble(total); 61 62 //计算折扣 63 double discount=this.GetDis().getDiscount(t); 64 65 decimal to = Convert.ToDecimal(discount); 66 //打印小票 67 this.printTicket(ShopCar, total,discount); 68 //显示库存 69 s.showGoods(); 70 } 71 72 /// <summary> 73 /// 工厂类:获得Discount父类对象 74 /// </summary> 75 /// <returns></returns> 76 public Discount GetDis() 77 { 78 Discount dis=null; 79 Console.WriteLine("请输入打折类型 (1 不打折 | 2 9折 | 3 8.5折 | 4 满300送100 | 5 满500 送200):"); 80 string discountType = Console.ReadLine(); 81 switch (discountType) 82 83 { 84 case "1": dis = new DiscountRate(1); break; 85 case "2": dis = new DiscountRate(0.9); break; 86 case "3": dis = new DiscountRate(0.85); break; 87 case "4": dis = new DiscountMN(300, 100); break; 88 case "5": dis = new DiscountMN(500, 200); break; 89 90 } 91 return dis; 92 } 93 94 public void printTicket(List<List<GoodsBase>> ShopCar, decimal total,double discount) 95 { 96 Console.WriteLine("***********************购物小票********************"); 97 Console.WriteLine("您一共消费{0}元--------------{1}", total, DateTime.Now.ToString()); 98 Console.WriteLine("打折后一共{0}元--------------{1}", discount, DateTime.Now.ToString()); 99 Console.WriteLine("明细:"); 100 foreach (var car in ShopCar) 101 { 102 foreach (var item in car) 103 { 104 105 Console.WriteLine("商品名字:{0}-----商品编码:{1}--------商品单价:{2}", item.Name, (item.Id).Substring(0,12), item.Price); 106 } 107 } 108 109 Console.WriteLine("购物愉快,再见。"); 110 111 } 112 113 114 } 115 } 116 4.商品apple类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SupperMarket 8 { 9 class Apple:GoodsBase 10 { 11 public Apple(String name, decimal price, string id) 12 : base(name, price, id) 13 { 14 } 15 } 16 } 17 5.商品pen类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace SupperMarket { class Pen:GoodsBase { public Pen(String name, decimal price, string id) : base(name, price, id) { } }
}6.商品book类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace SupperMarket { class Book:GoodsBase { public Book(String name, decimal price, string id) : base(name, price, id) { } } }
7.折扣抽象工厂类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace SupperMarket { public abstract class Discount {
public abstract double getDiscount(double total); } }
8.折扣实现类rate using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace SupperMarket { public class DiscountRate : Discount { public double Rate{get;set;}
public DiscountRate(double rate) { this.Rate = rate; }
public override double getDiscount(double total) { double discount = 0;
return discount =total*this.Rate; } } }9.
9.折扣实现类MN using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace SupperMarket { public class DiscountMN :Discount {
public double M { get; set; }
public double N { get; set; }
public DiscountMN(double m,double n) { this.M = m; this.N = n; } public override double getDiscount(double total) { double discount = 0;
discount = total - (int)(total / M) * N;
return discount; } } }
10 main using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace SupperMarket { class Program { static void Main(string[] args) { //Store s=new Store();
//while (true) //{ //Console.WriteLine("请输入要添加的商品名称 (请输入:苹果 | 书籍 | 钢笔):"); //string name = Console.ReadLine();
//Console.WriteLine("请输入要添加的商品名称的数量:"); //int number = Convert.ToInt32(Console.ReadLine());
// s.AddGoods(name,number); // Console.WriteLine("继续添加? Y | N"); // string flag = Console.ReadLine(); // if (flag == "N") // { // break; // } //}
//s.showGoods();
new SettleMent().Settle();
Console.ReadKey(); } } }
|