1 using System;
2
3 namespace 超市系统
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 //创建超市对象
10 SupperMarket sm = new SupperMarket();
11 //展示货物
12 sm.ShowPros();
13 //跟用户交互
14 sm.AskBuying();
15 Console.ReadKey();
16 }
17 }
18 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9
10 class ProductFather
11 {
12 public double Price
13 {
14 get;
15 set;
16 }
17
18 public string Name
19 {
20 get;
21 set;
22 }
23
24 public string ID
25 {
26 get;
27 set;
28 }
29
30 public ProductFather(string id, double price, string Name)
31 {
32 this.ID = id;
33 this.Price = price;
34 this.Name = Name;
35 }
36 }
37 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class Acer:ProductFather
10 {
11 public Acer(string id, double price, string Name)
12 : base(id, price, Name)
13 {
14
15 }
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class Banana:ProductFather
10 {
11 public Banana(string id, double price, string Name)
12 : base(id, price, Name)
13 {
14
15 }
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class JiangYou:ProductFather
10 {
11 public JiangYou(string id, double price, string Name)
12 : base(id, price, Name)
13 {
14
15 }
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class SamSung:ProductFather
10 {
11 public SamSung(string id, double price, string Name)
12 : base(id, price, Name)
13 {
14
15 }
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class CangKu
10 {
11 List> list = new List>();
12
13
14
15 ///
16 ///向用户展示货物
17 ///
18 public void ShowPros()
19 {
20 foreach (var item in list)
21 {
22 Console.WriteLine("我们超市有:" + item[0].Name + "," + "\t" + "有" + item.Count + "个," + "\t" + "每个" + item[0].Price + "元");
23 }
24 }
25 //list[0]存储Acer电脑
26 //list[1]存储三星手机
27 //list[2]存储酱油
28 //list[3]存储香蕉
29 ///
30 /// 在创建仓库对象的时候 像仓库中添加货架
31 ///
32 public CangKu()
33 {
34 list.Add(new List());
35 list.Add(new List());
36 list.Add(new List());
37 list.Add(new List());
38 }
39 ///
40 /// 进货
41 ///
42 /// 货物的类型
43 /// 货物的数量
44 public void JinPros(string strType, int count)
45 {
46 for (int i = 0; i < count; i++)
47 {
48 switch (strType)
49 {
50 case "Acer":
51 list[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏基笔记本"));
52 break;
53 case "SamSung":
54 list[1].Add(new SamSung(Guid.NewGuid().ToString(), 2000, "棒之手机"));
55 break;
56 case "JiangYou":
57 list[2].Add(new JiangYou(Guid.NewGuid().ToString(), 10, "老抽酱油"));
58 break;
59 case "Banana":
60 list[3].Add(new Banana(Guid.NewGuid().ToString(), 50, "大香蕉"));
61 break;
62 }
63 }
64 }
65 ///
66 /// 从仓库中提取货物
67 ///
68 ///
69 ///
70 ///
71 public ProductFather[] QuPros(string strType, int count)
72 {
73 ProductFather[] pros = new ProductFather[count];
74 for (int i = 0; i < pros.Length; i++)
75 {
76 switch (strType)
77 {
78 case "Acer":
79 pros[i] = list[0][0];
80 list[0].RemoveAt(0);
81 break;
82 case "SamSung":
83 pros[i] = list[1][0];
84 list[1].RemoveAt(0);
85 break;
86 case "JiangYou":
87 pros[i] = list[2][0];
88 list[2].RemoveAt(0);
89 break;
90 case "Banana":
91 pros[i] = list[3][0];
92 list[3].RemoveAt(0);
93 break;
94 }
95 }
96 return pros;
97 }
98 }
99 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 abstract class CalFather
10 {
11 public abstract double GetTotalMoney(double realMoney);
12 }
13 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class CalMN : CalFather
10 {
11 //买500送100
12 public double M
13 {
14 get;
15 set;
16 }
17
18 public double N
19 {
20 get;
21 set;
22 }
23
24 public CalMN(double m, double n)
25 {
26 this.M = m;
27 this.N = n;
28 }
29 public override double GetTotalMoney(double realMoney)
30 {
31 //600 -100
32 //1000-200
33 //1200
34 if (realMoney >= this.M)
35 {
36 return realMoney - (int)(realMoney / this.M) * this.N;
37 }
38 else
39 {
40 return realMoney;
41 }
42 }
43 }
44 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class CalNormal:CalFather
10 {
11 public override double GetTotalMoney(double realMoney)
12 {
13 return realMoney;
14 }
15 }
16 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class CalRate:CalFather
10 {
11 ///
12 /// 折扣率
13 ///
14 public double Rate
15 {
16 get;
17 set;
18 }
19
20 public CalRate(double rate)
21 {
22 this.Rate = rate;
23 }
24 public override double GetTotalMoney(double realMoney)
25 {
26 return realMoney * this.Rate;
27 }
28 }
29 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 超市系统
8 {
9 class SupperMarket
10 {
11 //创建仓库对象
12 CangKu ck = new CangKu();
13 ///
14 /// 创建超市对象的时候,给仓库的货架上导入货物
15 ///
16 public SupperMarket()
17 {
18 ck.JinPros("Acer", 1000);
19 ck.JinPros("SamSung", 1000);
20 ck.JinPros("JiangYou", 1000);
21 ck.JinPros("Banana", 1000);
22 }
23
24
25 ///
26 /// 跟用户交互的过程
27 ///
28 public void AskBuying()
29 {
30 Console.WriteLine("欢迎观临,请问您需要些什么?");
31 Console.WriteLine("我们有 Acer、SamSung、Jiangyou、Banana");
32 string strType = Console.ReadLine();
33 Console.WriteLine("您需要多少?");
34 int count = Convert.ToInt32(Console.ReadLine());
35 //去仓库取货物
36 ProductFather[] pros = ck.QuPros(strType, count);
37 //下面该计算价钱了
38 double realMoney = GetMoney(pros);
39 Console.WriteLine("您总共应付{0}元", realMoney);
40 Console.WriteLine("请选择您的打折方式 1--不打折 2--打九折 3--打85 折 4--买300送50 5--买500送100");
41 string input = Console.ReadLine();
42 //通过简单工厂的设计模式根据用户的舒服获得一个打折对象
43 CalFather cal = GetCal(input);
44 double totalMoney = cal.GetTotalMoney(realMoney);
45 Console.WriteLine("打完折后,您应付{0}元", totalMoney);
46 Console.WriteLine("以下是您的购物信息");
47 foreach (var item in pros)
48 {
49 Console.WriteLine("货物名称:" + item.Name + "," + "\t" + "货物单价:" + item.Price + "," + "\t" + "货物编号:" + item.ID);
50 }
51
52 }
53
54
55
56
57
58
59
60 ///
61 /// 根据用户的选择打折方式返回一个打折对象
62 ///
63 /// 用户的选择
64 /// 返回的父类对象 但是里面装的是子类对象
65 public CalFather GetCal(string input)
66 {
67 CalFather cal = null;
68 switch (input)
69 {
70 case "1":
71 cal = new CalNormal();
72 break;
73 case "2":
74 cal = new CalRate(0.9);
75 break;
76 case "3":
77 cal = new CalRate(0.85);
78 break;
79 case "4":
80 cal = new CalMN(300, 50);
81 break;
82 case "5":
83 cal = new CalMN(500, 100);
84 break;
85 }
86 return cal;
87 }
88
89
90
91
92 ///
93 /// 根据用户买的货物计算总价钱
94 ///
95 ///
96 ///
97 public double GetMoney(ProductFather[] pros)
98 {
99 double realMoney = 0;
100 //realMoney = pros[0].Price * pros.Length;
101
102 for (int i = 0; i < pros.Length; i++)
103 {
104 realMoney += pros[i].Price;
105
106 // realMoney = pros[i] * pros.Length;
107 }
108 return realMoney;
109 }
110
111
112
113
114 public void ShowPros()
115 {
116 ck.ShowPros();
117 }
118
119 }
120 }