golang示例项目 家庭收支软件需求和界面

需求说明:

模拟实现基于文本界面的《家庭记账软件》

该软件能够记录家庭的收入、支出,并能够打印收支明细表

项目采用分级菜单方式。主菜单如下:

----------家庭收支记账软件---------

      1 收支明细

      2 登记收入

      3 登记支出

      4 退  出

      请选择(1-4):_

 

1.面向过程的方法实现:

复制代码
package main

import "fmt"

func main() {
    key := ""
    loop := true
    detile := "收支\t账户金额\t收支金额\t说\t明"
    balance := 10000.00
    money := 0.0
    note := ""
    //设置收支的标志符
    flag := false
    //显示主菜单:循环显示
    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("没有任何收支,请来一笔吧")
                break
            }
            fmt.Println(detile)
        case "2":
            fmt.Println("本次收入金额")
            fmt.Scanln(&money)
            balance += money
            fmt.Println("本次收入说明:")
            fmt.Scanln(&note)
            flag = true
            detile += fmt.Sprintf("\n收入\t%v\t%v\t%v", balance, money, note)

        case "3":
            fmt.Println("本次支出金额")
            fmt.Scanln(&money)
            if money > balance {
                fmt.Println("余额不足,不能进行支出")
                break
            }
            balance -= money
            fmt.Println("本次支出说明:")
            fmt.Scanln(&note)

            flag = true
            detile += fmt.Sprintf("\n收入\t%v\t%v\t%v", balance, money, note)
        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
        }
    }
}
复制代码

2.面向对象的方法实现:

将面向过程的代码修改成面向对象的方法,编写myFamilyAccount.go,并使用testMyFamilyAccount.go去完成测试

思路分析:

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

在通过在main方法中,创建一个MyFamilyAccount实例,实现记账

MyFamilyAccount.go
复制代码
package utils

import "fmt"

type MyFamilyAccount struct {
    key     string
    loop    bool
    detile  string
    balance float64
    money   float64
    note    string
    flag    bool
}

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

//显示明细
func (this *MyFamilyAccount) display() {
    fmt.Println("--------------当前收支明细--------------")
    if !this.flag {
        fmt.Println("没有任何收支,请来一笔吧")
    } else {
        fmt.Println(this.detile)
    }

}
func (this *MyFamilyAccount) incom() {
    fmt.Println("本次收入金额")
    fmt.Scanln(&this.money)
    this.balance += this.money
    fmt.Println("本次收入说明:")
    fmt.Scanln(&this.note)
    this.flag = true
    this.detile += fmt.Sprintf("\n收入\t%v\t%v\t%v", this.balance, this.money, this.note)
}
func (this *MyFamilyAccount) pay() {
    fmt.Println("本次支出金额")
    fmt.Scanln(&this.money)
    if this.money > this.balance {
        fmt.Println("余额不足,不能进行支出")
    } else {
        this.balance -= this.money
        fmt.Println("本次支出说明:")
        fmt.Scanln(&this.note)
    }

    this.flag = true
    this.detile += fmt.Sprintf("\n支出\t%v\t%v\t%v", this.balance, this.money, this.note)
}
func (this *MyFamilyAccount) 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 *MyFamilyAccount) MainMenu() {
    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.display()
        case "2":
            this.incom()
        case "3":
            this.pay()
        case "4":
            this.exit()
        default:
            fmt.Println("请输入正确的选项...")
        }
        if !this.loop {
            break
        }
    }
}
复制代码

main.go

package main

import "gotest1/src/test/test59/utils"

func main() {
    utils.NewMyFamilyAccount().MainMenu()
}

 

posted @   潇潇暮鱼鱼  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2022-03-10 istio安装与升级
点击右上角即可分享
微信分享提示