golang用户记账软件——面向对象

familyAccount.go文件里

package utils

import (
	"fmt"
)

type FamilyAccount struct {
	key string
	//循环显示主菜单
	loop bool
	//定义一个变量是否有收支行为
	flag bool
	//定义账户余额
	balance float64
	//每次收支的金额
	money float64
	//每次收支的说明
	note string
	//收支的详情
	details string
}

//将收支明细封装到一个方法

func (this *FamilyAccount) showDetails() {
	fmt.Println("---------------------当前收支明细-----------------------\n")
	if !this.flag {
		fmt.Println("当前没有收支明细,请来一笔吧")
	} else {
		fmt.Println(this.details)
	}
}
func (this *FamilyAccount) RegistShouru() {
	fmt.Print("本次收入金额:")
	fmt.Scan(&this.money)
	this.balance += this.money
	fmt.Print("本次收入说明:")
	fmt.Scan(&this.note)
	//将收入情况记录到details
	this.details += fmt.Sprintf("\n收入\t%v\t\t%v\t\t%v", this.balance, this.money, this.note)
	this.flag = true
}
func (this *FamilyAccount) RegistZhichu() {
	fmt.Print("本次支出金额:")
	fmt.Scan(&this.money)
	//做一个判断
	if this.money > this.balance {
		fmt.Println("支出余额不足")
	}
	this.balance -= this.money
	fmt.Print("本次支出说明:")
	fmt.Scan(&this.note)
	this.details += fmt.Sprintf("\n支出\t%v\t\t%v\t\t%v", this.balance, this.money, this.note)
	this.flag = true
}
func (this *FamilyAccount) Retire() {
	fmt.Print("\n你确定要退出吗?y/n\n")
	choice := ""
	for {
		fmt.Scan(&choice)
		if choice == "y" || choice == "n" {
			break
		}
		fmt.Print("您输入有误,请重新输入")
	}
	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.Println("请选择1-4")
		fmt.Scan(&this.key)
		switch this.key {

		case "1":
			this.showDetails()

		case "2":
			this.RegistShouru()
		case "3":
			this.RegistZhichu()
		case "4":
			this.Retire()
		default:
			fmt.Println("请输入正确的选项")
		}
		if !this.loop {
			break
		}
	}
}

func NewFamilyAccount() *FamilyAccount {

	return &FamilyAccount{
		key:     "",
		loop:    true,
		balance: 100000.0,
		money:   0.0,
		flag:    false,
		details: "收支\t账户金额\t收支金额\t说明",
	}
}

main.go文件里

package main

import (
	"fmt"

	"../utils"
)

func main() {
	fmt.Println("这是面向对象的方式完成的")
	utils.NewFamilyAccount().MainMenu()
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值