客户信息管理系统
项目需求说明
- 模拟实现及语文本界面的《客户信息管理软件》。
该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表。
项目的界面设计
-
程序框架图
程序框架图:分析该模块有多少文件(类),和各个类之间的调用关系,程序员需要按照架构师的要求,进行分层设计。
- CustomerView.go
- 可以显示界面
- 可以接受用户的输入
- 根据用户的输入完成客户的管理,他是调用CustomerService方法。
- CustomerService(处理业务逻辑)
- 完成对用户的各种操作
- 对客户的增加、删除、修改,显示
- 会声明一个customer的切片
- Customer(表示数据)
- 表示客户信息
- 客户各种字段
- 表示客户信息
- CustomerView.go
-
项目功能实现
项目功能实现-显示主菜单和完成退出软件功能
功能说明
- 当用户运行程序时,可以看到主菜单,当输入5时,可以退出该程序。
- 思路分析
- 编写 customerView.go,另外可以把 customer.go 和 customerService.go 写上
- 代码实现 ```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, } }
```go
//-customerManage/service/customerSerive.go
package service
import "GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
//该 CustomerService,完成对 Customer 的操作,包括增删改查
type CustomerService struct {
customers []model.Customer
//声明一个字段,表示当前切片含有多少个客户
//该字段后面,还可以作为新客户的 Id(customerNum + 1)
customerNum int
}
//-customerManage/view/customerView
package main
import "fmt"
type customerView struct {
//定义必要的字段
key string //接受用户的输入
loop bool //表示是否循环的显示菜单
}
//显示主菜单
func (cv *customerView) mainMenu() {
for {
fmt.Println("----------------客户信息管理软件----------------")
fmt.Println(" 1 添 加 客 户")
fmt.Println(" 2 修 改 客 户")
fmt.Println(" 3 删 除 客 户")
fmt.Println(" 4 客 户 列 表")
fmt.Println(" 5 退 出")
fmt.Println()
fmt.Print(" 请选择(1-5):")
fmt.Scanln(&cv.key)
switch cv.key {
case "1":
fmt.Println("添 加 客 户")
case "2":
fmt.Println("修 改 客 户")
case "3":
fmt.Println("删 除 客 户")
case "4":
fmt.Println("客 户 列 表")
case "5":
cv.loop = false
default:
fmt.Println("你的输入有误,请重新输入...")
}
if !cv.loop {
break
}
}
fmt.Println("你退出了客户关系管理系统")
}
func main() {
fmt.Println("ok")
//在 main 主函数,创建一个 customerView,并运行显示主菜单...
customerView := customerView{
key : "",
loop : true,
}
//显示主菜单..
customerView.mainMenu()
}
项目功能实现-完成显示客户列表的功能
- 代码实现 ```go //-customerManage/model/customer.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 (customer *Customer) GetInfo() string { info := fmt.Sprintf(“%v\t%v\t%v\t%v\t%v\t%v”, customer.Id, customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email) return info }
```go
//-customerManage/service/customerSerive.go
package service
import "GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
//该 CustomerService,完成对 Customer 的操作,包括增删改查
type CustomerService struct {
customers []model.Customer
//声明一个字段,表示当前切片含有多少个客户
//该字段后面,还可以作为新客户的 Id(customerNum + 1)
customerNum int
}
//编写一个方法, 可以返回 *CustomerService
func NewCustomerService() *CustomerService {
//为了能够看到有客户在切片中,我们初始化一个客户
customerService := &CustomerService{}
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男", 20, "112", "zs@souhu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (cs *CustomerService) List() []model.Customer {
return cs.customers
}
//-customerManage/view/customerView
package main
import (
"GoProject/src/go_code/chapter13/project_case_01/customerManage/service"
"fmt"
)
type customerView struct {
//定义必要的字段
key string //接受用户的输入
loop bool //表示是否循环的显示菜单
//增加一个字段 customerService
customerService *service.CustomerService
}
//显示所有的客户信息
func (cv *customerView) list() {
//首先,获取到当前所有的用户信息
customers := cv.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 (cv *customerView) mainMenu() {
for {
fmt.Println("----------------客户信息管理软件----------------")
fmt.Println(" 1 添 加 客 户")
fmt.Println(" 2 修 改 客 户")
fmt.Println(" 3 删 除 客 户")
fmt.Println(" 4 客 户 列 表")
fmt.Println(" 5 退 出")
fmt.Println()
fmt.Print(" 请选择(1-5):")
fmt.Scanln(&cv.key)
switch cv.key {
case "1":
fmt.Println("添 加 客 户")
case "2":
fmt.Println("修 改 客 户")
case "3":
fmt.Println("删 除 客 户")
case "4":
cv.list()
case "5":
cv.loop = false
default:
fmt.Println("你的输入有误,请重新输入...")
}
if !cv.loop {
break
}
}
fmt.Println("你退出了客户关系管理系统")
}
func main() {
//fmt.Println("ok")
//在 main 主函数,创建一个 customerView,并运行显示主菜单...
customerView := customerView{
key : "",
loop : true,
}
//这里完成对 customerView 结构体的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单..
customerView.mainMenu()
}
项目功能实现-添加客户功能
- 代码实现 ```go //-customerManage/model/customer.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 (customer *Customer) GetInfo() string { info := fmt.Sprintf(“%v\t%v\t%v\t%v\t%v\t%v”, customer.Id, customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email) return info }
```go
//-customerManage/service/customerSerive.go
package service
import "GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
//该 CustomerService,完成对 Customer 的操作,包括增删改查
type CustomerService struct {
customers []model.Customer
//声明一个字段,表示当前切片含有多少个客户
//该字段后面,还可以作为新客户的 Id(customerNum + 1)
customerNum int
}
//编写一个方法, 可以返回 *CustomerService
func NewCustomerService() *CustomerService {
//为了能够看到有客户在切片中,我们初始化一个客户
customerService := &CustomerService{}
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男", 20, "112", "zs@souhu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (cs *CustomerService) List() []model.Customer {
return cs.customers
}
//添加客户到 customer 切片
//!!!
func (cs *CustomerService) Add(customer model.Customer) bool {
//我们确定一个分配 id 规则,就是添加顺序
cs.customerNum ++
customer.Id = cs.customerNum
cs.customers = append(cs.customers, customer)
return true
}
//-customerManage/view/customerView
package main
import (
"GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
"GoProject/src/go_code/chapter13/project_case_01/customerManage/service"
"fmt"
)
type customerView struct {
//定义必要的字段
key string //接受用户的输入
loop bool //表示是否循环的显示菜单
//增加一个字段 customerService
customerService *service.CustomerService
}
//显示所有的客户信息
func (cv *customerView) list() {
//首先,获取到当前所有的用户信息
customers := cv.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 (cv *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 是唯一的,需要系统分配
//id := cv.customerService.customerNum
customer := model.NewCustomer2(name, gender, age, phone, email)
//调用
if cv.customerService.Add(customer) {
fmt.Printf("-------------------添加成功-------------------\n\n")
} else {
fmt.Printf("-------------------添加失败-------------------\n\n")
}
}
//显示主菜单
func (cv *customerView) mainMenu() {
for {
fmt.Println("----------------客户信息管理软件----------------")
fmt.Println(" 1 添 加 客 户")
fmt.Println(" 2 修 改 客 户")
fmt.Println(" 3 删 除 客 户")
fmt.Println(" 4 客 户 列 表")
fmt.Println(" 5 退 出")
fmt.Println()
fmt.Print(" 请选择(1-5):")
fmt.Scanln(&cv.key)
switch cv.key {
case "1":
cv.add()
case "2":
fmt.Println("修 改 客 户")
case "3":
fmt.Println("删 除 客 户")
case "4":
cv.list()
case "5":
cv.loop = false
default:
fmt.Println("你的输入有误,请重新输入...")
}
if !cv.loop {
break
}
}
fmt.Println("你退出了客户关系管理系统")
}
func main() {
//fmt.Println("ok")
//在 main 主函数,创建一个 customerView,并运行显示主菜单...
customerView := customerView{
key : "",
loop : true,
}
//这里完成对 customerView 结构体的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单..
customerView.mainMenu()
}
项目功能实现-完成删除客户的功能
- 代码实现 ```go //-customerManage/model/customer.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 (customer *Customer) GetInfo() string { info := fmt.Sprintf(“%v\t%v\t%v\t%v\t%v\t%v”, customer.Id, customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email) return info }
```go
//-customerManage/service/customerSerive.go
package service
import "GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
//该 CustomerService,完成对 Customer 的操作,包括增删改查
type CustomerService struct {
customers []model.Customer
//声明一个字段,表示当前切片含有多少个客户
//该字段后面,还可以作为新客户的 Id(customerNum + 1)
customerNum int
}
//编写一个方法, 可以返回 *CustomerService
func NewCustomerService() *CustomerService {
//为了能够看到有客户在切片中,我们初始化一个客户
customerService := &CustomerService{}
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男", 20, "112", "zs@souhu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (cs *CustomerService) List() []model.Customer {
return cs.customers
}
//添加客户到 customer 切片
//!!!
func (cs *CustomerService) Add(customer model.Customer) bool {
//我们确定一个分配 id 规则,就是添加顺序
cs.customerNum ++
customer.Id = cs.customerNum
cs.customers = append(cs.customers, customer)
return true
}
//根据 id 查找客户再切片中国对应的下标,如果没有该客户,返回 -1。
func (cs *CustomerService) FindById(id int) int {
index := -1
//遍历
for i := 0; i < len(cs.customers); i++ {
if cs.customers[i].Id == id {
//找到
index = i
}
}
return index
}
//根据 id 删除客户(从切片中删除)
func (cs *CustomerService) Delete(id int) bool {
index := cs.FindById(id)
//如果 index == -1, 说明没有这个客户
if index == -1 {
return false
}
//如何从切片中删除一个元素
cs.customers = append(cs.customers[:index], cs.customers[index + 1:]...)
return true
}
//-customerManage/view/customerView
package main
import (
"GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
"GoProject/src/go_code/chapter13/project_case_01/customerManage/service"
"fmt"
)
type customerView struct {
//定义必要的字段
key string //接受用户的输入
loop bool //表示是否循环的显示菜单
//增加一个字段 customerService
customerService *service.CustomerService
}
//显示所有的客户信息
func (cv *customerView) list() {
//首先,获取到当前所有的用户信息
customers := cv.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 (cv *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 是唯一的,需要系统分配
//id := cv.customerService.customerNum
customer := model.NewCustomer2(name, gender, age, phone, email)
//调用
if cv.customerService.Add(customer) {
fmt.Printf("-------------------添加成功-------------------\n\n")
} else {
fmt.Printf("-------------------添加失败-------------------\n\n")
}
}
//得到用户的输入 id,删除该 id 对应的客户
func (cv *customerView) delete() {
fmt.Println("-------------------删除客户-------------------")
fmt.Print("请选择待删除客户编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return //放弃删除
}
fmt.Print("确认是否删除(Y/N):")
choice := ""
fmt.Scanln(&choice)
if choice == "y" || choice == "Y" {
//调用 customerService 的 Delete 方法
if cv.customerService.Delete(id) {
fmt.Printf("-------------------删除完成-------------------\n\n")
} else {
fmt.Printf("-----------删除失败,输入 id 号不存在------------\n\n")
}
}
}
//显示主菜单
func (cv *customerView) mainMenu() {
for {
fmt.Println("----------------客户信息管理软件----------------")
fmt.Println(" 1 添 加 客 户")
fmt.Println(" 2 修 改 客 户")
fmt.Println(" 3 删 除 客 户")
fmt.Println(" 4 客 户 列 表")
fmt.Println(" 5 退 出")
fmt.Println()
fmt.Print(" 请选择(1-5):")
fmt.Scanln(&cv.key)
switch cv.key {
case "1":
cv.add()
case "2":
fmt.Println("修 改 客 户")
case "3":
cv.delete()
case "4":
cv.list()
case "5":
cv.loop = false
default:
fmt.Println("你的输入有误,请重新输入...")
}
if !cv.loop {
break
}
}
fmt.Println("你退出了客户关系管理系统")
}
func main() {
//fmt.Println("ok")
//在 main 主函数,创建一个 customerView,并运行显示主菜单...
customerView := customerView{
key : "",
loop : true,
}
//这里完成对 customerView 结构体的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单..
customerView.mainMenu()
}
项目功能实现-完善退出确认功能
- 代码实现 ```go //-customerManage/view/customerView package main
import ( “GoProject/src/go_code/chapter13/project_case_01/customerManage/model” “GoProject/src/go_code/chapter13/project_case_01/customerManage/service” “fmt” )
type customerView struct { //定义必要的字段 key string //接受用户的输入 loop bool //表示是否循环的显示菜单 //增加一个字段 customerService customerService *service.CustomerService
}
//显示所有的客户信息 func (cv *customerView) list() { //首先,获取到当前所有的用户信息 customers := cv.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 (cv *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 是唯一的,需要系统分配
//id := cv.customerService.customerNum
customer := model.NewCustomer2(name, gender, age, phone, email)
//调用
if cv.customerService.Add(customer) {
fmt.Printf("-------------------添加成功-------------------\n\n")
} else {
fmt.Printf("-------------------添加失败-------------------\n\n")
}
}
//得到用户的输入 id,删除该 id 对应的客户 func (cv *customerView) delete() { fmt.Println(“—————————-删除客户—————————-“) fmt.Print(“请选择待删除客户编号(-1退出):”) id := -1 fmt.Scanln(&id) if id == -1 { return //放弃删除 } fmt.Print(“确认是否删除(Y/N):”) choice := “” fmt.Scanln(&choice) if choice == “y” || choice == “Y” { //调用 customerService 的 Delete 方法 if cv.customerService.Delete(id) { fmt.Printf(“—————————-删除完成—————————-\n\n”) } else { fmt.Printf(“—————-删除失败,输入 id 号不存在——————\n\n”) } }
}
//退出软件 func (cv *customerView) exit() { fmt.Printf(“确认是否退出(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" {
cv.loop = false
}
}
//显示主菜单
func (cv *customerView) mainMenu() { for { fmt.Println(“————————客户信息管理软件————————“) fmt.Println(“ 1 添 加 客 户”) fmt.Println(“ 2 修 改 客 户”) fmt.Println(“ 3 删 除 客 户”) fmt.Println(“ 4 客 户 列 表”) fmt.Println(“ 5 退 出”) fmt.Println() fmt.Print(“ 请选择(1-5):”) fmt.Scanln(&cv.key) switch cv.key { case “1”: cv.add() case “2”: fmt.Println(“修 改 客 户”) case “3”: cv.delete() case “4”: cv.list() case “5”: cv.exit() default: fmt.Println(“你的输入有误,请重新输入…”) } if !cv.loop { break } } fmt.Println(“你退出了客户关系管理系统”) }
func main() { //fmt.Println(“ok”) //在 main 主函数,创建一个 customerView,并运行显示主菜单… customerView := customerView{ key : “”, loop : true, } //这里完成对 customerView 结构体的 customerService 字段的初始化 customerView.customerService = service.NewCustomerService() //显示主菜单.. customerView.mainMenu() }
<a name="6a84ed3c"></a>
### 项目功能实现-完成修改客户的功能
1. 代码实现
```go
//-customerManage/service/customerSerive.go
package service
import (
"GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
"fmt"
)
//该 CustomerService,完成对 Customer 的操作,包括增删改查
type CustomerService struct {
customers []model.Customer
//声明一个字段,表示当前切片含有多少个客户
//该字段后面,还可以作为新客户的 Id(customerNum + 1)
customerNum int
}
//编写一个方法, 可以返回 *CustomerService
func NewCustomerService() *CustomerService {
//为了能够看到有客户在切片中,我们初始化一个客户
customerService := &CustomerService{}
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男", 20, "112", "zs@souhu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (cs *CustomerService) List() []model.Customer {
return cs.customers
}
//添加客户到 customer 切片
//!!!
func (cs *CustomerService) Add(customer model.Customer) bool {
//我们确定一个分配 id 规则,就是添加顺序
cs.customerNum ++
customer.Id = cs.customerNum
cs.customers = append(cs.customers, customer)
return true
}
//根据 id 查找客户再切片中国对应的下标,如果没有该客户,返回 -1。
func (cs *CustomerService) FindById(id int) int {
index := -1
//遍历
for i := 0; i < len(cs.customers); i++ {
if cs.customers[i].Id == id {
//找到
index = i
}
}
return index
}
//根据 id 删除客户(从切片中删除)
func (cs *CustomerService) Delete(id int) bool {
index := cs.FindById(id)
//如果 index == -1, 说明没有这个客户
if index == -1 {
return false
}
//如何从切片中删除一个元素
cs.customers = append(cs.customers[:index], cs.customers[index + 1:]...)
return true
}
//根据 id 更新客户
func (cs *CustomerService) Update(id int) bool {
index := cs.FindById(id)
//如果 index == -1, 说明没有这个客户
if index == -1 {
return false
}
fmt.Printf("姓名(%v):", cs.customers[index].Name)
name := ""
fmt.Scanln(&name)
if name != "" {
//fmt.Println()
cs.customers[index].Name = name
}
fmt.Printf("性别(%v):", cs.customers[index].Gender)
gender := ""
fmt.Scanln(&gender)
if gender != "" {
//fmt.Println()
cs.customers[index].Gender = gender
}
fmt.Printf("年龄(%v):", cs.customers[index].Age)
age := 0
fmt.Scanln(&age)
if age != 0 {
//fmt.Println()
cs.customers[index].Age = age
}
fmt.Printf("电话(%v):", cs.customers[index].Phone)
phone := ""
fmt.Scanln(&phone)
if phone != "" {
//fmt.Println()
cs.customers[index].Phone = phone
}
fmt.Printf("邮箱(%v):", cs.customers[index].Email)
email := ""
fmt.Scanln(&email)
if email != "" {
//fmt.Println()
cs.customers[index].Email = email
}
return true
}
//-customerManage/view/customerView
package main
import (
"GoProject/src/go_code/chapter13/project_case_01/customerManage/model"
"GoProject/src/go_code/chapter13/project_case_01/customerManage/service"
"fmt"
)
type customerView struct {
//定义必要的字段
key string //接受用户的输入
loop bool //表示是否循环的显示菜单
//增加一个字段 customerService
customerService *service.CustomerService
}
//显示所有的客户信息
func (cv *customerView) list() {
//首先,获取到当前所有的用户信息
customers := cv.customerService.List()
//显示
fmt.Println("\n-------------------客户列表-------------------")
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 (cv *customerView) add() {
fmt.Println("\n-------------------添加客户-------------------")
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 是唯一的,需要系统分配
//id := cv.customerService.customerNum
customer := model.NewCustomer2(name, gender, age, phone, email)
//调用
if cv.customerService.Add(customer) {
fmt.Printf("\n-------------------添加成功-------------------\n\n")
} else {
fmt.Printf("\n-------------------添加失败-------------------\n\n")
}
}
//得到用户的输入 id,删除该 id 对应的客户
func (cv *customerView) delete() {
fmt.Println("\n-------------------删除客户-------------------")
fmt.Print("请选择待删除客户编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return //放弃删除
}
fmt.Print("确认是否删除(Y/N):")
choice := ""
fmt.Scanln(&choice)
if choice == "y" || choice == "Y" {
//调用 customerService 的 Delete 方法
if cv.customerService.Delete(id) {
fmt.Printf("\n-------------------删除完成-------------------\n\n")
} else {
fmt.Printf("\n-----------删除失败,输入 id 号不存在------------\n\n")
}
}
}
//得到用户的输入 id,修改该 id 对应的客户信息
func (cv *customerView) update() {
fmt.Println("\n-------------------修改客户-------------------")
fmt.Print("请选择待修改客户编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return //放弃修改
}
if cv.customerService.Update(id) {
fmt.Printf("\n-------------------删除完成-------------------\n\n")
} else {
fmt.Printf("\n-----------修改失败,输入 id 号不存在------------\n\n")
}
}
//退出软件
func (cv *customerView) exit() {
fmt.Printf("确认是否退出(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" {
cv.loop = false
}
}
//显示主菜单
func (cv *customerView) mainMenu() {
for {
fmt.Println("----------------客户信息管理软件----------------")
fmt.Println(" 1 添 加 客 户")
fmt.Println(" 2 修 改 客 户")
fmt.Println(" 3 删 除 客 户")
fmt.Println(" 4 客 户 列 表")
fmt.Println(" 5 退 出")
fmt.Println()
fmt.Print(" 请选择(1-5):")
fmt.Scanln(&cv.key)
switch cv.key {
case "1":
cv.add()
case "2":
//fmt.Println("修 改 客 户")
cv.update()
case "3":
cv.delete()
case "4":
cv.list()
case "5":
cv.exit()
default:
fmt.Println("你的输入有误,请重新输入...")
}
if !cv.loop {
break
}
}
fmt.Println("你退出了客户关系管理系统")
}
func main() {
//fmt.Println("ok")
//在 main 主函数,创建一个 customerView,并运行显示主菜单...
customerView := customerView{
key : "",
loop : true,
}
//这里完成对 customerView 结构体的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单..
customerView.mainMenu()
}