go学习之简单项目

项目

文章目录

1.项目开发流程图

2.家庭收支记账软件项目

1)需求说明

  • 模拟实现基于文本界面的《家庭记账软件》

  • 该软件能够记录家庭的收入、支出,并能够打印收支明细表

  • 项目采用分级菜单的方式,主菜单如下:

    --------家庭收支记账软件-------
             1.收支明细
             2.登记收入
             3.登记支出
             4.退出
             
             请选择(1-4):
    
2)项目代码实现

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

编写文件TestMyAccount.go 完成基本功能

  1. 功能1:先完成可以显示主菜单,并且可以退出
  2. 功能2:完成可以显示明细和登记收入的功能
  3. 功能3:完成了登记支出的功能
3)具体功能实现

功能1:先完成可以显示主菜单,并且可以退出

思路分析:给出的界面完成,主菜单的显示,当用户输入4的时候就退出

go 复制代码
package main
import (
	"fmt"
)

func main(){

	//声明一个变量,保存接收用户输入的选项
	key := ""
	//声明一个变量,控制是否退出for循环
	loop := true

	//显示这个主菜单
	for {
		fmt.Println("--------家庭收支记账软件---------")
		fmt.Println("         1.收支明细")
		fmt.Println("         2.登记收入")
		fmt.Println("         3.登记支出")
		fmt.Println("         4.退出软件")
		fmt.Print("请选择(1-4)")

		fmt.Scanln(&key)

		switch key {
		case "1" :
			fmt.Println("1.收支明细")
		case "2" :
			fmt.Println("2.登记收入")
	    case "3" :
			fmt.Println("3.登记支出")
		case "4" :
			loop = false	
		default :
		    fmt.Println("请输入正确的选项")			
		}

		if !loop {
			break
		}
	}
	fmt.Println("你退出了家庭记账软件的使用")
}

功能2:完成可以显示明细和登记收入的功能

思路分析:

1.因为需要显示明细,我们定义一个变量details string来记录

2.还需要定义变量来记录余额(balance),每次支出的收支的金额(money),以及收支说明(note)

走代码

go 复制代码
    //声明一个变量统计余额
	balance := 10000.0
	//每次收支的金额
	money := 0.0
	//每次收支的说明
    note := ""
	//收支的详情
	//当有收支发生的时候,就对details进行拼接处理
	details := "收支\t账户余额\t收支金额\t说明"

在case的操作
case "2" :
			fmt.Println("本次收入金额:")
			fmt.Scanln(&money)
			balance += money //修改账户余额
			fmt.Println("本次收入的说明:")
			fmt.Scanln(&note)
			//将这个收入情况,拼接到details变量当中
			details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balance,money,note)

功能3完成登记支出的功能

思路分析:登记支出的功能和登记收入的功能类似做一些修改即可

go 复制代码
case "3" :
			fmt.Println("本次支出的金额:")
			fmt.Scanln(&money)
			//这里需要做出一个必要的判断
			if money > balance {
				fmt.Println("余额不足")
				break
			}
			balance -=money
			fmt.Println("本次的支出说明:")
			fmt.Scanln(&note)
			details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note)

项目改进

1.用户输入4时,给出提示"你确定要退出吗?y/n",必须输入正确的y/n,否则循环输入指令,直到输入y或者n

go 复制代码
case "4" :
			fmt.Println("您确定要退出吗? y/n")
			choice :=" "
			for {
				fmt.Scanln(&choice)
				if choice == "y" || choice == "n"{ //输了y/n就break出去
					break
				}
				fmt.Println("您的输入有误请重新输入 y/n")
			}
			if choice == "y" {
				loop = false	
			}
			
			

2.当没有任何收支明细时,提示"当前没有收支明细。。。来一笔把!"

go 复制代码
case "1" :
			fmt.Println("------------当前收支明细记录--------")
			if flag {
				fmt.Println(details)
			}else{
				fmt.Println("您当前没有支出记录,来一笔吧!")
			}

