Go语言学习篇04

Go语言学习篇04

家庭收支记账软件项目

  • 项目开发流程介绍

  • 项目需求说明

  • 项目的界面

  • 项目代码实现

    • 实现基本功能(先使用面向过程,后面改成面向对象)

项目开发流程介绍

在这里插入图片描述

项目需求说明

  • 模拟实现基于文本界面的《家庭记账软件》
  • 该软件能够记录家庭的收入、支出,并能够打印收支明细表
  • 项目采用分级菜单方式。主菜单如下:
----------家庭收支记账软件----------
		1 收支明细
		2 登记收入
		3 登记支出
		4 退出

		请选择(1-4):

项目代码实现

先面向过程。再面向对象

  • 显示主菜单、并能正常退出
  • 完成显示明细的功能
  • 完成登记收入功能
  • 完成登记支出功能

面向过程

package main

import (
	"fmt"
	"strings"
)

func main() {
    
    
	//定义账户余额
	var balance = 10000.0
	//每次收支的金额
	var money float64 = 0.0
	//每次收支的说明
	var note string = ""
	//详情
	//当有收支,只需要进行details进行拼接就行
	var details string = "收支\t账户金额\t收支金额\t说明"
	//是否退出软件
	var exit string
	//是否进行收支,默认未收支
	var flag bool = false
	//显示主菜单
	var selected int
	flag01:
	for {
    
    
		fmt.Println("\n----------家庭收支记账软件----------")
		fmt.Println("\n            1  收支明细")
		fmt.Println("            2  登记收入")
		fmt.Println("            3  登记支出")
		fmt.Println("            4   退  出")
		fmt.Print("\n请选择(1-4):")
		fmt.Scanln(&selected)

		switch selected {
    
    
		case 1:
			fmt.Println("----------当前收支明细记录----------")
			if !flag {
    
    
				fmt.Println("当前没有任何收支...")
				break
			}
			fmt.Println(details)
		case 2:
			fmt.Print("本次收入金额:")
			fmt.Scanln(&money)
			fmt.Print("本次收入说明:")
			fmt.Scanln(&note)
			balance += money
			//将收入情况拼接到details中
			details += fmt.Sprintf("\n收入\t%v\t%v\t%v", balance, money, note)
			flag = true
		case 3:
			flag02:
			fmt.Printf("总额度%v\n", balance)
			fmt.Print("本次支出金额:")
			fmt.Scanln(&money)
			if money > balance {
    
    
				fmt.Println("你的余额不足...")
				flag04:
				fmt.Print("是否重新输入支出金额?(y/n):")
				fmt.Scanln(&exit)
				if strings.EqualFold(exit, "y") {
    
    
					//重新输入金额
					goto flag02
				} else if strings.EqualFold(exit, "n") {
    
    
					//退出switch语句
					break
				} else {
    
    
					fmt.Println("你的输入有误...")
					//重新选项
					goto flag04
				}
				flag = true
			}
			fmt.Print("本次支出说明:")
			fmt.Scanln(&note)
			balance-= money
			//将收入情况拼接到details中
			details += fmt.Sprintf("\n支出\t%v\t%v\t%v", balance, money, note)
		case 4:
			flag03:
			fmt.Print("你确定要退出吗?(y/n):")
			fmt.Scanln(&exit)
			if strings.EqualFold(exit, "y") {
    
    
				//退出for循环
				break flag01
			}else if strings.EqualFold(exit, "n") {
    
    
				//退出switch
				break
			} else {
    
    
				fmt.Println("你的输入有误...")
				//重新输入
				goto flag03
			}
		default:
			fmt.Println("你的输入有误...")
		}
	}
	fmt.Println("已退出")
}

面向对象

  • 思路
    • 将记账软件的功能,封装到结构体中,通过调用结构体的方法来实现软件

utils代码

package utils

import (
	"fmt"
	"strings"
)

type familyAccount struct {
    
    
	//是否退出软件,控制for循环
	loop bool
	//是否进行收支,默认未收支
	flag bool
	//定义账户余额
	balance float64
	//每次收支的金额
	money float64
	//每次收支的说明
	note string
	//收支详情
	details string
	//用户输入的选项
	selected string
}

//编写一个构造方法---工厂模式
func NewFamilyAccount() *familyAccount {
    
    
	return &familyAccount{
    
    
		loop:     true,
		flag:     false,
		balance:  10000.0,
		money:    0,
		note:     "",
		details:  "收支\t账户金额\t收支金额\t说明",
		selected: "",
	}
}


//给该结构体绑定相应的方法

