1) 模拟实现基于文本界面的《家庭记帐软件》
2) 该软件可以记录家庭的收入、支出,并可以打印收支明细表oop
看代码效果测试
功能 1: 先完成能够显示主菜单,而且 能够退出
思路分析:
更加给出的界面完成,主菜单的显示, 当用户输入 4 时,就退出该程序this
功能 2:完成能够 显示明细和 登记收入的功能code
1) 由于须要显示明细,咱们定义一个变量 details string 来记录
2) 还须要定义变量来记录余额(balance)、每次收支的金额(money), 每次收支的说明(note)对象
功能 3:完成了登记支出的功能
思路分析:
登记支出的功能和登陆收入的功能相似,作些修改便可字符串
package main import ( "fmt" ) func main() { //声明一个变量,保存接收用户输入的选项 key := "" //声明一个变量,控制是否退出for loop := true //定义帐户的余额 [] balance := 10000.0 //每次收支的金额 money := 0.0 //每次收支的说明 note := "" //定义个变量,记录是否有收支的行为 flag := false //收支的详情使用字符串来记录 //当有收支时,只须要对details 进行拼接处理便可 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("-----------------当前收支明细记录-----------------") if flag { fmt.Println(details) } else { fmt.Println("当前没有收支明细... 来一笔吧!") } case "2": fmt.Println("本次收入金额:") fmt.Scanln(&money) balance += money // 修改帐户余额 fmt.Println("本次收入说明:") fmt.Scanln(¬e) //将这个收入状况,拼接到details变量 //收入 11000 1000 有人发红包 details += fmt.Sprintf("\n收入\t%v\t%v\t%v", balance, money, note) 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%v", balance, money, note) 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("你退出家庭记帐软件的使用...") }
1) 用户输入 4 退出时,给出提示"你肯定要退出吗? y/n",必须输入正确的 y/n ,不然循环输入指令,直到输入 y 或者 n
2) 当没有任何收支明细时,提示 "当前没有收支明细... 来一笔吧!"
3) 在支出时,判断余额是否够,并给出相应的提示
4) 将 面 向 过 程 的 代 码 修 改 成 面 向 对 象 的 方 法 , 编 写 myFamilyAccount.go , 并 使 用testMyFamilyAccount.go 去完成测试string
思路分析:
把记帐软件的功能,封装到一个结构体中,而后调用该结构体的方法,来 实现记帐, 显示明细。结构体的名字 FamilyAccount .
在经过在 main 方法中,建立一个结构体 FamilyAccount 实例,实现记帐便可.it
main.goclass
package main import ( "go_code/code/account/utils" "fmt" ) func main() { fmt.Println("面向对象的方式来完成.....") utils.NewMyFamilyAccount().MainMenu() }
myFamilyAccount.gotest
package utils import ( "fmt" ) type MyFamilyAccount struct { // 定义一个字段 loop bool // 用于接收用户的输入 key string // 记录用户的收入和支出状况,该字符串会拼接 details string // 保存帐号的金额 balance float64 // 定义一个标识符 flag bool // 定义一个金额 money float64 // 声明一个说明 note string } func NewMyFamilyAccount() *MyFamilyAccount { return &MyFamilyAccount{ loop : true, key : "", details : "收支\t帐户金额\t收支金额\t说 明", balance : 10000.0, flag : false, money : 0.0, note : "", } } func (this *MyFamilyAccount) MainMenu() { for { // 1. 先输出这个主菜单 fmt.Println("-----------------家庭收支记帐软件-----------------") fmt.Println(" 1 收支明细") fmt.Println(" 2 登记收入") fmt.Println(" 3 登记支出") fmt.Println(" 4 退出") fmt.Print("请选择(1-4):") fmt.Scanln(&this.key) // 使用switch switch (this.key) { case "1": this.showDetails() case "2": this.income() case "3": this.pay() case "4": this.loop = this.exit() default: fmt.Println("请输入正确的选项...") } if !this.loop { break } } fmt.Println("你退出了软件的使用。。。。") } //显示收支明细的方法 func (this *MyFamilyAccount) showDetails() { // 增长我代码。。 fmt.Println("-----------------当前收支明细记录-----------------") if this.flag { fmt.Println(this.details) } else { fmt.Println("没有任何收支明细") } } //登记收入 func (this *MyFamilyAccount) income() { fmt.Print("本次收入金额:") fmt.Scanln(&this.money) fmt.Print("本次收入说明:") fmt.Scanln(&this.note) this.balance += this.money this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v", this.balance, this.money, this.note) this.flag = true } //登记支出 func (this *MyFamilyAccount) pay() { fmt.Print("本次支出金额:") fmt.Scanln(&this.money) fmt.Print("本次支出说明:") fmt.Scanln(&this.note) //判断 if this.money > this.balance { fmt.Println("朋友,余额不足 ...") return // 终止一个方法使用return } this.balance -= this.money this.details += fmt.Sprintf("\n支出\t%v\t%v\t%v", this.balance, this.money, this.note) this.flag = true } //退出 func (this *MyFamilyAccount) exit() bool { // 修改loop fmt.Println("肯定要退出吗?y/n"); choice := "" //循环判断,直到输入y 或者 n for { fmt.Scanln(&choice) if choice == "y" || choice == "n" { break } fmt.Print("你的输入有误,请输入y/n:") } if choice == "y" { return false } else { return true } }