3.在支出时,判断余额是否够,并给出相应的提示

go 复制代码
case "3" :
			fmt.Println("本次支出的金额:")
			fmt.Scanln(&money)
			//这里需要做出一个必要的判断
			if money > balance {
				fmt.Println("余额不足")
				break
			}
			balance -=money
			fmt.Println("本次的支出说明:")
			fmt.Scanln(&note)
			details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note)
			flag = true

面向过程的家庭记账收支软件全部代码

go 复制代码
package main
import (
	"fmt"
)

func main(){

	//声明一个变量,保存接收用户输入的选项
	key := ""
	//声明一个变量,控制是否退出for循环
	loop := true
	//声明一个变量统计余额
	balance := 10000.0
	//每次收支的金额
	money := 0.0
	//每次收支的说明
    note := ""
	//定义一个变量记录是否有收支的行为
	flag := false
	//收支的详情
	//当有收支发生的时候,就对details进行拼接处理
	details := "收支\t账户余额\t收支金额\t说明"
	


	//显示这个主菜单
	for {
		fmt.Println("\n--------家庭收支记账软件---------")
		fmt.Println("         1.收支明细")
		fmt.Println("         2.登记收入")
		fmt.Println("         3.登记支出")
		fmt.Println("         4.退出软件")
		fmt.Print("请选择(1-4)")

		fmt.Scanln(&key)

		switch key {
		case "1" :
			fmt.Println("------------当前收支明细记录--------")
			if flag {
				fmt.Println(details)
			}else{
				fmt.Println("您当前没有支出记录,来一笔吧!")
			}
		case "2" :
			fmt.Println("本次收入金额:")
			fmt.Scanln(&money)
			balance += money //修改账户余额
			fmt.Println("本次收入的说明:")
			fmt.Scanln(&note)
			//将这个收入情况,拼接到details变量当中
			details += fmt.Sprintf("\n收入\t%v\t%v\t%v",balance,money,note)
	        flag = true
		case "3" :
			fmt.Println("本次支出的金额:")
			fmt.Scanln(&money)
			//这里需要做出一个必要的判断
			if money > balance {
				fmt.Println("余额不足")
				break
			}
			balance -=money
			fmt.Println("本次的支出说明:")
			fmt.Scanln(&note)
			details += fmt.Sprintf("\n支出\t%v\t%v\t%v",balance,money,note)
			flag = true
		case "4" :
			fmt.Println("您确定要退出吗? y/n")
			choice :=" "
			for {
				fmt.Scanln(&choice)
				if choice == "y" || choice == "n"{ //输了y/n就break出去
					break
				}
				fmt.Println("您的输入有误请重新输入 y/n")
			}
			if choice == "y" {
				loop = false	
			}
			
		default :
		    fmt.Println("请输入正确的选项")	

		}

		if !loop {
			break
		}
	}
	fmt.Println("你退出了家庭记账软件的使用")
}

4.将面向过程的代码改为面向对象的方法编写myFamilyAccount.go,并使用testMyFamilyAccount.go去完成测试。

思路分析

把记账软件的功能封装到一个结构体中,然后调用该结构体的方法来实现记账,显示明细就可以了,结构体的名字为FamilyAccount

再通过main方法中创建一个结构体FamilyAccount实例,实现记账即可

代码实现,代码不需要重新写,只需要引用上侧代码

go 复制代码
package objectTestAcc
import (
	"fmt"
)

type FamilyAccount struct {
	//声明必须字段
	
	//声明一个字段,保存接收用户输入的选项
	key string
	//声明一个字段,控制是否退出for循环
	loop bool
	//声明一个字段统计余额
	balance float64
	//每次收支的金额
	money float64
	//每次收支的说明
    note string
	//定义一个字段记录是否有收支的行为
	flag bool
	//收支的详情
	//当有收支发生的时候,就对details进行拼接处理
	details string
}
//编写一个构造方法返回一个FamilyAccount实例 
func NewFamilyAccount() *FamilyAccount {
	return &FamilyAccount{
		key : "",
		loop : true,
		balance : 10000.0,
		money : 0.0,
		note : "",
		flag : false,
		details :  "收支\t账户余额\t收支金额\t说明",
	}
}