//1、显示主菜单
func (family *familyAccount) MainMenu() {
    
    
	for {
    
    
		//清除上一次的记录,防止重复操作
		family.familyClear()
		fmt.Println("\n----------家庭收支记账软件----------")
		fmt.Println("\n            1  收支明细")
		fmt.Println("            2  登记收入")
		fmt.Println("            3  登记支出")
		fmt.Println("            4   退  出")
		fmt.Print("\n请选择(1-4):")
		fmt.Scanln(&family.selected)
		switch family.selected {
    
    
		case "1":
			family.ShowDetails()
		case "2":
			family.Income()
		case "3":
			family.Expenditure()
		case "4":
			family.ExitSoftware()
		default:
			fmt.Println("你的输入有误...")
		}
		//退出for循环
		if !family.loop {
    
    
			break
		}
	}
}

//2、显示收支明细
func (family *familyAccount) ShowDetails() {
    
    
	fmt.Println("----------当前收支明细记录----------")
	if family.flag {
    
    
		fmt.Println(family.details)
	} else {
    
    
		fmt.Println("当前没有任何收支...")
	}
}

//3、登记收入
func (family *familyAccount) Income() {
    
    
	fmt.Print("本次收入金额:")
	fmt.Scanln(&family.money)

	fmt.Print("本次收入说明:")
	fmt.Scanln(&family.note)

	//总金额
	family.balance += family.money
	//将收入拼接到details中
	family.details += fmt.Sprintf("\n收入\t%v\t%v\t%v", family.balance, family.money, family.note)
	//产生收支明细
	family.flag = true
}

//4、登记支出
func (family *familyAccount) Expenditure() {
    
    
	//显示可用总额度
	fmt.Printf("总额度%v\n", family.balance)

	fmt.Print("本次支出金额:")
	fmt.Scanln(&family.money)

	if family.money > family.balance {
    
    
		fmt.Println("你的余额不足...")
		return
	}

	fmt.Print("本次支出说明:")
	fmt.Scanln(&family.note)

	//计算得到支出后的总额度
	family.balance-= family.money
	//将收入拼接到details中
	family.details += fmt.Sprintf("\n支出\t%v\t%v\t%v", family.balance, family.money, family.note)
	//产生了收支明细
	family.flag = true
}

//5、退出
func (family *familyAccount) ExitSoftware()  {
    
    
	var choice string
	for {
    
    
		fmt.Print("你确定要退出吗?(y/n):")
		fmt.Scanln(&choice)
		if strings.EqualFold(choice, "y") || strings.EqualFold(choice, "n") {
    
    
			break
		}
		fmt.Println("你的输入有误...")
	}
	//判断客户是否选择Y进行退出
	if strings.EqualFold(choice, "y") {
    
    
		//表示退出
		family.loop = false
	}
}

//6、清除上一次操作记录
func (family *familyAccount) familyClear() {
    
    
	family.selected = ""
	family.money = 0.0
	family.note = ""
}

main代码

package main

import (
	"familyAccount/utils"
	"fmt"
)

type User struct{
    
    
	username string
	password string
}

func (user *User) Login() {
    
    
	if user.username == "carter" && user.password == "1122"{
    
    
		var familyAccount = utils.NewFamilyAccount()
		familyAccount.MainMenu()
		fmt.Println("已成功退出")
	} else {
    
    
		fmt.Println("用户名或密码不正确!!!")
	}
}

func main() {
    
    
	var user User
	fmt.Print("请输入用户名:")
	fmt.Scanln(&user.username)
	fmt.Print("请输入密码:")
	fmt.Scanln(&user.password)
	(&user).Login()
}

结果

----------家庭收支记账软件----------

            1  收支明细
            2  登记收入
            3  登记支出
            4   退  出

请选择(1-4)2
本次收入金额:6000
本次收入说明:工资

----------家庭收支记账软件----------

            1  收支明细
            2  登记收入
            3  登记支出
            4   退  出

请选择(1-4)1
----------当前收支明细记录----------
收支	账户金额	收支金额	说明
收入	16000	6000	工资

----------家庭收支记账软件----------

            1  收支明细
            2  登记收入
            3  登记支出
            4   退  出

请选择(1-4)3
总额度16000
本次支出金额:60
本次支出说明:看电影

----------家庭收支记账软件----------

            1  收支明细
            2  登记收入
            3  登记支出
            4   退  出

请选择(1-4)1
----------当前收支明细记录----------
收支	账户金额	收支金额	说明
收入	16000	6000	工资
支出	15940	60	看电影

----------家庭收支记账软件----------

            1  收支明细
            2  登记收入
            3  登记支出
            4   退  出

请选择(1-4)4
你确定要退出吗?(y/n):

客户信息管理系统项目

项目需求分析

1)模拟实现基于文本界面的《客户端信息管理软件》

2)能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表

程序框架图

在这里插入图片描述

项目结构

