[GO language basics] Go project combat: Realizing household income and expenditure accounting software (18) Family income and expenditure accounting software

Project development process

  1. Demand analysis (30%)
  2. Software design (20%)
  3. coding & testing (20%)
  4. Online release
  5. Later operation and maintenance

Project requirement description

  1. Simulate the realization of "Family Accounting Software" based on text interface
  2. The software can record the income and expenditure of the family, and can print the income and expenditure schedule

interface design

-----------------家庭收支记账软件-----------------    
                    1 收支明细      
                    2 登记收入   
                    3 登记支出    
                    4 退出软件    

                    请选择(1-4):

Code

Process-oriented implementation

  • Function 1: Finish first to display the main menu and exit

Analysis of the train of thought:
After finishing the given interface, the main menu is displayed, when the user enters 4, the program will be exited

    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" :

                case "2" :

                case "3" :

                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("你退出家庭记账软件的使用...")
    }
  • Function 2: complete the function that can display details

Thinking analysis:
because we need to display details, we define a variable details string to record

    func main() {
        //声明一个变量,保存接收用户输入的选项
        key := ""
        //声明一个变量,控制是否退出for
        loop := true

        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" :
                
                case "3" :

                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("你退出家庭记账软件的使用...")
    }
  • Function 3: Complete the function of registering income and expenditure

Analysis of ideas:
Variables need to be defined to record the balance (balance), the amount of each income and expenditure (money), and the description of each income and expenditure (note)

    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("你退出家庭记账软件的使用...")
    }

Object-oriented implementation

Thinking analysis:

  1. Encapsulate the function of the accounting software into a structure, and then call the method of the structure to realize accounting and display the details. The name of the structure FamilyAccount.
    type FamilyAccount struct {
        //声明必须的字段.

        //声明一个字段,保存接收用户输入的选项
        key  string
        //声明一个字段,控制是否退出for
        loop bool
        //定义账户的余额 []
        balance float64
        //每次收支的金额
        money float64
        //每次收支的说明
        note string
        //定义个字段,记录是否有收支的行为
        flag bool
        //收支的详情使用字符串来记录
        //当有收支时,只需要对details 进行拼接处理即可
        details string
    }

    //编写要给工厂模式的构造方法,返回一个*FamilyAccount实例
    func NewFamilyAccount() *FamilyAccount { 

        return &FamilyAccount{
            key : "",
            loop : true,
            balance : 10000.0,
            money : 0.0,
            note : "",
            flag : false,
            details : "收支\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)
        //将这个收入情况,拼接到details变量
        //收入    11000           1000            有人发红包
        this.details += fmt.Sprintf("\n收入\t%v\t%v\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%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" {
                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.showDetails()
                case "2":
                    this.income()
                case "3":
                    this.pay()
                case "4":
                    this.exit()
                default :
                    fmt.Println("请输入正确的选项..")		
            }

            if !this.loop {
                break 
            }

        }
    }

2. In the main method, create an instance of the structure FamilyAccount to realize accounting.

package main
    import (
        "familyaccount/utils"
        "fmt"
    )
    func main() {
        utils.NewFamilyAccount().MainMenu()
    }

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/114006221