- 需求分析:懂技术 懂业务
- 设计阶段
- 实现阶段(代码)
- 测试阶段
- 实施阶段
- 维护阶段
家庭收记账
1 面向过程
package main
import "fmt"
func main() {
key:=""
loop :=true
balance:=10000.0
money:=0.0
note:=""
details:="收支\t账号金额\t收支金额\t 说 明:"
ifdomoney:= false
for{
fmt.Printf("****************家庭收记账软件****************\n")
fmt.Printf("----------------1 收支明细----------------\n")
fmt.Printf("----------------2 登记收入----------------\n")
fmt.Printf("----------------3 登记支出----------------\n")
fmt.Printf("----------------4 退出软件----------------\n")
fmt.Printf("请选择(1-4:)----------------")
fmt.Scanln(&key)
switch key {
case "1":
if ifdomoney{
fmt.Printf("----------------1 收支明细----------------\n")
fmt.Printf("%v ",details )
}else {
fmt.Println("无明细,请来一笔收支吧")
}
fmt.Printf("\n\n\n")
case "2":
fmt.Println("本次收入金额")
fmt.Scanln(&money)
balance+=money
fmt.Println("本次收入说明")
fmt.Scanln(¬e)
ifdomoney=true
details+=fmt.Sprintf("\n收入\t %v \t +%v \t %v",balance,money,note)
case "3":
fmt.Println("登记支出")
if money >balance{
fmt.Println("余额不足,无法支出")
break
}
fmt.Println("本次支出金额")
fmt.Scanln(&money)
balance-=money
fmt.Println("本次收入说明")
fmt.Scanln(¬e)
ifdomoney=true
details+=fmt.Sprintf("\n支出\t %v \t -%v \t %v",balance,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.Printf("input is error\n")
fmt.Println("loop为:",loop)
}
if !loop {
break
}
}
fmt.Printf("----------------已退出软件----------------")
}
面向对象:
package utils
import "fmt"
//封装到机构
type FamilyAccount struct {
key string
loop bool
balance float64
money float64
note string
details string
ifdomoney bool
}
func NewFamilyAccount() *FamilyAccount {
return &FamilyAccount{
key:"",
loop :true,
balance:10000.0,
money:0.0,
note:"",
details:"收支\t账号金额\t收支金额\t 说 明:",
ifdomoney:false,
}
}
func (f *FamilyAccount) showDetails(){
if f.ifdomoney{
fmt.Printf("----------------1 收支明细----------------\n")
fmt.Printf("%v ",f.details )
}else {
fmt.Println("无明细,请来一笔收支吧")
}
fmt.Printf("\n\n\n")
}
func (f *FamilyAccount) income() {
fmt.Println("本次收入金额")
fmt.Scanln(&f.money)
f.balance+=f.money
fmt.Println("本次收入说明")
fmt.Scanln(&f.note)
f.ifdomoney=true
f.details+=fmt.Sprintf("\n收入\t %v \t +%v \t %v",f.balance,f.money,f.note)
}
func (f *FamilyAccount) pay() {
fmt.Println("登记支出")
if f.money >f.balance{
fmt.Println("余额不足,无法支出")
//break
}
fmt.Println("本次支出金额")
fmt.Scanln(&f.money)
f.balance-=f.money
fmt.Println("本次收入说明")
fmt.Scanln(&f.note)
f.ifdomoney=true
f.details+=fmt.Sprintf("\n支出\t %v \t -%v \t %v",f.balance,f.money,f.note)
}
func (f *FamilyAccount) exit() {
fmt.Println("确定退出吗? y/n")
choice:=""
for {
fmt.Scanln(&choice)
if choice=="y"|| choice=="n"{
break
}
fmt.Println("输入有误")
}
if choice=="y"{
f.loop=false
}
}
func (f *FamilyAccount) ManiMenu() {
for{
fmt.Printf("****************家庭收记账软件****************\n")
fmt.Printf("----------------1 收支明细----------------\n")
fmt.Printf("----------------2 登记收入----------------\n")
fmt.Printf("----------------3 登记支出----------------\n")
fmt.Printf("----------------4 退出软件----------------\n")
fmt.Printf("请选择(1-4:)----------------")
fmt.Scanln(&f.key)
switch f.key {
case "1":
f.showDetails()
case "2":
f.income()
case "3":
f.pay()
case "4":
f.exit()
default:
fmt.Printf("input is error\n")
fmt.Println("loop为:",f.loop)
}
if !f.loop {
break
}
}
fmt.Printf("----------------已退出软件----------------")
}
func main() {
}
mian.go
package main
import (
"awesomeProject/oopTest1/utils"
)
func main() {
utils.NewFamilyAccount().ManiMenu()
}