将代码用面对对象的方式转换

初始代码:

package main

import(
	"fmt"
)

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

	//定义账户的余额[]
	balcance := 10000.0
	//每次收支的金额
	money := 0.0
	//每次收支的说明
	note := ""
	//收支的详情请使用字符串记录
	//当有收支时,只需要对details进行拼接处理
	details := "收支\t账户金额\t收支金额\t说   明"


	//显示菜单
	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)
			balcance += money//修改账户金额
			fmt.Println("本次收入说明:")
			fmt.Scanln(&note)
			//将这个收入情况,拼接到detail变量
			details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balcance,money,note)

		case "3":
			fmt.Println("本次支出金额:")
			fmt.Scanln(&money)
			if money > balcance{
				fmt.Println("余额不足")
				break
			}
			balcance -= money
			fmt.Println("本次支出说明:")
			fmt.Scanln(&note)
			details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balcance,money,note)

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

		default :
		    fmt.Println("请输入正确的选项")
			
		}
		if !loop {
			break
		}
	}
	fmt.Println("你退出了软件")
}

这是用面对对象的方法后改进的代码:

  把记账的功能封装到一个结构体中,然后调用该结构体的方法。

package main

import(
	"go_code/xmkf/demo01"
	"fmt"
)

func main(){
	fmt.Println("面对对象的方式")
	utils.NewFamilyAccount().MainMenu()
	
}
package utils

import(
	"fmt"
)

type FamilyAccount struct{
	//声明一个变量,保存接收用户输入的选项
	key string
	//声明一个变量,控制是否退出for
	loop bool

	//定义账户的余额[]
	balcance float64
	//每次收支的金额
	money float64
	//每次收支的说明
	note string

	flag bool
	//收支的详情请使用字符串记录
	//当有收支时,只需要对details进行拼接处理
	details string
}

//编写要给工厂模式的构造方法
func NewFamilyAccount() *FamilyAccount{
	return &FamilyAccount{
		key : "",
		loop : true,
		balcance : 10000,
		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(this.details)
	}			
}

//将登记收入写成一个方法
func (this *FamilyAccount) income(){
	fmt.Println("本次收入金额:")
	fmt.Scanln(&this.money)
	this.balcance += this.money//修改账户金额
	fmt.Println("本次收入说明:")
	fmt.Scanln(&this.note)
    //将这个收入情况,拼接到detail变量
	this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v",this.balcance,this.money,this.note)
	this.flag = true
}

//将登记支出写成一个方法
func (this *FamilyAccount) pay(){
	fmt.Println("本次支出金额:")
	fmt.Scanln(&this.money)
	if this.money > this.balcance{
		fmt.Println("余额不足")
			}
	this.balcance -= this.money
	fmt.Println("本次支出说明:")
	fmt.Scanln(&this.note)
	this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v",this.balcance,this.money,this.note)
	this.flag = true
}

//将退出系统写成方法
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
	}
	

}

//给该结构体绑定方法
//显示主菜单
func (this *FamilyAccount) MainMenu(){
	//显示菜单
	for {
		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
		}
	}

}
func main(){
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老实憨厚的腾锅锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值