Go:个人收支记账

需求分析

需求说明

记录个人收入支出并能够打印收支明细表(登录、收入或支出、账户金额、收支金额、收支说明)
首先能实现用户登录功能,成功后进入用户功能选择菜单
采用菜单方式 并实现以下功能:
1.收支明细
2.登记收入
3.登记支出
4.转账
5.退出

设计阶段

项目界面

打开程序先登录
登录后有菜单选择功能
其他略

代码实现

main.go

package main

import (
	Family1"code/Family1/utils"
)
func main() {
	Family1.NewFamilyAccount().LoginStart()
}

FamilyAccount.go

package utils

import (
"fmt"
	"os"
	"time"
)
type FamilyAccount struct {
	    //登录账号
	    account string
	    //登录密码
	    pwd string
	    //选择功能
		key string
		//定义账户余额
		balance float64
		//每次收支的金额
		money float64
		//控制是否退出菜单(程序)
		loop bool
		//每次收支的说明
		note string
		//收支明细
		detail string
		//初始化下面上的一行不显示
		flag bool
		//显示菜单
}
//工厂模式的构造方法,返回一个*FamilyAccount实例
func NewFamilyAccount() *FamilyAccount{
		return &FamilyAccount{
			account:"monkey",
			pwd:"12345678",
			key:"",
			loop:true,
			balance:10000,
			money:0.0,
			note:"",
			flag:false,
			detail:"收支\t账户余额\t收支金额\t收支说明",
	    }
}

//登录功能
func (this *FamilyAccount) Login(account string,pwd string){
	for{
		fmt.Print("请输入你的账号:")
		fmt.Scanln(&account)
		fmt.Print("请输入你的密码:")
		fmt.Scanln(&pwd)
		if pwd != this.pwd||account !=this.account{
			fmt.Println("输入的账号或者密码不正确!请重新输入..")
			this.Login("","")
		}else{
			fmt.Println("欢迎进入记账软件!")
			this.MainMenu()
			break
		}
	}
	this.LoginStart()
}
//登录界面
func (this *FamilyAccount)LoginStart(){
	choice :=""
	fmt.Println("1.登录系统")
	fmt.Println("2.退出系统")
	fmt.Print("请选择功能:")
	fmt.Scanln(&choice)
	switch choice{
	case "1":
		this.Login("","")
	case "2":
		os.Exit(1)
	}
}
//收支明细界面
func (this *FamilyAccount)showDetails(){
		fmt.Println("-----------------当前收支明细--------------------")
		if this.flag{
			fmt.Println(this.detail)
		}else{
			fmt.Println("当前没有收支明细..请进行登记")
		}
}
//登录退出
func (this *FamilyAccount) exit(){
		fmt.Println("你确定要退出此账号吗?y/n")
		choice := ""
		for{
			fmt.Scanln(&choice)
			if choice == "y"||choice == "n"{
				break
			}
			fmt.Println("你的输入错误,请重新输入")
		}
		if choice == "y"{
			this.loop  = false
		}
}
//收入登记界面
func (this *FamilyAccount) income(){
		fmt.Println("登记收入..")
		fmt.Print("本次收入金额:")
		fmt.Scanln(&this.money)
		this.balance += this.money
		fmt.Print("本次收入说明:")
		fmt.Scanln(&this.note)
		//将收入情况记录到detail变量,拼接
		this.detail += fmt.Sprintf("\n收入\t %v \t\t %v \t\t%v",this.balance,this.money,this.note)
		this.flag = true
}
//支出登记界面
func (this *FamilyAccount) pay(){
		fmt.Println("登记支出..")
		fmt.Print("本次支出金额:")
		fmt.Scanln(&this.money)
		if this.money >this.balance{
			fmt.Println("你的余额不足!")
		}else{
			this.balance -= this.money
			fmt.Print("本次支出说明:")
			fmt.Scanln(&this.note)
			this.detail += fmt.Sprintf("\n支出\t %v \t\t %v \t\t%v",this.balance,this.money,this.note)
			this.flag = true
		}
}
//转账界面
func (this *FamilyAccount) Transfer(){
		fmt.Println("转账页面..")
		fmt.Print("本次转账金额:")
		fmt.Scanln(&this.money)
		if this.money >this.balance{
			fmt.Println("你的金额不足,请重新输入")
		}else{
			cnt := 1
			newpwd :=""
			for{
				fmt.Println("请输入正确的密码:")
				fmt.Scanln(&newpwd)
				if newpwd == this.pwd{
					this.balance -= this.money
					fmt.Printf("转账成功!,成功转账%v元\n",this.money)
					break
				}else{
					if cnt>=3{
						fmt.Println("密码输入3次错误,请10秒后再输入...")
						time.Sleep(time.Second*10)
					}else{
						fmt.Println("密码输入错误!")
						cnt++
					}
				}
			}
		}
}
//给结构体绑定方法
func (this *FamilyAccount) MainMenu(){
		for{
			fmt.Println("\n-----------------个人收支记账软件--------------------")
			fmt.Println("                   1.收支明细")
			fmt.Println("                   2.登记收入")
			fmt.Println("                   3.登记支出")
			fmt.Println("                   4.转账")
			fmt.Println("                   5.退出登录")
			fmt.Print("                   请选择1-5:")
			fmt.Scanln(&this.key)
			switch this.key {
			case "1":
				this.showDetails()
			case "2":
				this.income()
			case "3":
				this.pay()
			case "4":
				this.Transfer()
			case "5":
				this.exit()
			default:
				fmt.Println("请输入正确的选项")
			}
			if !this.loop{
				break
			}
		}
}

测试阶段

暂时略

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值