//将显示明细写成一个方法
func (this *FamilyAccount) ShowDetails(){
    fmt.Println("------------当前收支明细记录--------")
	if this.flag {
		fmt.Println(this.details)
	}else{
		fmt.Println("您当前没有支出记录,来一笔吧!")
	}
}

//将登记收入写成一个方法和*FamilyAccount绑定
func (this *FamilyAccount) Income(){
	fmt.Println("本次收入金额:")
	fmt.Scanln(&this.money)
	this.balance += this.money //修改账户余额
	fmt.Println("本次收入的说明:")
	fmt.Scanln(&this.note)
	//将这个收入情况,拼接到details变量当中
	this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v",this.balance,this.money,this.note)
	this.flag = true
}
//将支出也绑定到一个方法当中
func (this *FamilyAccount) Pay(){
	fmt.Println("本次支出的金额:")
	fmt.Scanln(&this.money)
	//这里需要做出一个必要的判断
	if this.money > this.balance {
		fmt.Println("余额不足")
	}
	this.balance -=this.money
	fmt.Println("本次的支出说明:")
	fmt.Scanln(&this.note)
	this.details += fmt.Sprintf("\n支出\t%v\t%v\t%v",this.balance,this.money,this.note)
	this.flag = true
}

//将退出系统写成一个方法
func (this *FamilyAccount) exit(){
	fmt.Println("您确定要退出吗? y/n")
	choice :=" "
	for {
		fmt.Scanln(&choice)
		if choice == "y" || choice == "n"{ //输了y/n就break出去
			break
		}
		fmt.Println("您的输入有误请重新输入 y/n")
	}
	if 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.Print("请选择(1-4)")
		fmt.Scanln(&this.key)
		switch this.key {
		case "1" :
			this.ShowDetails()
		case "2" :
			this.Income()
		case "3" :
			this.Pay()
		case "4" :
			this.exit()	
		default :
		    fmt.Println("请输入正确的选项")	
		}

		if !this.loop {
			break
		}
	}
}
建立一个main方法
package main
import (
	"fmt"
	"go_code/project/objectTestAcc"
)

func main() {
	fmt.Println("这个是面向对象的方式完成")
	objectTestAcc.NewFamilyAccount().MainMenu()

}

3.客户信息管理系统

1)项目需求说明

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

该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表 多个对象协同工作

2)界面设计

添加客户界面

修改客户界面

删除客户界面

客户列表的界面

3)项目框架图
4)流程

功能说明

当用户运行程序,可以看到主菜单,当输入5时,可以退出该软件

思路分析

编写customerView.go另外可以把customer.go和customerDervice.go协商

代码实现

customerManager/model/customer.go

go 复制代码
package model
// import (
// 	"fmt"
// )
//声明一个customer结构体,表示一个客户信息
type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

//编写一个工厂模式,返回一个Customer的实例

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,
		}
	}

customerManagerservice/customerService.go

go 复制代码
package service
import (
	"go_code/project/customerManager/model"
)

//该CustomerService ,完成对Customer的操作,包括增删改查
type CustomerService struct {
	customers []model.Customer
    //声明一个字段,表示当前切片含有多少客户
	//该字段后面,还可以作为新客户的id+1
	customerNum int
}

customerManager/view/customerView.go

go 复制代码
package main
import (
	"fmt"
)

type customerView struct {
     //定义必要字段
	 key string //接收用户输入
	 loop bool //是否循环显示菜单

}

