1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| package utils
import ( "fmt" )
type FamilyAccount struct { key string loop bool balance float64 money float64 note string flag bool details string }
func NewFamilyAccount() *FamilyAccount {
return &FamilyAccount{ key: "", loop: true, balance: 10000.0, money: 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("当前没有收支明细,来一笔吧!") } }
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%v", this.balance, this.money, this.note) this.flag = true }
func (this *FamilyAccount) pay() { fmt.Println("本次支出金额:") fmt.Scanln(&this.money)
if this.money > this.balance { fmt.Println("余额不足") } 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 *FamilyAccount) exit() { fmt.Println("确认退出吗? 确定退出请输入y 否则输入n") choice := "" for { fmt.Scanln(&choice) if choice == "y" { this.loop = false break } else if choice == "n" { break } fmt.Println("输入有误,请重新输入 y/n") } }
func (this *FamilyAccount) MainMenu() {
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() case "2": this.income() case "3": this.pay() case "4": this.exit() default: fmt.Println("请输入正确的选项..") }
if !this.loop { break } }
}
|