项目需求
- 模拟实现基于文本界面的《客户信息管理软件》
- 该软件可以实现对客户对象的插入、修改、删除(用切片实现),并能够打印客户明细表
- 项目界面
- 主菜单
- 添加客户
- 修改客户
- 删除客户
- 客户列表
- 退出
程序框架图
MVC架构
代码实现
Model层(M)
go_code/customerItem/model/customer.go
package model
import "fmt"
// 定义 Customer 结构体
type Customer struct {
Id int
Name string
Gender string
Age int
Phone string
Email string
}
// 工厂模式的函数,创建 Customer 实例, 并返回 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,
}
}
// 编写 GetInfo() 方法,返回当前用户的信息
func (customer Customer) GetInfo() string {
info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t",
customer.Id,
customer.Name,
customer.Gender,
customer.Age,
customer.Phone,
customer.Email)
return info
}
视图层(V)
go_code/customerItem/view/customerView.go
package main
import (
"fmt"
"go_code/customerItem/model"
"go_code/customerItem/service"
)
type customerView struct {
key string
loop bool
customerService *service.CustomerService
}
// 编写 list() 方法,调用 CustomerService.List() 展示所有客户信息
func (customer *customerView) list() {
// 获取当前客户信息
customers := customer.customerService.List()
// 显示出来
fmt.Println("***********************客户列表***********************")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱")
for i := 0; i < len(customers); i++ {
fmt.Println(customers[i].GetInfo())
}
fmt.Println("*********************客户列表完成*********************")
}
// 编写 add() 方法,调用 CustomerService.Add() 方法,新增客户
func (customer *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)
newCustomer := model.NewCustomer2(name, gender, age, phone, email)
isAdd := customer.customerService.Add(newCustomer)
if isAdd {
fmt.Println("*********************客户添加完成*********************")
} else {
fmt.Println("*********************客户添加失败*********************")
}
}
// 编写 delete() 方法,调用 CustomerService.Delete() 方法,删除客户
func (customer *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" {
// 执行删除
if customer.customerService.Delete(id) {
fmt.Println("***********************删除成功***********************")
} else {
fmt.Println("***********************删除失败***********************")
}
}
}
// 编写 update() 方法,调用 CustomerService.Update() 方法,修改客户
func (customer *customerView) update() {
fmt.Println("***********************修改客户***********************")
fmt.Println("请输入要删除的客户编号(-1:退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return
}
// 输入回车,不修改
_, selectCustomer := customer.customerService.FindById(id)
fmt.Printf("姓名(%v): ", selectCustomer.Name)
name := ""
fmt.Scanln(&name)
fmt.Printf("性别(%v): ", selectCustomer.Gender)
gender := ""
fmt.Scanln(&gender)
fmt.Printf("年龄(%v): ", selectCustomer.Age)
age := 0
fmt.Scanln(&age)
fmt.Printf("电话(%v): ", selectCustomer.Phone)
phone := ""
fmt.Scanln(&phone)
fmt.Printf("邮箱(%v): ", selectCustomer.Email)
email := ""
fmt.Scanln(&email)
// 回车表示不修改
if name == "" {
name = selectCustomer.Name
}
if gender == "" {
gender = selectCustomer.Gender
}
if age == 0 {
age = selectCustomer.Age
}
if phone == "" {
phone = selectCustomer.Phone
}
if email == "" {
email = selectCustomer.Email
}
newCustomer := model.NewCustomer(id, name, gender, age, phone, email)
isUpdate := customer.customerService.Update(id, newCustomer)
if isUpdate {
fmt.Println("*********************客户修改完成*********************")
} else {
fmt.Println("*********************客户修改失败*********************")
}
}
// 退出方法
func (customer *customerView) exit() {
fmt.Println("你确定退出吗,请输入:Y(是)/N(否)")
choice := ""
for {
fmt.Scanln(&choice)
if choice == "Y" || choice == "N" {
break
}
fmt.Println("你的输入有误,请重新输入:Y/N")
}
if choice == "Y" {
customer.loop = false
}
}
// 显示主菜单
func (customer *customerView) mainView() {
for {
fmt.Println("***********************客户信息管理软件***********************")
fmt.Println(" 1、添加客户")
fmt.Println(" 2、修改客户")
fmt.Println(" 3、删除客户")
fmt.Println(" 4、客户列表")
fmt.Println(" 5、退出")
fmt.Print("请选择(1-5): ")
fmt.Scanln(&customer.key)
switch customer.key {
case "1":
customer.add()
case "2":
customer.update()
case "3":
customer.delete()
case "4":
customer.list()
case "5":
customer.exit()
default:
fmt.Println("输入有误,请重新输入")
}
if !customer.loop {
break
}
fmt.Println()
fmt.Println()
}
fmt.Println("你已退出客户关系管理系统")
}
func main() {
fmt.Println("ok")
// 初始化创建一个客户
customerView := customerView{
key: "",
loop: true,
}
customerView.customerService = service.NewCustomerService()
customerView.mainView()
}
控制层/服务层(C)
go_code/customerItem/service/customerService.go
package service
import (
"fmt"
"go_code/customerItem/model"
)
// 封装对 Customer 的操作方法(增删改查)
type CustomerService struct {
customers []model.Customer
// 声明一个字段,表示当前切片有多少客户,
// 该字段还可以作为客户的Id号
customerNum int
}
func NewCustomerService() *CustomerService {
customerService := CustomerService{}
customerService.customerNum = 1
// id int, name string, gender string, age int, phone string, email string
customer := model.NewCustomer(1, "tom", "男", 20, "16000000000", "16000000000@email.com")
customerService.customers = append(customerService.customers, customer)
return &customerService
}
// 编写 List() 方法, 返回客户切片
func (customerService *CustomerService) List() []model.Customer {
return customerService.customers
}
// 编写 Add() 方法,新增客户
// *CustomerService 指针接收者
func (customerService *CustomerService) Add(customer model.Customer) bool {
// 客户编号处理
customerService.customerNum++
customer.Id = customerService.customerNum
customerService.customers = append(customerService.customers, customer)
return true
}
// 编写 Delete() 方法,删除客户
func (customerService *CustomerService) Delete(id int) bool {
index, customer := customerService.FindById(id)
fmt.Println("customer = ", customer)
if index != -1 {
// 删除 id 对应的客户
customerService.customers = append(customerService.customers[:index], customerService.customers[index+1:]...)
return true
} else {
fmt.Printf("找不到编号: %v 的客户\n", id)
return false
}
}
// 编写 Update() 方法,修改客户
func (customerService *CustomerService) Update(id int, customer model.Customer) bool {
index, _ := customerService.FindById(id)
if index != -1 {
// 修改 id 对应的客户
customerService.customers[index] = customer
return true
} else {
fmt.Printf("找不到编号: %v 的客户\n", id)
return false
}
}
// 编写 FindById(id) 方法,查找客户,并返回客户切片下标, 和对应的客户信息 (编号个底层切片下标存在不对应的情况)
func (customerService *CustomerService) FindById(id int) (int, model.Customer) {
// 查找客户切片下标,有则返回,没有则返回 -1
index := -1
var customer model.Customer
for i := 0; i < len(customerService.customers); i++ {
if customerService.customers[i].Id == id {
index = i
customer = customerService.customers[i]
}
}
return index, customer
}