Go 小项目1 - 家庭收支记账软件
一)需求
1)模拟实现基于文本界面的《家庭记账软件》
2)该软件能够记录家庭的收入、支出,并能够打印收支明细表
二)编码
1)功能
1、完成主菜单的编写
2、显示明细和登记收入
3、登记支出
三)实现
实现1:
teatMyAccount.go
package main
import (
"fmt"
)
func main() {
key := "" //声明一个变量,保存接收用户输入的选项
loop := false //声明一个变量用来判断是否要退出
balance := 10000.0 //声明账户的余额
money := 0.0 //声明每次收支的金额
note := "" //每次收支的说明
details :="收 支\t账户余额\t收支金额\t说 明" //收支的详情
//显示主菜单
for {
fmt.Println("\n----------家庭收支记账软件----------")
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("\n----------当前收支明细记录----------")
if details == "收 支\t账户余额\t收支金额\t说 明" {
fmt.Println("当前没有收支情况。。。")
break
}
fmt.Println(details)
fmt.Println("")
case "2":
fmt.Println("\n----------登记收入----------")
fmt.Println("本次收入金额:")
fmt.Scanln(&money)
balance += money
fmt.Println("本次收入说明:")
fmt.Scanln(¬e)
details += fmt.Sprintf("\n收 入\t%v\t%v\t%v",balance,money,note)
fmt.Println("")
case "3":
fmt.Println("\n----------登记支出----------")
fmt.Println("本次支出金额:")
fmt.Scanln(&money)
//需判断money与balance的大小
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)
fmt.Println("")
case "4":
fmt.Println("您是否选择退出家庭记账本?y/n")
choice := ""
fmt.Scanln(&choice)
if choice == "y"{
loop = true
}else{
loop=false
}
default:
fmt.Println("请输入正确的选项。。。")
fmt.Println("")
}
if loop {
break
}
}
fmt.Println("您退出了家庭记账软件")
}
实现2:
account.go
package account
import "fmt"
type Account struct {
key string //声明一个变量,保存接收用户输入的选项
loop bool //声明一个变量用来判断是否要退出
balance float64 //声明账户的余额
money float64 //声明每次收支的金额
note string //每次收支的说明
details string //收支的详情
flag bool //记录是否有收支情况
}
//编写工厂模式的构造方法,返回一个*Account实例
func NewAccount() *Account {
return &Account{
key: "",
loop: false,
balance: 10000.0,
money: 0.0,
note: "",
flag: false,
details:"收 支\t账户余额\t收支金额\t说 明",
}
}
//显示明细
func (this *Account)showDetails() {
fmt.Println("\n----------当前收支明细记录----------")
if !this.flag {
fmt.Println("当前没有收支情况。。。")
}else{
fmt.Println(this.details)
}
}
//登记收入
func (this *Account)income() {
fmt.Println("\n----------登记收入----------")
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%v",this.balance,this.money,this.note)
this.flag = true
}
//登记支出
func (this *Account)pay() {
fmt.Println("\n----------登记支出----------")
fmt.Println("本次支出金额:")
fmt.Scanln(&this.money)
//需判断money与balance的大小
if this.money > this.balance {
fmt.Println("账户余额不足")
}else{
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)
this.flag = true
}
}
//退出
func (this *Account)exit() {
fmt.Println("您是否选择退出家庭记账本?y/n")
choice := ""
fmt.Scanln(&choice)
if choice == "y"{
this.loop = true
}else{
this.loop=false
}
}
//给该结构体绑定相应的功能
//显示主菜单
func (this *Account)Menu() {
for {
fmt.Println("\n----------家庭收支记账软件----------")
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()
fmt.Println("")
case "2":
this.income()
fmt.Println("")
case "3":
this.pay()
fmt.Println("")
case "4":
this.exit()
default:
fmt.Println("请输入正确的选项。。。")
fmt.Println("")
}
if this.loop {
break
}
}
}
familyAccount.go
package main
import (
"familyAccount/account"
"fmt"
)
func main() {
username :=""
passord :=""
fmt.Println("请输入用户名:")
fmt.Scanln(&username)
fmt.Println("请输入密 码:")
fmt.Scanln(&passord)
if username == "zmh" && passord =="123" {
account.NewAccount().Menu()
}else {
fmt.Println("用户名或密码错误")
}
}