11.1. 家庭收支记账软件

11.1.1. 面向过程代码示例

package main

import "fmt"

func main() {
    key := ""
    loop := true
    //定义账户的余额
    balance := 10000.0
    //每次收支的金额
    money := 0.0
    //每次收支的说明
    note := ""
    // 定义个变量,记录是否有收支的行为
    flag := false

    details := "收支\t账户余额\t收支金额\t\t说 明"
    for {
        fmt.Println("-----------------家庭收支记账软件-------------")
        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变量
            details += fmt.Sprintf("\n收入\t%v\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(&note)
            details += fmt.Sprintf("\n支出\t%v\t%v\t\t%v", balance, money, note)
            flag = true

        case "4":
            fmt.Println("你确定要退出吗? y/n")
            choice := ""
            for {
                fmt.Scanln(&choice)
                if choice == "y" || choice == "n" {
                    if choice == "y" {
                        loop = false
                    }
                    break
                }
                fmt.Println("你的输入有误,请重新输入 y/n")
            }

        default:
            fmt.Println("请输入正确的选项..")
        }
        if !loop {
            break
        }
    }
    fmt.Println("你退出家庭记账软件的使用...")
}

11.1.2. 面向对象代码示例

utils/familyAccount.go

package utils

import (
    "fmt"
)

type FamilyAccount struct {
    key     string  //接收用户输入
    loop    bool    //控制是否退出for
    balance float64 //账户的余额
    money   float64 //每次收支的金额
    note    string  //每次收支的说明
    flag    bool    //是否有收支的行为
    details string  //收支的详细使用字段记录
}

//编写要给工厂模式的构造方法,返回一个*FamilyAccount 实例
func NewFamilyAccount() *FamilyAccount {
    return &FamilyAccount{
        key:     "",
        loop:    true,
        balance: 10000.0,
        money:   0.0,
        note:    "",
        flag:    false,
        details: "收支\t账户余额\t收支金额\t\t说   明",
    }
}

/*显示当前收支明细方法*/
func (this *FamilyAccount) showDetails() {
    fmt.Println("----------------当前收支明细记录----------------")
    if this.flag {
        fmt.Println(this.details)
    } else {
        fmt.Println("当前没有收支明细.... 来一笔吧!")
    }
}

/*登记收入写成一个方法,和*FamilyAccount绑定*/
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\t%v", this.balance, this.money, this.note)
    this.flag = true
}

/*将登记支出写成一个方法,和*FamilyAccount 绑定*/
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%v\t\t%v", this.balance, this.money, this.note)
    this.flag = true
}

/*将退出系统写成一个方法,和*FamilyAccount 绑定*/
func (this *FamilyAccount) exit() {
    fmt.Println("你确定要退出吗? y/n")
    choice := ""
    for {
        fmt.Scanln(&choice)
        if choice == "y" || choice == "n" {
            if choice == "y" {
                this.loop = false
                break
            }
        }
        fmt.Println("你的输入有误,请重新输入 y/n")
    }

}

//给该结构体绑定相应的方法
//显示主菜单
func (this *FamilyAccount) MianMenu() {
    for {
        fmt.Println("-----------------家庭收支记账软件-------------")
        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
        }
    }
    fmt.Println("你退出家庭记账软件的使用...")
}

main/main.go

package main

import (
    "fmt"
    "github.com/go_hexin01/day34/utils"
)

func main() {
    fmt.Println("*****面向对象完成家庭开支系统~*****")
    utils.NewFamilyAccount().MianMenu()     //思路非常清晰
}