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
| package util
import ( "fmt" )
type familyAccount struct { choice string // 用户输入 loop bool // 控制是否退出for循环 flag bool // 当有收支行为是,置为true money float64 // 发生收支行为的金额 note string // 发生收支行为的说明 balance float64 // 余额 detail string }
func NewFamilyAccount() *familyAccount { return &familyAccount{ choice: "", // 用户输入 loop: true, // 控制是否退出for循环 flag: false, // 当有收支行为是,置为true money: 0.0, // 发生收支行为的金额 note: "", // 发生收支行为的说明 balance: 0.0, // 余额 detail: "收支\t收支金额\t帐户余额\t说明", // 记录所有收支明细 }
}
func (this *familyAccount) showdetails() { fmt.Println("\n--------------当前收支明细记录--------------") if this.flag { fmt.Printf(this.detail) } else { fmt.Println("还没有收支明细,来一笔吧") } }
func (this *familyAccount) income() { fmt.Println("本次收入金额:") fmt.Scan(&this.money) fmt.Println("本次收入说明:") fmt.Scan(&this.note) this.balance += this.money this.detail += fmt.Sprintf("\n收入\t%v\t%v\t%v", this.money, this.balance, this.note) this.flag = true } func (this *familyAccount) pay() { fmt.Println("本次支出金额:") fmt.Scan(&this.money) if this.money <= this.balance { this.balance -= this.money fmt.Println("本次支出说明:") fmt.Scan(&this.note) this.detail += fmt.Sprintf("\n支出\t%v\t%v\t%v", this.money, this.balance, this.note) this.flag = true } else { fmt.Println("您的余额不足,不能完成此笔支出") } }
func (this *familyAccount) exit() { for { fmt.Println("确定退出系统吗y/n?") fmt.Scan(&this.choice) if this.choice == "y" || this.choice == "n" { break } fmt.Println("您的输入有误") } if this.choice == "y" { this.loop = false }
} func (this *familyAccount) MainMenu() { for { fmt.Println("\n--------------家庭收支记帐软件--------------") fmt.Println("1-收支明细") fmt.Println("2-登记收入") fmt.Println("3-登记支出") fmt.Println("4-退出程序") fmt.Println("请选择(1-4):") fmt.Scan(&this.choice) switch this.choice { case "1": this.showdetails() case "2": this.income() case "3": this.pay() case "4": this.exit() default: fmt.Println("输入不正确,请输入正确选项") } if this.loop == false { fmt.Printf("感谢您使用家庭收支记账软件") break } }
}
|