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 }
1702.貨物基類


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 }
175.商品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;
}
}
}


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();
}
}
}


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


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


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)
{
}
}
}


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)
{
}
}
}


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);
}
}


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;
}
}
}


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();
}
}
}