项目程序结构
项目完成效果
main.go
// 系统程序入口
package main
import (
App "Account/core"
)
func main() {
App.CreatFamilyAccount().Runing()
}
Account/core/familyAccount.go
// 当前收支明细记录
package core
import (
"fmt"
"sort"
"strconv"
"time"
)
// 财务接口,收入,支出,明细。
// 下面家庭账户全部实现。
type finance interface {
// 收支明细记录
showlog()
// 收入
receivables()
// 支出
pay()
}
// 家庭账户
type acccount struct {
name string
accID int
balance float64
}
// 交易信息
type trade struct {
order string
ftype string
money float64
balance float64
desc string
date int
time int
}
type tradeDetail []trade
// 排序
func (td tradeDetail) Len() int {
return len(td)
}
func (td tradeDetail) Less(i, j int) bool {
return td[i].order > td[j].order
}
func (td tradeDetail) Swap(i, j int) {
td[i], td[j] = td[j], td[i]
}
// 结构体切片,用来存储收支明细
var accountLog tradeDetail
var dateFormate = "2006-01-02 15:04:05"
var menu string = `
-----------欢迎进入家庭收支记账系统--------
1.收支明细
2.收 入
3.支 出
4.退 出
请选择(1-4)>>> `
/**
* 工厂模式
* 创建家庭账户
*/
func CreatFamilyAccount() *acccount {
return &acccount{
name: "帅哥之家",
accID: 20198899,
balance: 0.0,
}
}
/**
* [Runing 进入家庭收支记账系统]
* @author Jhou Shuai
* @datetime 2019-06-09T13:44:50+0800
*/
func (this *acccount) Runing() {
// 是否退出标志位
var flag bool = false
// 声明或定义一个账户
for {
// 显示菜单 并返回编号
var item = this.showMenu()
switch item {
case 1:
this.showlog()
case 2:
this.receivables()
case 3:
this.pay()
case 4:
flag = this.exit()
default:
this.otherService()
}
if flag {
break
}
}
}
/**
* 显示菜单
*/
func (acc *acccount) showMenu() int8 {
// 菜单编号
var item int8
// 打印菜单,供选择
fmt.Print(menu)
fmt.Scanln(&item)
return item
}
/**
* [otherService 其他服务,暂未开放...]
* @author Jhou Shuai
* @datetime 2019-06-09T13:27:14+0800
*/
func (acc *acccount) otherService() {
fmt.Println("目前系统不支持此项服务,请重新输入!")
}
/**
* 退出系统
*/
func (acc *acccount) exit() bool {
var ok string
fmt.Print("您确定要退出么,请输入[y]退出系统,其他任意继续使用! >>> ")
fmt.Scanln(&ok)
if ok == "y" {
fmt.Println()
fmt.Println("[成功退出]家庭收支记账系统!**********")
return true
} else {
return false
}
}
/**
* [showlog 收支明细记录]
* @author Jhou Shuai
* @datetime 2019-06-09T10:15:38+0800
*/
func (acc *acccount) showlog() {
fmt.Println("-------------------当前收支明细记录------------------")
if len(accountLog) > 0 {
fmt.Println("收支\t账户金额\t收支金额\t收支说明\t日期")
sort.Sort(accountLog)
for _, v := range accountLog {
fmt.Printf("%v\t%0.2f\t%8.2f\t%v\t%v\n", v.ftype, v.balance, v.money, v.desc, v.date)
}
} else {
fmt.Println("暂无记录...")
}
}
/**
* [receivables 收入]
* @author Jhou Shuai
* @datetime 2019-06-09T10:15:54+0800
*/
func (acc *acccount) receivables() {
var desc string
var money float64
var now = time.Now()
oderID := now.Format("20060102150405")
tradDate, _ := strconv.Atoi(now.Format("20060102"))
tradTime, _ := strconv.Atoi(now.Format("150405"))
fmt.Print("本次收入金额>>> ")
fmt.Scanln(&money)
fmt.Print("本次收入说明>>> ")
fmt.Scanln(&desc)
var balance = acc.balance + money
acc.balance = balance
var trade = trade{
order: oderID,
ftype: "收入",
balance: balance,
money: money,
desc: desc,
date: tradDate,
time: tradTime,
}
accountLog = append(accountLog, trade)
}
/**
* [pay 支出]
* @author Jhou Shuai
* @datetime 2019-06-09T10:17:18+0800
*/
func (acc *acccount) pay() {
var desc string
var money float64
var now = time.Now()
oderID := now.Format("20060102150405")
tradDate, _ := strconv.Atoi(now.Format("20060102"))
tradTime, _ := strconv.Atoi(now.Format("150405"))
fmt.Print("本次支出金额>>> ")
fmt.Scanln(&money)
fmt.Print("本次支出说明>>> ")
fmt.Scanln(&desc)
if acc.balance < money {
fmt.Println("余额不足,不能做支出,记账操作失败!")
return
}
var balance = acc.balance - money
acc.balance = balance
var trade = trade{
order: oderID,
ftype: "支出",
balance: balance,
money: money,
desc: desc,
date: tradDate,
time: tradTime,
}
accountLog = append(accountLog, trade)
}
网友评论