Go-家庭收支記賬軟件項目實現(xiàn)
項目開發(fā)流程說明

項目需求說明
模擬實現(xiàn)基于文本界面的《家庭記賬軟件》該軟件能夠記錄家庭的收入、支出,并能夠打印收支明細表
項目的界面



項目代碼實現(xiàn)
實現(xiàn)基本功能(先使用面向過程,后面改成面向?qū)ο?
功能 1:先完成可以顯示主菜單,并且可以退出
思路分析:
根據(jù)給出的界面完成,主菜單的顯示, 當用戶輸入 4時,就退出該程序
走代碼:
package main
import "fmt"
func main(){
key := ""
loop := true
for{
fmt.Println("-------------------家庭收支記賬軟件-------------------")
fmt.Println(" 1 收支明細")
fmt.Println(" 2 登記收入")
fmt.Println(" 3 登記支出")
fmt.Println(" 4 退出軟件")
fmt.Print("請選擇(1-4):")
fmt.Scanln(&key)
switch key {
case "1":
fmt.Println("-------------------當前收支明細-------------------")
case "2":
fmt.Println(" 登記收入")
case "3":
fmt.Println(" 登記支出")
case "4":
loop = false
default:
fmt.Println("請輸入正確的選項!")
}
if !loop{
break
}
}
fmt.Println("退出該軟件!")
}功能 2:完成可以顯示明細和登記收入的功能
思路分析:
因為需要顯示明細,我們定義一個變量 details string 來記錄還需要定義變量來記錄余額(balance)、每次收支的金額(money), 每次收支的說明(note)
package main
import "fmt"
func main() {
key := ""
loop := true
balance := 10000.0
money := 0.0
note := ""
details := "收支\t賬戶金額\t收支金額\t說 明"
for {
fmt.Println("-------------------家庭收支記賬軟件-------------------")
fmt.Println(" 1 收支明細")
fmt.Println(" 2 登記收入")
fmt.Println(" 3 登記支出")
fmt.Println(" 4 退出軟件")
fmt.Print("請選擇(1-4):")
fmt.Scanln(&key)
switch key {
case "1":
fmt.Println("-------------------當前收支明細-------------------")
fmt.Println(details)
case "2":
fmt.Println("本次收入金額:")
fmt.Scanln(&money)
balance += money
fmt.Println("本次收入說明")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n收入\t %v \t %v \t \t %v", balance, money, note)
fmt.Println(" 登記收入")
case "3":
fmt.Println(" 登記支出")
case "4":
loop = false
default:
fmt.Println("請輸入正確的選項!")
}
if !loop {
break
}
}
fmt.Println("退出該軟件!")
}功能 3:完成了登記支出的功能
思路分析:
登記支出的功能和登錄收入的功能類似,做些修改即可
走代碼:
package main
import "fmt"
loop:=true
balance := 10000.0
money := 0.0
note := ""
details := "收支\t賬戶金額\t收支金額\t說 明"
for {
fmt.Println("-------------------家庭收支記賬軟件-------------------")
fmt.Println(" 1 收支明細")
fmt.Println(" 2 登記收入")
fmt.Println(" 3 登記支出")
fmt.Println(" 4 退出軟件")
fmt.Print("請選擇(1-4):")
fmt.Scanln(&key)
switch key {
case "1":
fmt.Println("-------------------當前收支明細-------------------")
fmt.Println(details)
case "2":
fmt.Println("本次收入金額:")
fmt.Scanln(&money)
balance += money
fmt.Println("本次收入說明")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n收入\t %v \t %v \t \t %v", balance, money, note)
fmt.Println(" 登記收入")
case "3":
fmt.Println("本次支出金額:")
fmt.Scanln(&money)
balance -= money
fmt.Println("本次支出說明")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n支出\t %v \t %v \t \t %v", balance, money, note)
fmt.Println(" 登記支出")
case "4":
loop = false
default:
fmt.Println("請輸入正確的選項!")
}
if !loop {
break
}
}
fmt.Println("退出該軟件!")項目代碼實現(xiàn)改進
#### 12.4.2 項目代碼實現(xiàn)改進
1) 用戶輸入 4 退出時,給出提示"你確定要退出嗎? y/n",必須輸入正確的 y/n ,否則循環(huán)輸入指
令,直到輸入 y 或者 n

