上一次说明了, 原来的项目分配不是很好, 我又改了一个(但是因为没有使用数据库存储不能做到多用户注册登录功能)
项目目录
day01/familyaccount/model/account.go
package model
import "fmt"
type AccountRecord struct {
Title string
Income float32
Desc string
Balance float32
}
type Account struct {
TotalAmount float32
Records []AccountRecord
Loop bool
Key string
}
// func NewFamilyAccount(name, pwd string) (act *Account) {
// for {
// if len(pwd) < 6 {
// fmt.Println("密码不能小于6位")
// continue
// }
// act = &Account{
// TotalAmount: 0,
// Records: []AccountRecord{},
// Loop: true,
// }
// return
// }
// }
func (this *Account) Income() {
defer func() {
// 将异常处理掉, 不要让程序崩溃
err := recover()
if err != nil {
fmt.Println("err=", err)
}
}()
fmt.Println("请输入收入金额, 以及说明, 以空格隔开")
var title string = "收入"
var income float32
var desc string
_, err := fmt.Scanf("%f %s
", &income, &desc)
if err != nil {
panic(err)
}
if income < 0 {
panic("你在开玩笑么")
}
this.TotalAmount += income
account := AccountRecord{
Title: title,
Balance: this.TotalAmount,
Income: income,
Desc: desc,
}
this.Records = append(this.Records, account)
// 收入完之后打印
this.Detail()
}
// Expenses 支出
func (this *Account) Expenses() {
defer func() {
// 将异常处理掉, 不要让程序崩溃
err := recover()
if err != nil {
fmt.Println("err=", err)
}
}()
fmt.Println("请输入支出金额以及说明,以空格隔开")
var title string = "支出"
var income float32
var desc string
_, err := fmt.Scanf("%f %s
", &income, &desc)
if err != nil {
panic(err)
}
if income < 0 {
panic("你在开玩笑么")
}
if this.TotalAmount < income {
panic(fmt.Sprintf("你想贷款么穷逼看看你的余额:%.2f", this.TotalAmount))
}
this.TotalAmount -= income
account := AccountRecord{
Title: title,
Balance: this.TotalAmount,
Income: income,
Desc: desc,
}
this.Records = append(this.Records, account)
// 收入完之后打印
this.Detail()
}
// Detail 详情
func (this *Account) Detail() {
defer func() {
// 将异常处理掉, 不要让程序崩溃
err := recover()
if err != nil {
fmt.Println("err=", err)
}
}()
fmt.Println("--------------------------当前收支明细几率--------------------------")
fmt.Println("收支 账户金额 收支金额 说 明 ")
for _, val := range this.Records {
fmt.Printf("%v %v %v %v
", val.Title, val.Balance, val.Income, val.Desc)
}
fmt.Println()
}
func (this *Account) Exit() {
fmt.Println("你确定要退出么? y/n")
choice := ""
for {
fmt.Scanln(&choice)
if choice == "y" || choice == "n" {
break
}
fmt.Println("输入错误,请重新输入 y/n")
}
if choice == "y" {
fmt.Println("退出程序")
this.Loop = false
}
}
day01/familyaccount/model/user.go
package model
import (
"fmt"
)
type User struct {
Id int
Name string
Pwd string
UserAccount *Account
}
func (this *User) String() {
fmt.Printf("用户id=[%d] 用户名=[%s]
", this.Id, this.Name)
}
day01/familyaccount/utils/main.go
package untils
import (
"day01/familyaccount/model"
"fmt"
)
func Menu() {
// 如果用户名密码不匹配,直接退出程序
var key string = ""
var user *model.User
for {
var err error
user, err = UserMgr.GetUser()
if err != nil {
fmt.Println("用户验证是失败, err: ", err)
continue
} else {
break
}
}
for {
account := user.UserAccount
key = account.Key
fmt.Println("--------------------家庭收支记账软件---------------------")
fmt.Println("--------------------1. 收支明细---------------------")
fmt.Println("--------------------2. 等级收入---------------------")
fmt.Println("--------------------3. 等级支出---------------------")
fmt.Println("--------------------4. 显示当前系统注册的用户---------------------")
fmt.Println("--------------------5. 退出---------------------")
fmt.Println("--------------------请选择(1-4): 1---------------------")
fmt.Scanln(&key)
if len(key) == 0 {
key = "1"
}
switch key {
case "1":
fmt.Println("打印收支明细")
account.Detail()
case "2":
account.Income()
case "3":
account.Expenses()
case "4":
UserMgr.ShowUser()
case "5":
account.Exit()
default:
fmt.Println("输入错误,请重新输入")
}
if !account.Loop {
break
}
key = ""
}
}
day01/familyaccount/utils/userMgr.go
package untils
import (
"day01/familyaccount/model"
"fmt"
)
type UserManager struct {
Users []*model.User
}
var (
UserMgr *UserManager
Idx int
)
func init() {
UserMgr = &UserManager{
Users: []*model.User{},
}
}
func (this *UserManager) ShowUser() {
if len(this.Users) == 0 {
fmt.Println("当前系统还没有用户")
return
}
for _, val := range this.Users {
fmt.Println(val)
}
}
func (this *UserManager) GetUser() (user *model.User, err error) {
var name, pwd string
fmt.Println("请输入用户名以及密码使用空格隔开")
_, err = fmt.Scanf("%s %s
", &name, &pwd)
if err != nil {
// fmt.Println("程序出错, 请不要着急, 联系工作人员")
return
}
isExists := false
for _, val := range this.Users {
if val.Name == name && val.Pwd == pwd {
user = val
isExists = true
break
}
}
if !isExists {
err = fmt.Errorf("用户[%d]不存在", name)
return
}
return
}
func (this *UserManager) Register(name, pwd string) bool {
if len(pwd) < 6 {
fmt.Println("密码不能小于6位")
return false
}
account := &model.Account{
TotalAmount: 0,
Records: []model.AccountRecord{},
Loop: true,
Key: "",
}
this.Users = append(this.Users, &model.User{
Name: name,
Pwd: pwd,
Id: Idx,
UserAccount: account,
})
return true
}
day01/familyaccount/main/user.go
package main
import (
utils "day01/familyaccount/utils"
"fmt"
)
func main() {
var name, pwd string
fmt.Println("请输入要注册的用户名和密码, 用空格隔开")
fmt.Scanf("%s %s
", &name, &pwd)
for {
flag := utils.UserMgr.Register(name, pwd)
if flag {
break
}
}
utils.Menu()
}