客户信息管理系统
- 涉及知识点:切片的插入、删除和替换;多对象协同工作
项目需求说明
- 模拟实现基于文本界面的《客户信息管理软件》
- 该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表
- 主菜单页面
- 添加客户页面
- 修改客户界面
- 删除客户界面
- 客户列表界面
程序框架图
- 描述项目里面有哪些文件、文件与文件的的调用关系
客户信息管理系统程序框架图
- 1、完成:显示主菜单和退出软件
- 功能说明:当用户运行程序时,可以看到主菜单,当输入5时,可以退出该软件。
- 思路分析:编写customerView.go,另外可以把customerService.go和customer.go写上
- 代码实现
- customerManage/model/customer.go
package model
//声明一个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,
}
}
-
-
- customerManage/service/customerService.go
-
package service
import (
"go_code/customerManage/model"
)
//该CustomerService,完成对Customer的操作,包括
//增删改查
type CustomerService struct{
customers []model.Customer
//声明一个字段,表示当前切片已经含有多少个客户
//该字段后面,还可以作为新客户的id+1
customerNum int
}
-
-
- customerManage/view/customerView.go
-
package main
import(
"fmt"
)
type customerView struct{
//定义必要的字段
key string //接收用户的输入
loop bool //表示是否循环显示主菜单
}
//显示主菜单
func (this *customerView)mainMenu(){
for{
fmt.Println("-------------客户信息管理软件------------")
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":
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.mainMenu()
}
-
2、完成:显示客户列表的功能
-
功能说明:当用户运行程序时,可以看到主菜单,当输入5时,可以退出该软件。
-
思路分析:编写customerView.go,另外可以把customerService.go和customer.go写上
-
代码实现
-
model包中的customer.go增加客户列表输出格式化字串方法
-
-
- service包中的customerService.go中增加一个工厂模式,便于View层调用Service层的方法。新增一个查看客户信息的List()方法
-
- view包中的customerView.go中,customerView结构体增加一个customerService字段,用于调用customerService;新增一个绑定customerView的list()方法调用List()方法显示所有客户信息。
- view包中的customerView.go中,customerView结构体增加一个customerService字段,用于调用customerService;新增一个绑定customerView的list()方法调用List()方法显示所有客户信息。
-
3、完成:添加客户信息的功能
- 思路:
-
4、完成:删除客户信息的功能
- 思路:
- 思路:
-
5、完成:修改客户信息的功能
- 思路:需要改动customerService 和 customerView
-
6、完成:完善退出确认功能
- 思路:只需要改动customerView
-
- 思路:只需要改动customerView
完整源码
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
}
package service
import (
"go_code/customerManage/model"
"fmt"
)
//该CustomerService,完成对Customer的操作,包括
//增删改查
type CustomerService struct{
customer []model.Customer
//声明一个字段,表示当前切片已经含有多少个客户
//该字段后面,还可以作为新客户的id+1
customerNum int
}
//编写一个方法,可以放回 *CustomerService
func NewCustomerService() *CustomerService{
//为了能够看到有客户在切片中,我们初始化一个客户
customerService := &CustomerService{}
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男", 20, "112", "zs@163.com")
customerService.customer = append(customerService.customer, customer)
return customerService
}
func (this *CustomerService)List()[]model.Customer{
return this.customer
}
func (this *CustomerService)Add(customer model.Customer)bool{
//确定一个分配id的规则,即就是添加的顺序
this.customerNum++
customer.Id = this.customerNum
this.customer = append(this.customer, customer)
return true
}
//根据id删除客户(从切片中)
func (this *CustomerService)Delete(id int)bool{
index := this.FindById(id)
//如果index为-1 说明没有这个客户
if index == -1{
return false
}
//如何从切片中删除元素
this.customer = append(this.customer[:index], this.customer[index+1:]...)
return true
}
//根据id修改客户,先查找是否存在该用户
func (this *CustomerService)Update(id int)bool{
index := this.FindById(id)
//如果index为-1 说明没有这个客户
if index == -1{
return false
}
//存在,修改客户信息
fmt.Println("姓名:", this.customer[index].Name)
fmt.Scanln(&(this.customer[index].Name))
fmt.Println("性别:", this.customer[index].Gender)
fmt.Scanln(&(this.customer[index].Gender))
fmt.Println("年龄:", this.customer[index].Age)
fmt.Scanln(&(this.customer[index].Age))
fmt.Println("电话:", this.customer[index].Phone)
fmt.Scanln(&(this.customer[index].Phone))
fmt.Println("邮箱:",this.customer[index].Email)
fmt.Scanln(&(this.customer[index].Email))
return true
}
//根据id查找客户在切片中对应下标,如果没有该客户,返回-1
func (this *CustomerService)FindById(id int)int{
index := -1
for i := 0; i < len(this.customer); i++{
if this.customer[i].Id == id{
index = i
}
}
return index
}
package main
import(
"fmt"
"go_code/customerManage/service"
"go_code/customerManage/model"
)
type customerView struct{
//定义必要的字段
key string //接收用户的输入
loop bool //表示是否循环显示主菜单
//增加customerService字段,因为需要调用customerService
customerService *service.CustomerService
}
//显示所有客户信息
func (this *customerView)list(){
//首先,获取到当前所有的客户信息(在切片中)
customer := this.customerService.List()
//显示
fmt.Println("-------------客户列表------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
for i:=0; i < len(customer); i++{
fmt.Println(customer[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("-------------添加失败------------")
}
}
//得到用户的输入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"{
//调用customerService的Delete方法
if this.customerService.Delete(id){
fmt.Println("-------------删除成功------------")
}else{
fmt.Println("-------删除失败,输入的id号不存在-------")
}
}
}
//得到用户的输入id,查看是否存在该用户,存在则修改信息,不存在进行提示
func (this *customerView)update(){
fmt.Println("-------------修改客户------------")
fmt.Println("请选择带删除客户编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1{
return //放弃修改
}
//调用customerService的Update方法
if this.customerService.Update(id){
fmt.Println("-------------修改成功------------")
}else{
fmt.Println("-------修改失败,输入的id号不存在-------")
}
}
//退出软件
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)mainMenu(){
for{
fmt.Println("-------------客户信息管理软件------------")
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.add()
case "2":
//fmt.Println("修改客户")
this.update()
case "3":
this.delete()
case "4":
this.list()
case "5":
this.exit()
default:
fmt.Println("你的输入有误,请重新输入")
}
if !this.loop{
break
}
}
fmt.Println("你退出了客户关系管理系统")
}
func main(){
//在主函数中,创建一个customerView实例,并运行显示主菜单
customerView := customerView{
key : "",
loop : true,
}
//这里完成对customerView结构体的customerService字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单
customerView.mainMenu()
}