在这里插入图片描述

view

package main

import (
	"customterManage/model"
	"customterManage/service"
	"fmt"
	"strings"
)

type customerView struct {
    
    
	//接收客户输入
	key string
	//是否循环主菜单
	loop bool
	//customerService字段
	customerService *service.CustomerService
}

//显示主菜单
func (view *customerView) MainMenu() {
    
    
	for {
    
    
		//清除
		view.CustomerClear()
		fmt.Println("\n----------客户信息管理软件----------")
		fmt.Println("\n			1 添加客户")
		fmt.Println("			2 修改客户")
		fmt.Println("			3 删除客户")
		fmt.Println("			4 客户列表")
		fmt.Println("			5 退出")
		fmt.Print("\n请输入(1-5):")
		fmt.Scanln(&view.key)
		switch view.key {
    
    
		case "1":
			view.add()
		case "2":
			view.list()
			view.update()
		case "3":
			view.list()
			view.delete()
		case "4":
			view.list()
		case "5":
			view.Exit()
		default:
			fmt.Println("你的输入有误...")
		}
		//判断是否退出
		if !view.loop {
    
    
			break
		}
	}
}

//更新用户
func (view *customerView) update() {
    
    
	var id int = -1
	fmt.Print("\n输入你需要修改的用户编号:")
	fmt.Scanln(&id)

	if id == -1 {
    
    
		return
	}

	oldCustomer, index := view.customerService.FindCustomer(id)
	if index == -1 && oldCustomer == nil {
    
    
		fmt.Println("你需要修改的用户不存在...")
		return
	}

	var name string = ""
	var gender string = ""
	var age int = 0
	var phone string = ""
	var email string = ""

	fmt.Printf("姓名(%v):", oldCustomer.Name)
	fmt.Scanln(&name)
	fmt.Printf("性别(%v):", oldCustomer.Gender)
	fmt.Scanln(&gender)
	fmt.Printf("年龄(%v):", oldCustomer.Age)
	fmt.Scanln(&age)
	fmt.Printf("电话(%v):", oldCustomer.Phone)
	fmt.Scanln(&phone)
	fmt.Printf("邮箱(%v):", oldCustomer.Email)
	fmt.Scanln(&email)

	//使用工厂模式
	var newCustomer = model.NewCustomer(id, name, gender, age, phone, email)
	flag := view.customerService.Update(index, &newCustomer)

	if flag {
    
    
		fmt.Println("修改成功...")
	} else {
    
    
		fmt.Println("修改失败...")
	}
}

//删除客户
func (view *customerView) delete() {
    
    
	var id int = -1
	fmt.Print("\n请输入需删除的用户编号:")
	fmt.Scanln(&id)
	if id == -1 {
    
    
		return //放弃删除操作
	}
	var choice string = ""
	fmt.Print("是否删除?(y/n):")
	for {
    
    
		fmt.Scanln(&choice)
		if strings.EqualFold(choice, "y") || strings.EqualFold(choice, "n") {
    
    
			break
		}
		fmt.Print("输入有误,是否删除?(y/n):")
	}

	if strings.EqualFold(choice, "y") {
    
    
		flag := view.customerService.Delete(id)
		if flag {
    
    
			fmt.Println("删除成功...")
		} else {
    
    
			fmt.Println("删除失败,客户id不存在...")
		}
	}
}

//增加客户
func (view *customerView) add() {
    
    
	var name string = ""
	var gender string = ""
	var age int = 0
	var phone string = ""
	var email string = ""

	fmt.Print("姓名:")
	fmt.Scanln(&name)
	fmt.Print("性别:")
	fmt.Scanln(&gender)
	fmt.Print("年龄:")
	fmt.Scanln(&age)
	fmt.Print("电话:")
	fmt.Scanln(&phone)
	fmt.Print("邮箱:")
	fmt.Scanln(&email)
	//使用工厂模式
	var newCustomer = model.NewCustomerNoId(name, gender, age, phone, email)
	flag := view.customerService.Add(newCustomer)
	if flag {
    
    
		fmt.Println("添加客户成功...")
	} else {
    
    
		fmt.Println("添加失败...")
	}
}

//客户列表
func (view *customerView) list(){
    
    
	customers := view.customerService.List()
	fmt.Println("\n------------客户列表------------")
	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
	for _, customer := range customers {
    
    
		fmt.Println(&customer)
	}
}

//退出功能
func (view *customerView) Exit() {
    
    
	var choice string
	fmt.Print("是否退出?(y/n):")
	for {
    
    
		fmt.Scanln(&choice)
		if strings.EqualFold(choice, "y") || strings.EqualFold(choice, "n") {
    
    
			break
		}
		fmt.Print("输入有误,是否退出?(y/n):")
	}
	//输入y表示退出
	if strings.EqualFold(choice, "y") {
    
    
		view.loop = false
	}
}

