项目开发流程说明
项目需求说明
- 模拟实现基于文本界面的《家庭记账软件》
- 该软件能够记录家庭的收入、支出,并能够打印收支明细表
项目的界面
项目代码实现
实现基本功能(先使用面向过程,后面改成面向对象)
- 功能 1:先完成可以显示主菜单,并且可以退出
思路分析:
更加给出的界面完成,主菜单的显示, 当用户输入 4 时,就退出该程序
package main
import (
"fmt"
)
func main(){
//声明一个变量,接受用户输入选项
key := ""
//声明一个变量控制是否退出for循环
loop := true
//显示主菜单
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("-----------------当前收支明细记录-----------------")
case "2":
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 := ""
//声明一个变量控制是否退出for循环
loop := true
//显示主菜单
//定义账户于额
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("-----------------当前收支明细记录-----------------")
fmt.Println(details)
case "2":
fmt.Println("本次收入金额:")
fmt.Scanln(&money)
balance += money
fmt.Println("本次收入说明:")
fmt.Scanln(¬e)
//将这个收入情况,拼接到details变量
//收入 11000 1000 有人发红包
details += fmt.Sprintf("\n收入\t%v\t\t%v\t\t%v", balance, money, note)
case "3":
fmt.Println("本次支出金额:")
case "4":
loop = false
default:
fmt.Println("请输入正确选项。。。")
}
if !loop{
break
}
}
fmt.Println("你退出家庭记账软件的使用...")
}
- 功能 3:完成了登记支出的功能
思路分析:
登记支出的功能和登录收入的功能类似,做些修改即可
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\t%v\t\t%v", balance, money, note)
项目代码实现改进
- 用户输入 4 退出时,给出提示"你确定要退出吗? y/n",必须输入正确的 y/n ,否则循环输入指 令,直到输入 y 或者 n
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("请输入正确选项。。。")
}
- 当没有任何收支明细时,提示 “当前没有收支明细… 来一笔吧!”
case "1":
fmt.Println("\n-----------------当前收支明细记录-----------------")
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\t%v\t\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\t%v\t\t%v", balance, money, note)
flag = true
- 将 面 向 过 程 的 代 码 修 改 成 面 向 对 象 的 方 法 , 编 写 myFamilyAccount.go , 并 使 用
testMyFamilyAccount.go 去完成测试
思路分析:
把记账软件的功能,封装到一个结构体中,然后调用该结构体的方法,来实现记账,显示明细。结 构体的名字 FamilyAccount .
在通过在 main 方法中,创建一个结构体 FamilyAccount 实例,实现记账即可.
package main
import (
"go_code/familyaccount/utils"
"fmt"
)
func main(){
fmt.Println("这个面向对象方法完成")
utils.NewFamilyAccount().MainMenu()
}
package utils
import (
"fmt"
)
type FamilyAccount struct{
//声明一个字段,接受用户输入选项
key string
//声明一个字段控制是否退出for循环
loop bool
//显示主菜单
//定义账户于额
balance float64
//每次收支的金额
money float64
//每次收支的说名
note string
//定义一个变量,记录是否有收支
flag bool
//每次收支的详情使用字符串来记录
details string
}
//编写一个工厂模式的构造方法,返回一个FamilyAccount实例
func NewFamilyAccount() *FamilyAccount{
return &FamilyAccount{
key : "",
loop : true,
balance : 1000.0,
money : 0.0,
note : "",
flag : false,
details : "收支\t账户金额\t收支金额\t说 明",
}
}
//将显示明细写成一个方法
func (this *FamilyAccount) showdatiles() {
fmt.Println("\n-----------------当前收支明细记录-----------------")
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)
//将这个收入情况,拼接到details变量
//收入 11000 1000 有人发红包
this.details += fmt.Sprintf("\n收入\t%v\t\t%v\t\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("余额不足")
//break
}
this.balance -= this.money
fmt.Println("本次支出说明:")
fmt.Scanln(&this.note)
this.details += fmt.Sprintf("\n收入\t%v\t\t%v\t\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" || choice == "n"{
break
}
fmt.Println("你的输入有误,请重新输入 y/n")
}
if 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.Print(" 请选择(1-4):")
fmt.Scanln(&this.key)
switch this.key{
case "1":
this.showdatiles()
case "2":
this.income()
case "3":
this.pay()
case "4":
this.exit()
default:
fmt.Println("请输入正确选项。。。")
}
if !this.loop{
break
}
}
fmt.Println("你退出家庭记账软件的使用...")
}