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ū)ο蟮姆椒?,編寫myFamilyAccount.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)文章
go語言使用Casbin實(shí)現(xiàn)角色的權(quán)限控制
Casbin是用于Golang項(xiàng)目的功能強(qiáng)大且高效的開源訪問控制庫。本文主要介紹了go語言使用Casbin實(shí)現(xiàn)角色的權(quán)限控制,感興趣的可以了解下2021-06-06
Go來合并兩個(gè)csv的實(shí)現(xiàn)示例
本文主要介紹了Go來合并兩個(gè)csv的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
源碼解析gtoken替換jwt實(shí)現(xiàn)sso登錄
這篇文章主要為大家介紹了源碼解析gtoken替換jwt實(shí)現(xiàn)sso登錄的示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

