客户信息关系系统
项目需求分析
- 模拟实现基于文本界面的《客户信息管理软件》。
- 该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表
项目的界面设计
-
主菜单界面
-
添加客户界面
-
修改客户界面
-
删除客户界面
-
客户列表界面
客户关系管理系统的程序框架图
项目功能实现-显示主菜单和完成退出软件功能
- 功能的说明
当用户运行程序时,可以看到主菜单,当输入 5 时,可以退出该软件. - 思路分析
编写 customerView.go ,另外可以把 customer.go 和 customerService.go 写上. - 代码实现
customerManage/model/customer.go
package model
//声明一个结构体
import (
"fmt"
)
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,
}
}
customerManage/service/customerService.go
package service
import (
"go_code/customerManagge/model"
)
//完成对Customer的操作
type CustomerService struct {
//定义一个客户切片,可以存放客户信息
customers []*model.Customer
//定义客户的实际个数
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();
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()
}
项目功能实现-完成显示客户列表的功能
- 功能说明
- 思路分析
- 代码实现
customerManage/model/customer.go
package model
//声明一个结构体
import (
"fmt"
)
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,
}
}
//返回用户信息
func (this Customer) GetInfo() string{
info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v", this.Id, this.Name, this.Gender,
this.Age, this.Phone, this.Email)
return info
}
customerManage/service/customerService.go[增加了两个方法]
package service
import (
"go_code/customerManage/model"
)
//完成对Customer的操作
type CustomerService struct {
//定义一个客户切片,可以存放客户信息
customers []*model.Customer
//定义客户的实际个数
customerNum int
}
//编写一个方法,返回*CustomerServer
func NewCustomerService() *CustomerService {
customerService := &CustomerService{}
//customerService.customers = make([]model.Customer, 1)
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男",
10, "999", "zs@sohu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (this *CustomerService) List() []*model.Customer {
return this.customers
}
customerManage/view/customerView.go
package main
import (
"fmt"
"go_code/customerManage/service"
)
type customerView struct{
//定义必要的字段
key string //接受输入
loop bool //是否循环显示菜单
//增加一个CustomerService字段
customerService *service.CustomerService
}
//显示所有客户信息
func (this * customerView) list(){
//首先,获取当前所有客户信息(切片中)
customerList := this.customerService .List()
//显示
fmt.Println("---------------------------客户列表---------------------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
//遍历
for i := 0; i < len(customerList); i++ {
fmt.Println(customerList[i].GetInfo())
}
fmt.Printf("\n---------------------------客户列表完成---------------------------\n\n")
}
func (this *customerView) mainMenu(){
for {
fmt.Println("-----------------客户信息管理软件-----------------")
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":
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结构体customerServer字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单
customerView.mainMenu()
}
项目功能实现-添加客户的功能
- 功能说明
- 思路分析
- 代码实现
customerManage/model/customer.go
package model
//声明一个结构体
import (
"fmt"
)
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,
}
}
//第二章创建Customer方法
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", this.Id, this.Name, this.Gender,
this.Age, this.Phone, this.Email)
return info
}
customerManage/service/customerService.go
package service
import (
"go_code/customerManage/model"
)
//完成对Customer的操作
type CustomerService struct {
//定义一个客户切片,可以存放客户信息
customers []*model.Customer
//定义客户的实际个数
customerNum int
}
//编写一个方法,返回*CustomerServer
func NewCustomerService() *CustomerService {
customerService := &CustomerService{}
//customerService.customers = make([]model.Customer, 1)
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男",
10, "999", "zs@sohu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (this *CustomerService) List() []*model.Customer {
return this.customers
}
//返回客户切片
//!使用指针
func (this *CustomerService) Add(customer *model.Customer) bool {
//确定一个分配id的规则,就是添加的顺序
this.customerNum++
customer.Id = this.customerNum
this.customers = append(this.customers, customer)
return true
}
customerManage/service/customerView.go
package main
import (
"fmt"
"go_code/customerManage/service"
"go_code/customerManage/model"
)
type CustomerView struct{
//定义必要的字段
key string //接受输入
loop bool //是否循环显示菜单
//增加一个CustomerService字段
customerService *service.CustomerService
}
//显示所有客户信息
func (this * CustomerView) list(){
//首先,获取当前所有客户信息(切片中)
customerList := this.customerService .List()
//显示
fmt.Println("---------------------------客户列表---------------------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
//遍历
for i := 0; i < len(customerList); i++ {
fmt.Println(customerList[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)
age := 0
fmt.Println("年龄:")
fmt.Scanln(&age)
fmt.Println("电话:");
phone := ""
fmt.Scanln(&phone)
fmt.Println("邮箱:");
email := ""
fmt.Scanln(&email)
//根据用户输入,创建一个Customer对象
customer := model.NewCustomer2(name, gender, age, phone, email)
if(this.customerService.Add(customer)){
fmt.Println("---------------------添加客户成功---------------------");
}else{
fmt.Println("---------------------添加客户失败---------------------");
}
}
func (this *CustomerView) mainMenu(){
for {
fmt.Println("-----------------客户信息管理软件-----------------")
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("修改")
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结构体customerServer字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单
customerView.mainMenu()
}
项目功能实现-完成删除客户的功能
- 功能说明
- 思路分析
- 代码实现
customerManage/model/customer.go [没有变化]
customerManage/service/customerService.go
package service
import (
"go_code/customerManage/model"
)
//完成对Customer的操作
type CustomerService struct {
//定义一个客户切片,可以存放客户信息
customers []*model.Customer
//定义客户的实际个数
customerNum int
}
//编写一个方法,返回*CustomerServer
func NewCustomerService() *CustomerService {
customerService := &CustomerService{}
//customerService.customers = make([]model.Customer, 1)
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男",
10, "999", "zs@sohu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (this *CustomerService) List() []*model.Customer {
return this.customers
}
//返回客户切片
//!使用指针
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.FindId(id)
if index == -1{
//没有客户
return false
} else {
this.customers = append(this.customers[:index], this.customers[index+1:]...)
return true
}
}
//根据id查找客户在切片中的下标
func (this *CustomerService) FindId(id int) int {
index := -1
for i := 0; i < len(this.customers); i++{
if this.customers[i].Id == id{
//找到
index = i
}
}
return index
}
customerManage/view/customerView.go
package main
import (
"fmt"
"go_code/customerManage/service"
"go_code/customerManage/model"
)
type CustomerView struct{
//定义必要的字段
key string //接受输入
loop bool //是否循环显示菜单
//增加一个CustomerService字段
customerService *service.CustomerService
}
//显示所有客户信息
func (this * CustomerView) list(){
//首先,获取当前所有客户信息(切片中)
customerList := this.customerService .List()
//显示
fmt.Println("---------------------------客户列表---------------------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
//遍历
for i := 0; i < len(customerList); i++ {
fmt.Println(customerList[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)
age := 0
fmt.Println("年龄:")
fmt.Scanln(&age)
fmt.Println("电话:");
phone := ""
fmt.Scanln(&phone)
fmt.Println("邮箱:");
email := ""
fmt.Scanln(&email)
//根据用户输入,创建一个Customer对象
customer := model.NewCustomer2(name, gender, age, phone, email)
if(this.customerService.Add(customer)){
fmt.Println("---------------------添加客户成功---------------------");
}else{
fmt.Println("---------------------添加客户失败---------------------");
}
}
func (this *CustomerView) mainMenu(){
for {
fmt.Println("-----------------客户信息管理软件-----------------")
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("修改")
case "3":
this.delete()
case "4":
this.list()
case "5":
this.loop = false
default:
fmt.Println("输入有误")
}
if !this.loop{
break
}
}
fmt.Println("已退出")
}
//得到输入,删除id
func (this *CustomerView) delete() {
fmt.Println("---------------------删除客户---------------------")
fmt.Println("请选择待删除客户编号(-1退出)")
id := 0
fmt.Scanln(&id)
//如果用户输入-1
if id == -1 {
return
}
fmt.Println("确认是否删除(Y/N):")
choice := ""
fmt.Scanln(&choice) // 可以
if choice == "Y" || choice == "y" {
if this.customerService.Delete(id) {
fmt.Println("---------------------删除完成---------------------")
} else {
fmt.Println("---------------------删除失败,id不存在---------------------")
}
}
}
func main(){
//在主函数中创建一个customerView实例,并运行
customerView := CustomerView{
key : "",
loop : true,
}
//完成对customerView结构体customerServer字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单
customerView.mainMenu()
}
项目功能实现-完善退出确认功能
- 功能说明:
要求用户在退出时提示 " 确认是否退出(Y/N):",用户必须输入 y/n, 否则循环提示。 - 思路分析:
需要编写 customerView.go - 代码实现:
customerManage/view/customerView.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
}
}
客户关系管理系统
- 代码实现
customerManage/model/customer.go [没有变化]
customerManage/service/customerService.go
package service
import (
"go_code/customerManage/model"
)
//完成对Customer的操作
type CustomerService struct {
//定义一个客户切片,可以存放客户信息
customers []*model.Customer
//定义客户的实际个数
customerNum int
}
//编写一个方法,返回*CustomerServer
func NewCustomerService() *CustomerService {
customerService := &CustomerService{}
//customerService.customers = make([]model.Customer, 1)
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男",
10, "999", "zs@sohu.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (this *CustomerService) List() []*model.Customer {
return this.customers
}
//返回客户切片
//!使用指针
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.FindId(id)
if index == -1{
//没有客户
return false
} else {
this.customers = append(this.customers[:index], this.customers[index+1:]...)
return true
}
}
//修改
//根据id修改客户信息
func (cs *CustomerService) Update(id int, customer model.Customer) bool {
for i := 0; i < len(cs.customers); i++ {
if cs.customers[i].Id == id {
cs.customers[i].Name = customer.Name
cs.customers[i].Gender = customer.Gender
cs.customers[i].Age = customer.Age
cs.customers[i].Phone = customer.Phone
cs.customers[i].Email = customer.Email
}
}
return true
}
//根据id查找客户在切片中的下标
func (this *CustomerService) FindId(id int) int {
index := -1
for i := 0; i < len(this.customers); i++{
if this.customers[i].Id == id{
//找到
index = i
}
}
return index
}
func (cs *CustomerService) GetInfoById(id int) *model.Customer {
i := id - 1
return cs.customers[i]
}
customerManage/view/customerView.go
package main
import (
"fmt"
"go_code/customerManage/service"
"go_code/customerManage/model"
)
type CustomerView struct{
//定义必要的字段
key string //接受输入
loop bool //是否循环显示菜单
//增加一个CustomerService字段
customerService *service.CustomerService
}
//显示所有客户信息
func (this * CustomerView) list(){
//首先,获取当前所有客户信息(切片中)
customerList := this.customerService .List()
//显示
fmt.Println("---------------------------客户列表---------------------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
//遍历
for i := 0; i < len(customerList); i++ {
fmt.Println(customerList[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)
age := 0
fmt.Println("年龄:")
fmt.Scanln(&age)
fmt.Println("电话:");
phone := ""
fmt.Scanln(&phone)
fmt.Println("邮箱:");
email := ""
fmt.Scanln(&email)
//根据用户输入,创建一个Customer对象
customer := model.NewCustomer2(name, gender, age, phone, email)
if(this.customerService.Add(customer)){
fmt.Println("---------------------添加客户成功---------------------");
}else{
fmt.Println("---------------------添加客户失败---------------------");
}
}
//退出
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
}
}
//得到用户的输入id,修改该id对应的客户
func (cv *CustomerView) update() {
cv.list()
fmt.Println()
fmt.Println("------------------修改客户------------------")
fmt.Print("请选择等待修改客户编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return //放弃删除操作
}
fmt.Print("确认是否修改(Y/N):")
//这里可以加入一个循环判断,直到用户输入y或者n,才退出...
choice := ""
fmt.Scanln(&choice)
if choice == "y" || choice == "Y" {
//调用customerService的Delete方法
if cv.customerService.FindId(id) != -1 {
customer := cv.customerService.GetInfoById(id)
fmt.Printf("姓名(%v:)", customer.Name)
name := ""
fmt.Scanln(&name)
fmt.Printf("性别(%v):", customer.Gender)
gender := ""
fmt.Scanln(&gender)
fmt.Printf("年龄(%v):", customer.Age)
age := 0
fmt.Scanln(&age)
fmt.Printf("电话(%v):", customer.Phone)
phone := ""
fmt.Scanln(&phone)
fmt.Printf("邮箱(%v):", customer.Email)
email := ""
fmt.Scanln(&email)
customer2 := model.NewCustomer2(name, gender, age, phone, email)
cv.customerService.Update(id, *customer2)
fmt.Println("------------------修改完成------------------")
} else {
fmt.Println("------------------修改失败,输入的id号不存在")
}
}
}
func (this *CustomerView) delete() {
fmt.Println("---------------------删除客户---------------------")
fmt.Println("请选择待删除客户编号(-1退出)")
id := 0
fmt.Scanln(&id)
//如果用户输入-1
if id == -1 {
return
}
fmt.Println("确认是否删除(Y/N):")
choice := ""
fmt.Scanln(&choice) // 可以
if choice == "Y" || choice == "y" {
if this.customerService.Delete(id) {
fmt.Println("---------------------删除完成---------------------")
} else {
fmt.Println("---------------------删除失败,id不存在---------------------")
}
}
}
func (this *CustomerView) mainMenu(){
for {
fmt.Println("-----------------客户信息管理软件-----------------")
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":
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结构体customerServer字段的初始化
customerView.customerService = service.NewCustomerService()
//显示主菜单
customerView.mainMenu()
}