//显示主菜单
func (this *customerView) mainView() {
	for{
		fmt.Println("--------客户信息管理系统------------")
		fmt.Println("         1.添加客户   ")
		fmt.Println("         2.修改客户   ")
		fmt.Println("         3.删除客户   ")
		fmt.Println("         4.客户列表   ")
		fmt.Println("         5.退出   ")
		fmt.Println("请选择(1-5): ")

		fmt.Scanln(&this.key)
		switch this.key {
		case "1":
			fmt.Println("添加客户")
		case "2":
		    fmt.Println("修改客户")
		case "3":
			fmt.Println("删除客户")
		case "4":
			fmt.Println("客户列表")
		case "5":
			this.loop = false
		default :
		    fmt.Println("你的输入有误,请重新输入...")						
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("你退出了客户关系管理系统的使用")
}
func main() {
	//在主函数中,创建一个customerView并运行显示主菜单...
	customerView := customerView{
		key : "",
		loop : true,	
	}
	//显示主菜单
	customerView.mainView()
}
5)完成显示客户列表的功能

思路分析

代码实现

customerManager/model/customer.go

go 复制代码
package model
import (
	"fmt"
)
//声明一个customer结构体,表示一个客户信息
type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

//编写一个工厂模式,返回一个Customer的实例

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,
	}
}
//增加了这个方法
//返回用户的信息,格式化的字符串
func (this Customer) GetInfo() string{
	info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t",this.Id,this.Name,
	this.Gender,this.Age,this.Phone,this.Email)
	return info
} 

customerManagerservice/customerService.go

go 复制代码
package service
import (
	"go_code/project/customerManager/model"
)

//该CustomerService ,完成对Customer的操作,包括增删改查
type CustomerService struct {
	customers []model.Customer
    //声明一个字段,表示当前切片含有多少客户
	//该字段后面,还可以作为新客户的id+1
	customerNum int
}

//编写一个方法,可以返回一个*customerService实例
func NewCustomerService() *CustomerService {
	//为了可以看到客户在切片中,我们初始化一个客户
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(1,"张三","男",20,"112","zs@sohu.com")
	customerService.customers = append(customerService.customers ,customer)
	return customerService
}

//返回客户切片
func (this *CustomerService) List()[]model.Customer{
    return this.customers
}

customerManager/view/customerView.go

go 复制代码
package main
import (
	"fmt"
	"go_code/project/customerManager/service"
)

type customerView struct {
     //定义必要字段
	 key string //接收用户输入
	 loop bool //是否循环显示菜单
	 //增加一个字段customerService
     customerService   *service.CustomerService
}

//显示所有的客户信息
func (this *customerView) list(){
     //首先获取到当前所有的客户信息(在切片中)
	 customers := this.customerService.List()
	 //显示
	 fmt.Println("----------客户列表--------------")
	 fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
	 for i :=0;i<len(customers);i++ {
		fmt.Println(customers[i].GetInfo())
	 }
	 fmt.Printf("\n--------客户列表完成------------\n\n")
}

