接口封装
package utils
import (
"fmt"
)
//定义结构体
type FamilyAccount struct{
key string
loop bool
balance float64
money float64
note string
details string
flags bool
}
//编写要给工厂模式的构造方法
func NewFamilyAccount() *FamilyAccount{
return &FamilyAccount{
key:"",
loop:true,
balance:10000.0,
money:0.0,
note:"",
flags:false,
details:"收支\t账户金额\t收支金额\t说 明\t",
}
}
//主体业务逻辑
func (this*FamilyAccount)InitMainPage(){
fmt.Println("---------------家庭收支记账软件---------------")
fmt.Println("---------------1,收支明细---------------")
fmt.Println("---------------2,登记收入---------------")
fmt.Println("---------------3,登记支出---------------")
fmt.Println("---------------4,退出软件---------------")
fmt.Println("----------------------------------------")
fmt.Println("请选择1-4:")
for{
fmt.Scanln(&this.key)
switch this.key {
case "1":
this.ShowDetail()
case "2":
this.inCome()
case "3":
this.outCome()
case "4":
this.logout()
default:
selectErr()
}
if !this.loop {
break
}
}
fmt.Println("---------------你选择了退出软件,再会!---------------")
}
//收支明细
func (this*FamilyAccount)ShowDetail(){
fmt.Println("---------------1,收支明细---------------")
if this.flags{
fmt.Println(this.details)
} else{
fmt.Println("当前没有收支记录,来记录一笔吧!")
}
}
//登记收入
func (this*FamilyAccount)inCome(){
fmt.Println("---------------2,登记收入---------------")
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)
this.flags = true
}
//支出记录
func (this*FamilyAccount)outCome(){
fmt.Println("---------------3,登记支出---------------")
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)
this.flags = true
}
//退出软件
func (this*FamilyAccount)logout(){
fmt.Println("---------------4,退出软件---------------")
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 selectErr(){
fmt.Println("---------------你的选择不正确---------------")
}
调用方式
package main
import(
"fmt"
"project/utils"
)
func main(){
fmt.Println("面向对象编程模式,接口封装")
utils.NewFamilyAccount().InitMainPage()
}