go project 家庭财务收支记录

  1. 面向过程的方式编写
  2. 面向对象的方式编写

面向过程的方式编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main

import (
"fmt"
)

func main() {
var choice string // 用户输入
loop := true // 控制是否退出for循环
flag := false // 当有收支行为是,置为true
var money float64 // 发生收支行为的金额
var note string // 发生收支行为的说明
var balance float64 // 余额
detail := "收支\t收支金额\t帐户余额\t说明" // 记录所有收支明细

for {

fmt.Println("\n--------------家庭收支记帐软件--------------")
fmt.Println("1-收支明细")
fmt.Println("2-登记收入")
fmt.Println("3-登记支出")
fmt.Println("4-退出程序")
fmt.Println("请选择(1-4):")
fmt.Scan(&choice)
switch choice {
case "1":
fmt.Println("\n--------------当前收支明细记录--------------")
if flag {
fmt.Printf(detail)
} else {
fmt.Println("还没有收支明细,来一笔吧")
}

case "2":
fmt.Println("本次收入金额:")
fmt.Scan(&money)
fmt.Println("本次收入说明:")
fmt.Scan(&note)
balance += money
detail += fmt.Sprintf("\n收入\t%v\t%v\t%v", money, balance, note)
flag = true
case "3":
fmt.Println("本次支出金额:")
fmt.Scan(&money)
if money <= balance {
balance -= money
fmt.Println("本次支出说明:")
fmt.Scan(&note)
detail += fmt.Sprintf("\n支出\t%v\t%v\t%v", money, balance, note)
flag = true
} else {
fmt.Println("您的余额不足,不能完成此笔支出")
}
case "4":
for {
fmt.Println("确定退出系统吗y/n?")
fmt.Scan(&choice)
if choice == "y" || choice == "n" {
break
}
fmt.Println("您的输入有误")
}
if choice == "y" {
loop = false
}

default:
fmt.Println("输入不正确,请输入正确选项")
}
if loop == false {
fmt.Printf("感谢您使用家庭收支记账软件")
break
}
}
}

面向对象的方式编写

目录结构:

1
2
3
4
family_account/
├── main.go
└── util
└── family_account.go

util/family_account.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package util

import (
"fmt"
)

type familyAccount struct {
choice string // 用户输入
loop bool // 控制是否退出for循环
flag bool // 当有收支行为是,置为true
money float64 // 发生收支行为的金额
note string // 发生收支行为的说明
balance float64 // 余额
detail string
}

func NewFamilyAccount() *familyAccount {
return &familyAccount{
choice: "", // 用户输入
loop: true, // 控制是否退出for循环
flag: false, // 当有收支行为是,置为true
money: 0.0, // 发生收支行为的金额
note: "", // 发生收支行为的说明
balance: 0.0, // 余额
detail: "收支\t收支金额\t帐户余额\t说明", // 记录所有收支明细
}

}

func (this *familyAccount) showdetails() {
fmt.Println("\n--------------当前收支明细记录--------------")
if this.flag {
fmt.Printf(this.detail)
} else {
fmt.Println("还没有收支明细,来一笔吧")
}
}

func (this *familyAccount) income() {
fmt.Println("本次收入金额:")
fmt.Scan(&this.money)
fmt.Println("本次收入说明:")
fmt.Scan(&this.note)
this.balance += this.money
this.detail += fmt.Sprintf("\n收入\t%v\t%v\t%v", this.money, this.balance, this.note)
this.flag = true
}
func (this *familyAccount) pay() {
fmt.Println("本次支出金额:")
fmt.Scan(&this.money)
if this.money <= this.balance {
this.balance -= this.money
fmt.Println("本次支出说明:")
fmt.Scan(&this.note)
this.detail += fmt.Sprintf("\n支出\t%v\t%v\t%v", this.money, this.balance, this.note)
this.flag = true
} else {
fmt.Println("您的余额不足,不能完成此笔支出")
}
}

func (this *familyAccount) exit() {
for {
fmt.Println("确定退出系统吗y/n?")
fmt.Scan(&this.choice)
if this.choice == "y" || this.choice == "n" {
break
}
fmt.Println("您的输入有误")
}
if this.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.choice)
switch this.choice {
case "1":
this.showdetails()
case "2":
this.income()
case "3":
this.pay()
case "4":
this.exit()
default:
fmt.Println("输入不正确,请输入正确选项")
}
if this.loop == false {
fmt.Printf("感谢您使用家庭收支记账软件")
break
}
}

}

main.go

1
2
3
4
5
6
7
8
9
10
package main

import (
"family_account/util"

)

func main() {
util.NewFamilyAccount().MainMenu()
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 rootid@126.com

文章标题:go project 家庭财务收支记录

本文作者:Shawn

发布时间:2019-05-26, 15:44:00

最后更新:2019-08-09, 14:16:15

原始链接:https://gitrootid.github.io/2019/05/26/golang/go-project-家庭财务收支记录_/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