//显示主菜单
func (this *customerView) mainView() {
	for{
		fmt.Println("--------客户信息管理系统------------")
		fmt.Println("         1.添加客户   ")
		fmt.Println("         2.修改客户   ")
		fmt.Println("         3.删除客户   ")
		fmt.Println("         4.客户列表   ")
		fmt.Println("         5.退出   ")
		fmt.Println("请选择(1-5): ")


		fmt.Scanln(&this.key)
		switch this.key {
		case "1":
			fmt.Println("添加客户")
		case "2":
		    fmt.Println("修改客户")
		case "3":
			fmt.Println("删除客户")
		case "4":
			this.list()
		case "5":
			this.loop = false
		default :
		    fmt.Println("你的输入有误,请重新输入...")						
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("你退出了客户关系管理系统的使用")
}
func main() {
	//在主函数中,创建一个customerView并运行显示主菜单...
	customerView := customerView{
		key : "",
		loop : true,	
	}
	//完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.mainView()
}
6)添加客户功能

功能说明

思路分析

代码实现

需要编写CustomerView和customerService,Customer类

规定,新添加的学院的id就是他是第几个加入的

customerManager/model/customer.go

go 复制代码
//编写一个工厂模式,返回二种Customer的实例方法,不带id
func NewCustomer2(name string, gender string,
	age int,phone string,email string) Customer {
	return Customer{
			Name : name,
			Gender : gender,
			Age : age,
			Phone : phone,
			Email : email,
	}
}

customerManagerservice/customerService.go

go 复制代码
增加一个方法
//添加客户到customer切片中
func (this *CustomerService) Add(customer model.Customer) bool{

	//我们确定一个分配id的规则,就是添加的顺序
	this.customerNum ++
	customer.Id = this.customerNum
	this.customers = append(this.customers,customer)
    return true
}

customerManager/view/customerView.go

go 复制代码
编写一个add方法调用servic蹭的Add()
//得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add() {
	fmt.Println("------------添加客户------------")
	fmt.Println("姓名:")
	name := ""
	fmt.Scanln(&name)
	fmt.Println("性别:")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Println("年龄:")
	age := 0
	fmt.Scanln(&age)
	fmt.Println("电话号码:")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Println("电邮:")
	email := ""
	fmt.Scanln(&email)

	//构建一个新的Customer实例
	//注意:id号没有让用户输入,id号是唯一的,让系统分配即可
	customer := model.NewCustomer2(name,gender,age,phone,email)
	//调用
	if this.customerService.Add(customer) {
		fmt.Println("------------添加完成------------")
	}else{
		fmt.Println("------------添加失败------------")
	}
}
下面的switch方法也要改一下
case "1":
			this.add()
7)删除客户功能

功能说明

思路分析

需要编写CustomerView和CustomerService

代码实现

customerManager/model/customer.go:无变化

customerManagerservice/customerService.go

go 复制代码
增加了这两个方法,一个删除一个查找id
//根据id删除客户(从切片中删除)
func (this *CustomerService) Delete(id int )bool {
	index :=this.FindById(id)
	//如果index ==-1说明没有这个客户
	if index== -1 {
		return false
	}
		//如何从切片中删除一个元素
	this.customers = append(this.customers[:index],this.customers[index+1:]...)
	return true
	
}

//根据Id查找客户在在切片对应中的下标,返回-1
func (this *CustomerService) FindById(id int) int {
	//默认为-1
	index := -1
	//遍历this.customers切片
	for i :=0;i < len(this.customers);i++ {
		if this.customers[i].Id ==id {
			//找到了
			index = i
		}
	}
	return index
}

customerManager/view/customerView.go

go 复制代码
增加这个方法
//得到用户输入的id删除该id对应的客户
func (this *customerView) delete() {
	fmt.Println("------------删除客户------------")
	fmt.Println("请选择待删除的客户编号(-1退出):")
	id :=-1
	fmt.Scanln(&id)
	if id == -1 {
		return //放弃删除操作
	}

	fmt.Println("确认是否删除(Y/N): ")
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
			//调用service中的delete方法
		if this.customerService.Delete(id) {
			fmt.Println("------------删除成功------------")
	}else{
		fmt.Println("------------删除失败,输入的id号不存在------------")
	}
  }
}

8)完善退出确认功能

功能说明:

要求用户在退出时提示"是否退出(Y/N),用户必须输入y/n否则循环提示

思路分析:需编写CustomerView

代码实现

在customerManager/view/customerView.go增加这个方法

go 复制代码
//退出软件
func (this *customerView) exit(){
	fmt.Println("确定是否退出(Y/N): ")
	for {
	fmt.Scanln(&this.key)
	if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n"{
		break
	  }
	  fmt.Println("您的输入有误,请重新输入(Y/N) : ")
	}
	if this.key == "Y" || this.key == "y" {
		this.loop = false
	}
}
然后在switch中修改一下
case "5":
			this.exit()
