Golang收支記賬程序詳細(xì)編寫過程
1.項(xiàng)目開發(fā)流程
2.項(xiàng)目需求說明
模擬實(shí)現(xiàn)基于文本界面的《家庭記賬軟件》
該軟件能夠記錄家庭的收入,支出,并能夠打印收支明細(xì)表
3.項(xiàng)目的界面
4.項(xiàng)目代碼實(shí)現(xiàn)
實(shí)現(xiàn)基本功能(先使用面向過程,后面改成面向?qū)ο螅?/p>
功能1:先完成可以顯示主菜單,并且可以退出
思路分析:
更加給出的界面完成,主菜單的顯示,當(dāng)用戶輸入4時(shí),就退出該程序
功能2:完成可以顯示明細(xì)和登記收入的功能
思路分析:
- 因?yàn)樾枰@示明細(xì),我們定義一個(gè)變量details string 來記錄
- 還需要定義變量來記錄余額(balance),每次收支的金額(money),每次收支的說明(note)
代碼改進(jìn)
用戶輸入4退出時(shí),給出提示“你確定要退出嗎?y/n”,必須輸入正確的y/n,否則循環(huán)輸入指令,直到輸入y或者n
package main import ( "fmt" ) func main(){ //聲明一個(gè)變量,保存接收用戶輸入的選項(xiàng) key := "" // 聲明一個(gè)變量,控制是否退出for loop := true // 定義賬戶的余額 [] balance := 10000.0 // 每次收支的金額 money := 0.0 // 每次收支的說明 note := "" // 定義一個(gè)變量,記錄是否有收支的行為 flag := false // 收支的詳情使用字符串來記錄 //當(dāng)有收支時(shí),只需要對details進(jìn)行拼接處理即可 details := "收入\t賬戶金額\t收入金額\t說 明" // 顯示這個(gè)主菜單 for { fmt.Println("-------家庭收支記賬軟件---------") fmt.Println(" 1.收支明細(xì)") fmt.Println(" 2.登記收入") fmt.Println(" 3.登記支出") fmt.Println(" 4.退出軟件") fmt.Println("請選擇(1-4):") fmt.Scanln(&key) switch key{ case "1": fmt.Println("\n----------當(dāng)前收支明細(xì)記錄-----------") if flag { fmt.Println(details) }else{ fmt.Println("當(dāng)前沒有收支明細(xì)... 來一筆吧!") } // fmt.Println(details) case "2": fmt.Println("本次收入金額:") fmt.Scanln(&money) balance += money // 修改賬戶余額 fmt.Println("本次收入說明:") fmt.Scanln(¬e) // 將這個(gè)收入情況,拼接到details變量 details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balance,money,note) flag = true case "3": fmt.Println("登記支出金額:") fmt.Scanln(&money) // 這里需要做一個(gè)必要的判斷 if money > balance{ fmt.Println("余額的金額不足") break } balance -= money fmt.Println("本次支出說明:") fmt.Scanln(¬e) details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note) case "4": fmt.Println("你確定要退出嗎?y/n") choice := "" for{ fmt.Scanln(&choice) if choice == "y" || choice == "n"{ break } fmt.Println("你的輸入有誤,請重新輸入y/n") } if choice == "y"{ loop = false } default : fmt.Println("請輸入正確的選項(xiàng)..") } if !loop { fmt.Println("你退出家庭賬本") break; } } }
將上面的代碼改成面向?qū)ο蟮姆椒ǎ帉憁yFamilyAccount.go,并使用testMyFamilyAccount.go去完成測試
思路分析
把記賬軟件的功能,封裝到一個(gè)結(jié)構(gòu)體中,然后調(diào)用該結(jié)構(gòu)體的方法,來實(shí)現(xiàn)記賬,顯示明細(xì)。結(jié)構(gòu)體的名字FamilyAccount
在通過main方法中,創(chuàng)建一個(gè)結(jié)構(gòu)體FamilyAccount實(shí)例,實(shí)現(xiàn)記賬即可。
// familyAccount.go
package utils import ( "fmt" ) type FamilyAccount struct{ key string // 聲明一個(gè)字段,控制是否退出for loop bool // 定義賬戶的余額 [] balance float64 // 每次收支的金額 money float64 // 每次收支的說明 note string // 定義一個(gè)變量,記錄是否有收支的行為 flag bool // 收支的詳情使用字符串來記錄 //當(dāng)有收支時(shí),只需要對details進(jìn)行拼接處理即可 details string } // 編寫一個(gè)工廠模式的構(gòu)造方法,返回一個(gè)*FamilyAccount實(shí)例 func NewFamilyAccount() *FamilyAccount{ return &FamilyAccount{ key:"", loop:true, balance:10000.0, money:0.0, note:"", flag:false, details:"收入\t賬戶金額\t收入金額\t說 明" } } // 顯示明細(xì)方法 func (this *FamilyAccount) showDetails(){ fmt.Println("\n----------當(dāng)前收支明細(xì)記錄-----------") if this.flag { fmt.Println(this.details) }else{ fmt.Println("當(dāng)前沒有收支明細(xì)... 來一筆吧!") } } // 登記收入方法 func (this *FamilyAccount) income(){ fmt.Println("本次收入金額:") fmt.Scanln(&this.money) this.balance += this.money // 修改賬戶余額 fmt.Println("本次收入說明:") fmt.Scanln(&this.note) // 將這個(gè)收入情況,拼接到details變量 this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v",this.balance,this.money,this.note) this.flag = true } // 登記支出方法 func (this *FamilyAccount) pay(){ fmt.Println("登記支出金額:") fmt.Scanln(&this.money) // 這里需要做一個(gè)必要的判斷 if this.money > this.balance{ fmt.Println("余額的金額不足") break } this.balance -= this.money fmt.Println("本次支出說明:") fmt.Scanln(&this.note) this.details += fmt.Sprintf("\n支出\t%v\t%v\t%v",this.balance,this.money,this.note) } // 退出方法 func (this *FamilyAccount) exit(){ fmt.Println("你確定要退出嗎?y/n") choice := "" for{ fmt.Scanln(&choice) if choice == "y" || choice == "n"{ break } fmt.Println("你的輸入有誤,請重新輸入y/n") } if choice == "y"{ this.loop = false } } //給該結(jié)構(gòu)體綁定相應(yīng)的方法 // 顯示主菜單 func (this *FamilyAccount) MainMenu(){ for { fmt.Println("-------家庭收支記賬軟件---------") fmt.Println(" 1.收支明細(xì)") fmt.Println(" 2.登記收入") fmt.Println(" 3.登記支出") fmt.Println(" 4.退出軟件") fmt.Println("請選擇(1-4):") fmt.Scanln(&this.key) switch this.key{ case "1": this.showDetails() case "2": this.income() case "3": this.pay() case "4": this.exit() default : fmt.Println("請輸入正確的選項(xiàng)..") } if !this.loop { break; } } }
// main.go
package main import ( "go_code/familyaccount/utils" ) func main(){ fmt.Println("這個(gè)是面向?qū)ο蟮姆绞酵瓿蓗~") utils.NewFamilyAccount().MainMenu() }
到此這篇關(guān)于Golang收支記賬程序詳細(xì)編寫過程的文章就介紹到這了,更多相關(guān)Golang收支記賬內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang實(shí)現(xiàn)優(yōu)雅的將struct轉(zhuǎn)換為map
在項(xiàng)目實(shí)踐中,有時(shí)候我們需要將struct結(jié)構(gòu)體轉(zhuǎn)為map映射表,然后基于map做數(shù)據(jù)裁剪或操作。那么下面我來介紹下常用的兩種轉(zhuǎn)換方式,希望對大家有所幫助2023-01-01詳解Golang中Context的三個(gè)常見應(yīng)用場景
Golang?context主要用于定義超時(shí)取消,取消后續(xù)操作,在不同操作中傳遞值。本文通過簡單易懂的示例進(jìn)行說明,感興趣的可以了解一下2022-12-12Golang校驗(yàn)字符串是否JSON格式的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Golang中校驗(yàn)字符串是否JSON格式的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04Golang實(shí)現(xiàn)簡易的rpc調(diào)用
RPC指(Remote Procedure Call Protocol)遠(yuǎn)程過程調(diào)用協(xié)議。本文將實(shí)現(xiàn)利用Golang進(jìn)行rpc調(diào)用(只實(shí)現(xiàn)一個(gè)rpc框架基本的功能,不對性能做保證),需要的可以參考一下2023-03-03Golang中 import cycle not allowed 問題
這篇文章主要介紹了Golang中 import cycle not allowed 問題的解決方法,問題從描述到解決都非常詳細(xì),需要的小伙伴可以參考一下2022-03-03