go-家庭收支记账软件例子

家庭收支记账软件项目

项目需求说明

  1. 模拟实现基于文本界面的《家庭记账软件》
  2. 该软件能够记录家庭的收入、支出,并能够打印收支明细表

项目的界面

看代码效果

项目代码实现

实现基本功能(先使用面向过程,后面改成面向对象)

功能 1: 先完成可以显示主菜单,并且 可以退出

思路分析:

更加给出的界面完成,主菜单的显示, 当用户输入 4 时,就退出该程序

功能 2:完成可以 显示明细和 登记收入的功能

  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(&note)
                                //将这个收入情况,拼接到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(&note)
                                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 去完成测试

思路分析:

把记账软件的功能,封装到一个结构体中,然后调用该结构体的方法,来 实现记账, 显示明细。结构体的名字 FamilyAccount .

在通过在 main 方法中,创建一个结构体 FamilyAccount 实例,实现记账即可.

main.go

package main
import (
        "go_code/code/account/utils"
        "fmt"
)
func main() {
        fmt.Println("面向对象的方式来完成.....")
        utils.NewMyFamilyAccount().MainMenu()
}

myFamilyAccount.go

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
        }
}