C#簡易超市收銀系統
阿新 • • 發佈:2022-01-18
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 Price13 { 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<ProductFather>> list = new List<List<ProductFather>>(); 12 13 14 15 /// <summary> 16 ///向用戶展示貨物 17 /// </summary> 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 /// <summary> 30 /// 在建立倉庫物件的時候 像倉庫中新增貨架 31 /// </summary> 32 public CangKu() 33 { 34 list.Add(new List<ProductFather>()); 35 list.Add(new List<ProductFather>()); 36 list.Add(new List<ProductFather>()); 37 list.Add(new List<ProductFather>()); 38 } 39 /// <summary> 40 /// 進貨 41 /// </summary> 42 /// <param name="strType">貨物的型別</param> 43 /// <param name="count">貨物的數量</param> 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 /// <summary> 66 /// 從倉庫中提取貨物 67 /// </summary> 68 /// <param name="strType"></param> 69 /// <param name="count"></param> 70 /// <returns></returns> 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 /// <summary> 12 /// 折扣率 13 /// </summary> 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 /// <summary> 14 /// 建立超市物件的時候,給倉庫的貨架上匯入貨物 15 /// </summary> 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 /// <summary> 26 /// 跟使用者互動的過程 27 /// </summary> 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 /// <summary> 61 /// 根據使用者的選擇打折方式返回一個打折物件 62 /// </summary> 63 /// <param name="input">使用者的選擇</param> 64 /// <returns>返回的父類物件 但是裡面裝的是子類物件</returns> 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 /// <summary> 93 /// 根據使用者買的貨物計算總價錢 94 /// </summary> 95 /// <param name="pros"></param> 96 /// <returns></returns> 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 }