go学习part13 收支软件小项目

229_尚硅谷_家庭收支软件-显示主菜单_哔哩哔哩_bilibili

软件开发六阶段

1.预期界面

该项目主要是在控制台显示与操作

几个界面:

2.代码实现流程

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

编写文件TestMyAccount.go完成基本功能
功能1:先完成可以显示主菜单,并旦可以退出

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

package main

import "fmt"

func main() {
	//一个变量存储用户输入
	key := ""
	//账户余额
	balance := 10000.0
	//每次收支金额
	money := 0.0
	//每次收支说明
	note := ""
	//收支详情字符串,当有收支时只需要进行字符串拼接就行
	details := "收支\t账户金额\t收支金额\t说  明"

label1:
	for {

		fmt.Println("---------家庭收支记账软件-----------")
		fmt.Println("         1 收支明细")
		fmt.Println("         2 登记收入")
		fmt.Println("         3 登记支出")
		fmt.Println("         4 退    出")
		fmt.Println("         请选择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(&note)
			details += 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)
			details += 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("输入有误,重新输入")
			}
			if choice == "y" {
				break label1
			}

		default:
			fmt.Println("请输入正确选项")
		}

	}

}

(2)分析

1)因为需要显示明细,我们定义一个变量details string来记录
2)还需要定义变量来记录余额(balance)、每次收支的金额(money).每次收支的说明(note)
 

3.OOP面向对象实现

package main

import (
	"fmt"
	"gocode/testproject01/unit12/OOP收支项目/utils"
)

func main() {
	fmt.Println("OOP")
	utils.NewFamilyAccount().MainMenu()

}

package utils

import "fmt"

type FamilyAccount struct {
	//字段
	//一个变量存储用户输入
	key string
	//跳出标志
	loop bool
	//账户余额
	balance float64
	//每次收支金额
	money float64
	//每次收支说明
	note string
	//收支详情字符串,当有收支时只需要进行字符串拼接就行
	details string
}

//工厂模式构造方法,返回*FamilyAccount
func NewFamilyAccount() *FamilyAccount {
	return &FamilyAccount{
		key:     "",
		loop:    true,
		balance: 10000.0,
		money:   0.0,
		note:    "",
		details: "收支\t账户金额\t收支金额\t说  明",
	}

}

func (this *FamilyAccount) showDetails() {
	fmt.Println("---------当前收支明细-----------")
	fmt.Println(this.details) //输出记录即可
}

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%v", this.balance, this.money, this.note)
}

func (this *FamilyAccount) pay() {
	fmt.Println("本次支出金额:")
	fmt.Scanln(&(this.money))
	if this.money > this.balance {
		fmt.Println("余额不足")
		return
	}
	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)
}

func (this *FamilyAccount) exit() {
	fmt.Println("确定退出吗?y/n")
	choice := ""
	for {
		fmt.Scanln(&choice)
		if choice == "y" || choice == "n" {
			break
		}
		fmt.Println("输入有误,重新输入")
	}
	if choice == "y" {
		this.loop = false
		//break label1
	}
}

func (this *FamilyAccount) MainMenu() {
	//label1:
	for {
		//fmt.Println()
		fmt.Println("---------家庭收支记账软件-----------")
		fmt.Println("         1 收支明细")
		fmt.Println("         2 登记收入")
		fmt.Println("         3 登记支出")
		fmt.Println("         4 退    出")
		fmt.Println("         请选择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
		}

	}

}

目录结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值