8)修改客户的功能

功能说明:根据id进行对客户的修改操作

思路:依旧在customerService和customerView中进行编写操作

代码实现

customerManagerservice/customerService.go

go 复制代码
//根据id进行修改客户信息的操作
func (this *CustomerService) Update(customer model.Customer) bool {
	index :=this.FindById(customer.Id)
	//如果index ==-1说明没有这个客户
	if index== -1 {
		return false
	}
	//将customer插入到指定的位置并对customers进行更新操作,就将原来位置的customer用一个新的customer进行替换操作
	this.customers = append(append(this.customers[:index],customer),this.customers[index+1:]...)
    return true
}

//根据Id查找客户在在切片对应中的下标,返回-1
func (this *CustomerService) FindById(id int) int {
	//默认为-1
	index := -1
	//遍历this.customers切片
	for i :=0;i < len(this.customers);i++ {
		if this.customers[i].Id ==id {
			//找到了
			index = i
		}
	}
	return index
}

customerManager/view/customerView.go

go 复制代码
//修改客户的操作
func (this *customerView) update() {
	fmt.Println("------------修改客户------------")
    fmt.Println("请选择修改客户的编号(-1的话就退出): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}

	fmt.Println("姓名:")
	name := ""
	fmt.Scanln(&name)
	fmt.Println("性别:")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Println("年龄:")
	age := 0
	fmt.Scanln(&age)
	fmt.Println("电话号码:")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Println("电邮:")
	email := ""
	fmt.Scanln(&email)

	//构建一个新的Customer实例
	//注意:id号没有让用户输入,id号是唯一的,让系统分配即可
	customer := model.NewCustomer(id,name,gender,age,phone,email)
	//调用
	if this.customerService.Update(customer) {
		fmt.Println("------------修改成功------------")
	}else{
		fmt.Println("------------修改失败------------")
	}
}

再外加一个简单的登录操作使得项目更加完善

在customerManager/view/customerView.go中进行编写

go 复制代码
//简单登录功能的时间
func (this *customerView) Login (){
	account :=""
	pwd :=""
	for {
    fmt.Println("请输入账号: ")
	fmt.Scanln(&account)
	fmt.Println("请输入密码")
	fmt.Scanln(&pwd)
	if account == "7758258" && pwd =="111"{
		fmt.Println("恭喜你!正在进入系统!")
		break
	   }
	   fmt.Println("您的输入的账号或者密码有误,请重新输入: ")	   	
	}
	this.mainView()
}
func main() {
	//在主函数中,创建一个customerView并运行显示主菜单...
	customerView := customerView{
		key : "",
		loop : true,	
	}
	//完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.Login()
}
9)完整代码的展示如下

customerManager/model/customer.go

go 复制代码
package model
import (
	"fmt"
)
//声明一个customer结构体,表示一个客户信息
type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

//编写一个工厂模式,返回一个Customer的实例
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,
	}
}

//编写一个工厂模式,返回二种Customer的实例方法,不带id
func NewCustomer2(name string, gender string,
	age int,phone string,email string) Customer {
	return Customer{
			Name : name,
			Gender : gender,
			Age : age,
			Phone : phone,
			Email : email,
	}
}

//返回用户的信息,格式化的字符串
func (this Customer) GetInfo() string{
	info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t",this.Id,this.Name,
	this.Gender,this.Age,this.Phone,this.Email)
	return info
} 

customerManagerservice/customerService.go

go 复制代码
package service
import (
	"go_code/project/customerManager/model"
)

//该CustomerService ,完成对Customer的操作,包括增删改查
type CustomerService struct {
	customers []model.Customer
    //声明一个字段,表示当前切片含有多少客户
	//该字段后面,还可以作为新客户的id+1
	customerNum int
}