2) 當沒有任何收支明細時,提示 "當前沒有收支明細... 來一筆吧!"

3) 在支出時,判斷余額是否夠,并給出相應的提示

````go
package main
import "fmt"
func main() {
key := ""
loop := true
balance := 10000.0
money := 0.0
note := ""
flag := false
details := "收支\t賬戶金額\t收支金額\t說 明"
for {
fmt.Println("-------------------家庭收支記賬軟件-------------------")
fmt.Println(" 1 收支明細")
fmt.Println(" 2 登記收入")
fmt.Println(" 3 登記支出")
fmt.Println(" 4 退出軟件")
fmt.Print("請選擇(1-4):")
fmt.Scanln(&key)
switch key {
case "1":
fmt.Println("-------------------當前收支明細-------------------")
if flag {
fmt.Println(details)
} else {
fmt.Println("當前沒有明細,來一筆吧!")
}
fmt.Println(details)
case "2":
fmt.Println("本次收入金額:")
fmt.Scanln(&money)
balance += money
fmt.Println("本次收入說明")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n收入\t %v \t %v \t \t %v", balance, money, note)
fmt.Println(" 登記收入")
flag = true
case "3":
fmt.Println("本次支出金額:")
fmt.Scanln(&money)
if money>balance{
fmt.Println("余額的金額不足!")
break
}
balance -= money
fmt.Println("本次支出說明")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n支出\t %v \t %v \t \t %v", balance, money, note)
fmt.Println(" 登記支出")
flag = true
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("請輸入正確的選項!")
}
if !loop {
break
}
}
fmt.Println("退出該軟件!")
}
將 面 向 過 程 的 代 碼 修 改 成 面 向 對 象 的 方 法 , 編 寫 myFamilyAccount.go , 并 使 用 testMyFamilyAccount.go 去完成測試
思路分析:
把記賬軟件的功能,封裝到一個結構體中,然后調(diào)用該結構體的方法,來實現(xiàn)記賬,顯示明細。
結構體的名字 FamilyAccount .
在通過在 main方法中,創(chuàng)建一個結構體 FamilyAccount 實例,實現(xiàn)記賬即可.
model
package model
import "fmt"
type FamilyAccount struct {
key string
loop bool
money float64
balance float64
note string
flag bool
details string
}
func NewFamilyAccount() *FamilyAccount {
return &FamilyAccount{
key: "",
loop: true,
money: 0.0,
balance: 0.0,
note: "",
flag: false,
details: "收支\t賬戶金額\t收支金額\t說 明",
}
}
func (this *FamilyAccount) showDetails() {
fmt.Println("-------------------當前收支明細-------------------")
if this.flag {
fmt.Println(this.details)
} else {
fmt.Println("當前沒有明細,來一筆吧!")
}
fmt.Println(this.details)
}
func (this *FamilyAccount) income() {
fmt.Println("本次收入金額:")
fmt.Scanln(&this.money)
this.balance += this.money
fmt.Println("本次收入說明")
fmt.Scanln(&this.note)
this.details += fmt.Sprintf("\n收入\t %v \t %v \t \t %v", this.balance, this.money, this.note)
fmt.Println(" 登記收入")
this.flag = true
}
func (this *FamilyAccount) outcome() {
fmt.Println("本次支出金額:")
fmt.Scanln(&this.money)
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 \t %v", this.balance, this.money, this.note)
fmt.Println(" 登記支出")
this.flag = true
}
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
}
}
func (this *FamilyAccount) MainMenu() {
for {
fmt.Println("-------------------家庭收支記賬軟件-------------------")
fmt.Println(" 1 收支明細")
fmt.Println(" 2 登記收入")
fmt.Println(" 3 登記支出")
fmt.Println(" 4 退出軟件")
fmt.Print("請選擇(1-4):")
fmt.Scanln(&this.key)
switch this.key{
case"1":
this.showDetails()
case"2":
this.income()
case"3":
this.outcome()
case"4":
this.exit()
default:
fmt.Println("請輸入正確選項..")
}
if!this.loop{
break
}
}
}main
package main
import (
"fmt"
"go_code/go_code/chapter12/model"
)
func main() {
fmt.Println("面向?qū)ο?)
model.NewFamilyAccount().MainMenu()
}對項目的擴展功能的練習
- 對上面的項目完成一個轉(zhuǎn)賬功能
- 在使用該軟件前,有一個登錄功能,只有輸入正確的用戶名和密碼才能操作.
model
package model
import "fmt"
type User struct {
Username string
Pwd string
key string
loop bool
money float64
balance float64
note string
flag bool
details string
}
func NewUser() *User {
return &User{
key: "",
loop: true,
money: 0.0,
balance: 0.0,
note: "",
flag: false,
details: "收支\t賬戶金額\t收支金額\t說 明",
Pwd:"666666",
Username: "tianyi1",
}
}
func (this *User) showDetails() {
fmt.Println("-------------------當前收支明細-------------------")
if this.flag {
fmt.Println(this.details)
} else {
fmt.Println("當前沒有明細,來一筆吧!")
}
}
func (this *User) income() {
fmt.Println("本次收入金額:")
fmt.Scanln(&this.money)
this.balance += this.money
fmt.Println("本次收入說明")
fmt.Scanln(&this.note)
this.details += fmt.Sprintf("\n收入\t %v \t %v \t \t %v", this.balance, this.money, this.note)
fmt.Println(" 登記收入")
this.flag = true
}
func (this *User) outcome() {
fmt.Println("本次支出金額:")
fmt.Scanln(&this.money)
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 \t %v", this.balance, this.money, this.note)
fmt.Println(" 登記支出")
this.flag = true
}
func (this *User) 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
}
}
func (user *User) Login(username string, pwd string) {
for {
fmt.Println("請輸入用戶名")
fmt.Scanln(&user.Username)
if username !=user.Username {
fmt.Println("輸入的用戶名不正確,請重新輸入:")
user.Login("", "")
} else {
break
}
}
for {
fmt.Println("請輸入密碼")
fmt.Scanln(&user.Pwd)
if pwd!= user.Pwd {
fmt.Println("輸入的密碼不正確,請重新輸入:")
} else {
fmt.Println("恭喜您,進入系統(tǒng)!")
user.MainMenu()
}
}
}
//將轉(zhuǎn)賬寫成一個方法
func (user *User)Transfer(){
fmt.Println("------------------------轉(zhuǎn)賬功能-------------------")
fmt.Println("本次轉(zhuǎn)賬金額:")
fmt.Scanln(&user.money)
if user.money>user.balance{
fmt.Println("你的余額不足")
}
user.balance -=user.money
fmt.Println("本次轉(zhuǎn)賬說明:")
fmt.Scanln(&user.note)
user.details+=fmt.Sprintf("\n轉(zhuǎn)賬\t %v \t %v \t \t %v",user.balance, user.money, user.note)
}
func (user *User) MainMenu() {
for {
fmt.Println("-------------------家庭收支記賬軟件-------------------")
fmt.Printf("用戶%v已登錄\n", user.Username)
fmt.Println(" 1 收支明細")
fmt.Println(" 2 登記收入")
fmt.Println(" 3 登記支出")
fmt.Println(" 4 選擇轉(zhuǎn)賬")
fmt.Println(" 5 退出軟件")
fmt.Print("請選擇(1-5):")
fmt.Scanln(&user.key)
switch user.key {
case "1":
user.showDetails()
case "2":
user.income()
case "3":
user.outcome()
case "4":
user.Transfer()
case "5":
user.exit()
default:
fmt.Println("請輸入正確選項..")
}
if !user.loop {
break
}
}
}main
package main
import (
"fmt"
"go_code/go_code/chapter12/model"
)
func main() {
fmt.Println("這個是面向?qū)ο蟮姆绞酵瓿?.\n")
//調(diào)用登錄頁面Showcode
model.NewUser().Login("tianyi1","666666")
}以上就是Go-家庭收支記賬軟件項目實現(xiàn)的詳細內(nèi)容,更多關于Go項目實現(xiàn)的資料請關注腳本之家其它相關文章!
相關文章
golang使用excelize庫操作excel文件的方法詳解
Excelize是Go語言編寫的用于操作Office Excel文檔基礎庫,基于ECMA-376,ISO/IEC 29500國際標準,下面這篇文章主要給大家介紹了關于golang使用excelize庫操作excel文件的相關資料,需要的朋友可以參考下2022-11-11

