C#實(shí)現(xiàn)簡(jiǎn)單超市收銀系統(tǒng)
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡(jiǎn)單超市收銀系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
今天使用一直學(xué)習(xí)的抽象類等做了個(gè)簡(jiǎn)單的超市收銀系統(tǒng),不難,不過里面的邏輯要理清楚。
首先我們要知道我們要實(shí)現(xiàn)的功能。
超市一般有倉(cāng)庫,有收銀臺(tái),有各種各樣的商品,每一個(gè)商品都有Name,Price,Count等屬性。我們可以寫一個(gè)抽象父類,這樣我們才能將每件商品屬性設(shè)置一樣。
新建一個(gè)ProduckFather類作為商品的父類 并將屬性和構(gòu)造函數(shù)寫好,每次新建商品時(shí)寫好參數(shù)就會(huì)方便很多
abstract class ProduckFather ? ? { ? ? ? ? private string _name; ? ? ? ? private double _price; ? ? ? ? private int _count; ? ? ? ? private string _id; ? ? ? ? public string Name { get => _name; set => _name = value; } ? ? ? ? public double Price { get => _price; set => _price = value; } ? ? ? ? public int Count { get => _count; set => _count = value; } ? ? ? ? public string Id { get => _id; set => _id = value; } ? ? ? ? public ProduckFather(string name,double price,int count) ? ? ? ? { ? ? ? ? ? ? this.Name = name; ? ? ? ? ? ? this.Price = price; ? ? ? ? ? ? this.Count = count; ? ? ? ? } ? ? }```
接下來創(chuàng)造自己的商品,這里只舉Apple一個(gè)例子,不在重復(fù)代碼
```csharp ?class Apple:ProduckFather ? ? { ? ? ? ? public Apple(string name,double price,int count) ? ? ? ? ? ? :base(name,price,count) ? ? ? ? { ? ? ? ?? ? ? ? ? } ? ? }
接下來就是倉(cāng)庫類了,這一部分是個(gè)重點(diǎn),他要實(shí)現(xiàn)存儲(chǔ)的功能,那么我們用什么來存儲(chǔ)呢?
我們既要存不同種類的物品,數(shù)量也不會(huì)相同,首選是list(集合)或dictionary(鍵值對(duì)),問題是集合存儲(chǔ)雖然容易,想要拿到固定的東西卻比較復(fù)雜
list就相當(dāng)于是一個(gè)大倉(cāng)庫,雖然什么東西都能存儲(chǔ),但是每次找東西都要翻半天,所以我們可以使集合中每一個(gè)元素都是一個(gè)集合,而且這個(gè)集合必須是父類類型的,不然無法裝有其他類型的物品。
//[0]Apple [1]Banana [2]Vivo [3]Oppo [4]Huaweu? //實(shí)現(xiàn)存儲(chǔ)貨物 List<List<ProduckFather>> list = new List<List<ProduckFather>>(); ? ?//建造一個(gè)倉(cāng)庫 //讓每一個(gè)倉(cāng)庫擺滿東西
代碼中的list可以理解為倉(cāng)庫里擺滿了貨架,什么類型的物品就放在什么貨架上。
倉(cāng)庫每次調(diào)用時(shí)里面必須要有貨架,所以寫成構(gòu)造函數(shù)。
public Cangku() ? ? ? ? { ? ? ? ? ? ? list.Add(new List<ProduckFather>());//蘋果香蕉華為oppo和vivo ? ? ? ? ? ? list.Add(new List<ProduckFather>()); ? ? ? ? ? ? list.Add(new List<ProduckFather>()); ? ? ? ? ? ? list.Add(new List<ProduckFather>()); ? ? ? ? ? ? list.Add(new List<ProduckFather>()); ? ? ? ? }
倉(cāng)庫還要有進(jìn)貨,出貨,以及展示超市內(nèi)物品的功能,這一部分代碼不難,還請(qǐng)讀者根據(jù)注釋自行理解
/// <summary> ? ? ? ? /// 進(jìn)貨 ? ? ? ? /// </summary> ? ? ? ? /// <param name="strType">貨物類型</param> ? ? ? ? /// <param name="count">貨物數(shù)量</param> ? ? ? ? public void GetPros(string strType,int count) ? ? ? ? { ? ? ? ? ? ? for (int i = 0; i < count; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? switch (strType) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? case "Apple":list[0].Add(new Apple("Apple", 6000, 1)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "Banana":list[1].Add(new Banana("Banana",5,1)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "Vivo":list[2].Add(new Vivo("Vivo", 3000, 1)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "Oppo":list[3].Add(new Oppo("Oppo", 2500, 1)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "Huawei":list[4].Add(new Huawei("Huawei", 5000, 1)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? default:Console.WriteLine("你輸入的有錯(cuò)誤,請(qǐng)重新購(gòu)買"); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 取貨 ? ? ? ? /// </summary> ? ? ? ? /// <param name="strType"></param> ? ? ? ? /// <param name="count"></param> ? ? ? ? public ProduckFather[] SetPros(string strType, int count) ? ? ? ? { ? ? ? ? ? ? ProduckFather[] pros = new ProduckFather[count]; ? ? ? ? ? ? for (int i = 0; i < count; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? switch (strType) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? case "Apple": ? ? ? ? ? ? ? ? ? ? ? ? if (list[0].Count > count) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? pros[i] = list[0][0];//這里只是賦值的功能,并沒有移除貨物 ? ? ? ? ? ? ? ? ? ? ? ? ? ? list[0].RemoveAt(0);//移除貨物 ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Apple貨物不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? case "Banana": ? ? ? ? ? ? ? ? ? ? ? ? if (list[1].Count > count) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? pros[i] = list[1][0]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? list[1].RemoveAt(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Banana貨物不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? case "Vivo": ? ? ? ? ? ? ? ? ? ? ? ? if (list[2].Count > count) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? pros[i] = list[2][0]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? list[2].RemoveAt(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Vivo產(chǎn)品不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? case "Oppo": ? ? ? ? ? ? ? ? ? ? ? ? if (list[3].Count > count) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? pros[i] = list[3][0]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? list[3].RemoveAt(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Oppo產(chǎn)品不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? case "Huawei": ? ? ? ? ? ? ? ? ? ? ? ? if (list[4].Count > count) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? pros[i] = list[4][0]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? list[4].RemoveAt(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("Huawei產(chǎn)品不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? default:Console.WriteLine("沒有您想要的物品!"); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return pros; ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 展示超市物品 ? ? ? ? /// </summary> ? ? ? ? public void ShowPros()//用遍歷list來展示超市里的物品 ? ? ? ? { ? ? ? ? ? ? foreach (var item in list) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("我們超市有:" + item[0].Name ?+"\t"+"共有" +item.Count+ "個(gè)," + "\t" + "每個(gè)"+item[0].Price+"元"); ? ? ? ? ? ? } ? ? ? ? }
倉(cāng)庫完結(jié)后,還有收銀臺(tái)的功能。
每次使用收銀臺(tái)時(shí),必須有貨物,所以寫成構(gòu)造函數(shù)保證倉(cāng)庫有貨
//每次引用時(shí),需要進(jìn)貨 Cangku ck = new Cangku();//創(chuàng)建倉(cāng)庫對(duì)象 /// <summary> /// 每次創(chuàng)建對(duì)象就進(jìn)貨 /// </summary> public SuperMarket() ? ? ? ? { ? ? ? ? ? ? ck.GetPros("Apple", 100);//調(diào)用倉(cāng)庫類的進(jìn)貨函數(shù)進(jìn)貨 ? ? ? ? ? ? ck.GetPros("Banana", 1000); ? ? ? ? ? ? ck.GetPros("Vivo", 150); ? ? ? ? ? ? ck.GetPros("Oppo", 200); ? ? ? ? ? ? ck.GetPros("Huawei", 500); ? ? ? ? }
超市往往會(huì)有打折活動(dòng),一般這個(gè)活動(dòng)分為兩種例如:八折或者滿300減50?;顒?dòng)往往是會(huì)變化的,所以寫成抽象類
抽象類中提供一個(gè)打折方法。
abstract class CalFather ? ? { ? ? ? ? public abstract double Cal(double realmoney); ? ? }
創(chuàng)建打折類CalRate
class CalRate : CalFather ? ? { ? ? ? ? double _rate; ? ? ? ? public double Rate { get => _rate; set => _rate = value; } ? ? ? ? public CalRate(double rate) ? ? ? ? { ? ? ? ? ? ? this.Rate = rate; ? ? ? ? } ? ? ? ? public override double Cal(double realmoney) ? ? ? ? { ? ? ? ? ? ? return realmoney * this.Rate; ? ? ? ? } ? ? }
滿減類CalMN
class CalMN : CalFather ? ? { ? ? ? ? double m; ? ? ? ? double n; ? ? ? ? public double M { get => m; set => m = value; } ? ? ? ? public double N { get => n; set => n = value; } ? ? ? ? public CalMN(double m,double n) ? ? ? ? { ? ? ? ? ? ? this.M = m; ? ? ? ? ? ? this.N = n; ? ? ? ? } ? ? ? ? public override double Cal(double realmoney) ? ? ? ? { ? ? ? ? ? ? return realmoney - N * (realmoney % M); ? ? ? ? } ? ? }
這兩塊沒有難點(diǎn),自行理解
接下來與用戶的交互重中之重,包含了計(jì)算用戶貨物的錢以及計(jì)算打折后的錢等。
不明白請(qǐng)看注釋
?/// <summary> ? ? ? ? /// 用顧客交互,詢問需要什么 ? ? ? ? /// </summary> ? ? ? ? public void SayWhat() ? ? ? ? { ? ? ? ? ? ? Console.WriteLine("您好您需要什么?"); ? ? ? ? ? ? string str = Console.ReadLine(); ? ? ? ? ? ? Console.WriteLine("您需要多少?"); ? ? ? ? ? ? int a = Convert.ToInt32(Console.ReadLine()); ? ? ? ? ? ? ProduckFather[] pros= ?ck.SetPros(str, a);//將貨物取出 ? ? ? ? ? ? double realmoney = ?GetMoney(pros); ? //計(jì)算真實(shí)價(jià)格 ? ? ? ? ? ? Console.WriteLine("您購(gòu)買的物品是{0},數(shù)量是{1},價(jià)格是{2}", str, a,realmoney);//用戶購(gòu)買清單 ? ? ? ? ? ? Console.WriteLine("請(qǐng)選擇您的打折方式:1--8折,2--滿300送50 ?3--不打折"); ? ? ? ? ? ? ?int input= Input(); //用戶輸入,調(diào)用Input判斷輸入是否合規(guī)并返回int類型 ? ? ? ? ? ? CalFather cal = Money(input);//判斷打折方法并,返回一個(gè)父類對(duì)象 ? ? ? ? ? ? double money= cal.Cal(realmoney);//調(diào)用打折虛方法并返回打折后的錢 ? ? ? ? ? ? Console.WriteLine("總共要花費(fèi){0}元", money); ? ? ? ? ? ? XiaoPiao(pros, money); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 計(jì)算真實(shí)價(jià)格 ? ? ? ? /// </summary> ? ? ? ? /// <param name="pros"></param> ? ? ? ? /// <returns></returns> ? ? ? ? public double GetMoney(ProduckFather[] pros)//遍歷pros(相當(dāng)于購(gòu)物車)來計(jì)算價(jià)錢 ? ? ? ? { ? ? ? ? ? ? double realmoney=0; ? ? ? ? ? ? foreach (var item in pros) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? realmoney += item.Price; ? ? ? ? ? ? } ? ? ? ? ? ? return realmoney; ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 打折后的錢 ? ? ? ? /// </summary> ? ? ? ? /// <param name="input"></param> ? ? ? ? /// <returns></returns> ? ? ? ? public CalFather Money(int input)//根據(jù)用戶的選擇來計(jì)算打折后的錢 ? ? ? ? { ? ? ? ? ? ? CalFather cal = null; ? ? ? ? ? ? switch (input) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case 1:cal = new CalRate(0.8); ? ? ? ?? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 2:cal = new CalMN(300, 50); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 3:cal = new CalNo();? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? return cal; ? ? ? ? } ? ? ? ? public void ShowPros() ? ? ? ? { ? ? ? ? ? ? ck.ShowPros(); ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 判斷用戶輸入是否合理 ? ? ? ? /// </summary> ? ? ? ? public int Input() ?//判斷用戶輸入的是否合規(guī),不合規(guī)則一直輸入 ? ? ? ? { ? ? ? ? ? ? bool b = true; ? ? ? ? ? ? int input; ? ? ? ? ? ? while (b) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? input = Convert.ToInt32(Console.ReadLine()); ? ? ? ? ? ? ? ? ? ? if (input <= 3 && input > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? return input; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? b = true; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? catch ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Console.WriteLine("您輸入的不正確,請(qǐng)重新輸入:1--8折,2--滿300送50 ?3--不打折"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return 0; ? ? ? ? ? ? ? ? ? } ? ? ? ? //打印偽小票 ? ? ? ? public void XiaoPiao(ProduckFather[] pros, double money) ? ? ? ? { ? ? ? ? ? ? Console.WriteLine("***************發(fā)票***************"); ? ? ? ? ? ? foreach (var item in pros) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("本次您購(gòu)買了" + item.Name + "\t" + "一共" + item.Count + "\t" + "件" ); ? ? ? ? ? ? } ? ? ? ? ? ? Console.WriteLine("總花費(fèi)了{(lán)0}元", money); ? ? ? ? ? ? Console.WriteLine("請(qǐng)?jiān)u價(jià)本次購(gòu)買:1--滿意 2--良好 3--不滿意"); ? ? ? ? ? ? Console.ReadKey(); ? ? ? ? ? ? Console.WriteLine("感謝您的評(píng)價(jià),祝您生活愉快!"); ? ? ? ? } ? ? }
主函數(shù):
//創(chuàng)建超市對(duì)象 SupperMarket sp = new SupperMarket(); //展示超市貨物 sp.ShowPros(); //跟用戶交互 sp.SayWhat();
運(yùn)行界面:
本系統(tǒng)還有部分尚不完善,是開源的,還可以實(shí)現(xiàn)進(jìn)貨的功能,以及try{}catch{}并沒有加很多的異常判斷,系統(tǒng)不算穩(wěn)定,讀者可以自行修改。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中把Json數(shù)據(jù)轉(zhuǎn)為DataTable
這篇文章介紹了C#中把Json數(shù)據(jù)轉(zhuǎn)為DataTable的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04C#面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)原則
這篇文章介紹了C#面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)原則,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03C#使用Http Post方式傳遞Json數(shù)據(jù)字符串調(diào)用Web Service
這篇文章主要為大家詳細(xì)介紹了C#使用Http Post方式傳遞Json數(shù)據(jù)字符串調(diào)用Web Service,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08C#單例模式Singleton的實(shí)現(xiàn)詳解
單例模式(Singleton?Pattern)是日常開發(fā)中最簡(jiǎn)單的設(shè)計(jì)模式之一,它提供了一種創(chuàng)建對(duì)象的最佳方式,本文主要為大家介紹的是C#單例模式的實(shí)現(xiàn)方法,需要的可以參考一下2023-05-05c# 以類名為參創(chuàng)建父類相同的類的實(shí)例代碼
下面小編就為大家?guī)硪黄猚# 以類名為參創(chuàng)建父類相同的類的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01c#中利用委托反射將DataTable轉(zhuǎn)換為實(shí)體集的代碼
c#中利用委托反射將DataTable轉(zhuǎn)換為實(shí)體集的代碼,需要的朋友可以參考下2012-10-10C#中IEnumerable接口介紹并實(shí)現(xiàn)自定義集合
這篇文章介紹了C#中IEnumerable接口并實(shí)現(xiàn)自定義集合,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04