//编写一个方法,可以返回一个*customerService实例
func NewCustomerService() *CustomerService {
	//为了可以看到客户在切片中,我们初始化一个客户
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(1,"张三","男",20,"112","zs@sohu.com")
	customerService.customers = append(customerService.customers ,customer)
	return customerService
}

//返回客户切片
//一定要使用指针的方式
func (this *CustomerService) List()[]model.Customer{
    return this.customers
}

//添加客户到customer切片中
//必须要用指针的方式,保证一直用的都是一个CustomerService
func (this *CustomerService) Add(customer model.Customer) bool{

	//我们确定一个分配id的规则,就是添加的顺序
	this.customerNum ++
	customer.Id = this.customerNum
	this.customers = append(this.customers,customer)
    return true
}

//根据id删除客户(从切片中删除)
func (this *CustomerService) Delete(id int )bool {
	index :=this.FindById(id)
	//如果index ==-1说明没有这个客户
	if index== -1 {
		return false
	}
		//如何从切片中删除一个元素
	this.customers = append(this.customers[:index],this.customers[index+1:]...)
	return true
	
}

//根据id进行修改客户信息的操作
func (this *CustomerService) Update(customer model.Customer) bool {
	index :=this.FindById(customer.Id)
	//如果index ==-1说明没有这个客户
	if index== -1 {
		return false
	}
	//将customer插入到指定的位置并对customers进行更新操作,就将原来位置的customer用一个新的customer进行替换操作
	this.customers = append(append(this.customers[:index],customer),this.customers[index+1:]...)
    return true
}

//根据Id查找客户在在切片对应中的下标,返回-1
func (this *CustomerService) FindById(id int) int {
	//默认为-1
	index := -1
	//遍历this.customers切片
	for i :=0;i < len(this.customers);i++ {
		if this.customers[i].Id ==id {
			//找到了
			index = i
		}
	}
	return index
}

customerManager/view/customerView.go

go 复制代码
package main
import (
	"fmt"
	"go_code/project/customerManager/service"
	"go_code/project/customerManager/model"
)

type customerView struct {
     //定义必要字段
	 key string //接收用户输入
	 loop bool //是否循环显示菜单
	 //增加一个字段customerService
     customerService   *service.CustomerService
}

//显示所有的客户信息
func (this *customerView) list(){
     //首先获取到当前所有的客户信息(在切片中)
	 customers := this.customerService.List()
	 //显示
	 fmt.Println("----------客户列表--------------")
	 fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
	 for i :=0;i<len(customers);i++ {
		fmt.Println(customers[i].GetInfo())
	 }
	 fmt.Printf("\n--------客户列表完成------------\n\n")
}

//得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add() {
	fmt.Println("------------添加客户------------")
	fmt.Println("姓名:")
	name := ""
	fmt.Scanln(&name)
	fmt.Println("性别:")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Println("年龄:")
	age := 0
	fmt.Scanln(&age)
	fmt.Println("电话号码:")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Println("电邮:")
	email := ""
	fmt.Scanln(&email)

	//构建一个新的Customer实例
	//注意:id号没有让用户输入,id号是唯一的,让系统分配即可
	customer := model.NewCustomer2(name,gender,age,phone,email)
	//调用
	if this.customerService.Add(customer) {
		fmt.Println("------------添加完成------------")
	}else{
		fmt.Println("------------添加失败------------")
	}
}