//清除功能
func (view *customerView) CustomerClear() {
    
    
	view.key = ""
}

func main() {
    
    
	var view = customerView{
    
    
		key:  "",
		loop: true,
	}
	view.customerService = service.NewCustomerService()
	view.MainMenu()
}

service

package service

import (
	"customterManage/model"
)

//完成对Customer的操作,增删改查
type CustomerService struct {
    
    
	customers []model.Customer
	//当前切片含有多少个客户,可以作为新客户的编号
	customerNum int
}

//工厂模式
func NewCustomerService() *CustomerService {
    
    
	var customerService *CustomerService = new(CustomerService)
	customerService.customerNum = 1
	var customer model.Customer = model.NewCustomer(1, "廖述幸", "男", 20, "15675288363", "[email protected]")
	customerService.customers = append(customerService.customers, customer)
	return customerService
}

//客户列表
func (customer *CustomerService) List() []model.Customer {
    
    
	return (*customer).customers
}

//增加
func (customer *CustomerService) Add(newCustomer model.Customer) bool {
    
    
	//分配Id规则
	(*customer).customerNum++
	newCustomer.Id = (*customer).customerNum
	//切片添加元素
	(*customer).customers = append((*customer).customers, newCustomer)
	return true
}

//删除
func (customer *CustomerService) Delete(id int) bool {
    
    
	index := customer.FindById(id)
	if index == -1 {
    
    
		return false
	}
	//删除切片对应下标
	customer.customers = append(customer.customers[:index],customer.customers[index+1:]...)
	return true
}

//修改
func (customer *CustomerService) Update(index int, newCustomer *model.Customer) bool {
    
    
	(*customer).customers[index] = *newCustomer
	return true
}

//查找切片下标
func (customer *CustomerService) FindById(id int) int {
    
    
	//查询客户Id
	for index, customer := range customer.customers {
    
    
		if id == customer.Id {
    
    
			//返回下标,进行删除操作
			return index
		}
	}
	//-1表示不存在
	return -1
}

//查找用户
func (customer *CustomerService) FindCustomer(id int) (oldCustomer *model.Customer,index int) {
    
    
	for index, customer := range (*customer).customers {
    
    
		if customer.Id == id {
    
    
			return &customer,index
		}
	}
	return nil,-1
}

model

package model

import "fmt"

//定义声明一个Customer结构体,表示客户信息
type Customer struct{
    
    
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

//使用工厂实例
func NewCustomer(id int, name string, gender string,
	age int, phone string, email string) Customer {
    
    
	return Customer{
    
    
		Id:     id,
		Name:   name,
		Gender: gender,
		Age:    age,
		Phone:  phone,
		Email:  email,
	}
}

//不带Id的工厂模式
func NewCustomerNoId(name string, gender string,
	age int, phone string, email string) Customer {
    
    
	return Customer{
    
    
		Name:   name,
		Gender: gender,
		Age:    age,
		Phone:  phone,
		Email:  email,
	}
}

//用户信息格式化输出
func (customer *Customer) String() string {
    
    
	return fmt.Sprintf("%v\t\t%v\t%v\t\t%v\t%v\t%v",
		customer.Id, customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email)
}

结果

----------客户信息管理软件----------

			1 添加客户
			2 修改客户
			3 删除客户
			4 客户列表
			5 退出

请输入(1-5):4

------------客户列表------------
编号	姓名	性别	年龄	电话	邮箱
1		廖述幸	男		20	15675288363	2672439345@qq.com

----------客户信息管理软件----------

			1 添加客户
			2 修改客户
			3 删除客户
			4 客户列表
			5 退出

			...

one: phone,
Email: email,
}
}

//不带Id的工厂模式
func NewCustomerNoId(name string, gender string,
age int, phone string, email string) Customer {
return Customer{
Name: name,
Gender: gender,
Age: age,
Phone: phone,
Email: email,
}
}

//用户信息格式化输出
func (customer *Customer) String() string {
return fmt.Sprintf("%v\t\t%v\t%v\t\t%v\t%v\t%v",
customer.Id, customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email)
}




**结果**

~~~go
----------客户信息管理软件----------

			1 添加客户
			2 修改客户
			3 删除客户
			4 客户列表
			5 退出

请输入(1-5):4

------------客户列表------------
编号	姓名	性别	年龄	电话	邮箱
1		廖述幸	男		20	15675288363	[email protected]

----------客户信息管理软件----------

			1 添加客户
			2 修改客户
			3 删除客户
			4 客户列表
			5 退出

			...

猜你喜欢

转载自blog.csdn.net/IT_Carter/article/details/110467768