//修改客户的操作
func (this *customerView) update() {
	fmt.Println("------------修改客户------------")
    fmt.Println("请选择修改客户的编号(-1的话就退出): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}

	fmt.Println("姓名:")
	name := ""
	fmt.Scanln(&name)
	fmt.Println("性别:")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Println("年龄:")
	age := 0
	fmt.Scanln(&age)
	fmt.Println("电话号码:")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Println("电邮:")
	email := ""
	fmt.Scanln(&email)

	//构建一个新的Customer实例
	//注意:id号没有让用户输入,id号是唯一的,让系统分配即可
	customer := model.NewCustomer(id,name,gender,age,phone,email)
	//调用
	if this.customerService.Update(customer) {
		fmt.Println("------------修改成功------------")
	}else{
		fmt.Println("------------修改失败------------")
	}
}

//得到用户输入的id删除该id对应的客户
func (this *customerView) delete() {
	fmt.Println("------------删除客户------------")
	fmt.Println("请选择待删除的客户编号(-1退出):")
	id :=-1
	fmt.Scanln(&id)
	if id == -1 {
		return //放弃删除操作
	}

	fmt.Println("确认是否删除(Y/N): ")
	choice := ""
	for {
	fmt.Scanln(&choice)
		if choice == "y" || choice == "Y" || choice =="n" || choice =="N"{
			break
		}
		fmt.Println("您的输入有误请重新输入(Y/N): ")
	}
	if choice == "y" || choice == "Y" {
			//调用service中的delete方法
		if this.customerService.Delete(id) {
			fmt.Println("------------删除成功------------")
	}else{
		fmt.Println("------------删除失败,输入的id号不存在------------")
	}
  } else{
	this.mainView()
  }
}

//退出软件
func (this *customerView) exit(){
	fmt.Println("确定是否退出(Y/N): ")
	for {
	fmt.Scanln(&this.key)
	if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n"{
		break
	  }
	  fmt.Println("您的输入有误,请重新输入(Y/N) : ")
	}
	if this.key == "Y" || this.key == "y" {
		this.loop = false
	}
}

//显示主菜单
func (this *customerView) mainView() {
	for{
		fmt.Println("--------客户信息管理系统------------")
		fmt.Println("         1.添加客户   ")
		fmt.Println("         2.修改客户   ")
		fmt.Println("         3.删除客户   ")
		fmt.Println("         4.客户列表   ")
		fmt.Println("         5.退出   ")
		fmt.Println("请选择(1-5): ")


		fmt.Scanln(&this.key)
		switch this.key {
		case "1":
			this.add()
		case "2":
		    this.update()
		case "3":
			this.delete()
		case "4":
			this.list()
		case "5":
			this.exit()
		default :
		    fmt.Println("你的输入有误,请重新输入...")						
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("你退出了客户关系管理系统的使用")
}

//简单登录功能的时间
func (this *customerView) Login (){
	account :=""
	pwd :=""
	for {
    fmt.Println("请输入账号: ")
	fmt.Scanln(&account)
	fmt.Println("请输入密码")
	fmt.Scanln(&pwd)
	if account == "7758258" && pwd =="111"{
		fmt.Println("恭喜你!正在进入系统!")
		break
	   }
	   fmt.Println("您的输入的账号或者密码有误,请重新输入: ")	   	
	}
	this.mainView()
}
func main() {
	//在主函数中,创建一个customerView并运行显示主菜单...
	customerView := customerView{
		key : "",
		loop : true,	
	}
	//完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.Login()
}

10)项目展示

1.登录

2.客户列表

3.添加客户

4.修改客户

5.删除客户

6.退出

相关推荐
Charles Ray13 分钟前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
我要吐泡泡了哦1 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
骑鱼过海的猫1231 小时前
【tomcat】tomcat学习笔记
笔记·学习·tomcat
贾saisai3 小时前
Xilinx系FPGA学习笔记(九)DDR3学习
笔记·学习·fpga开发
北岛寒沫3 小时前
JavaScript(JS)学习笔记 1(简单介绍 注释和输入输出语句 变量 数据类型 运算符 流程控制 数组)
javascript·笔记·学习
铁匠匠匠5 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
架构文摘JGWZ6 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
小齿轮lsl6 小时前
PFC理论基础与Matlab仿真模型学习笔记(1)--PFC电路概述
笔记·学习·matlab
Aic山鱼7 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
qq11561487077 小时前
Java学习第八天
学习