项目需求
- 模拟实现基于文本界面的《客户信息管理软件》
- 该软件可以实现对客户对象的插入、修改、删除(用切片实现),并能够打印客户明细表
- 项目界面
- 主菜单

- 添加客户

- 修改客户

- 删除客户

- 客户列表

- 退出
程序框架图
MVC架构
代码实现
Model层(M)
go_code/customerItem/model/customer.go
package modelimport "fmt"// 定义 Customer 结构体type Customer struct {Id intName stringGender stringAge intPhone stringEmail 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 mainimport ("fmt""go_code/customerItem/model""go_code/customerItem/service")type customerView struct {key stringloop boolcustomerService *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 := 0fmt.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 := -1fmt.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 := -1fmt.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 := 0fmt.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 serviceimport ("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 stringcustomer := 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.customerNumcustomerService.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] = customerreturn true} else {fmt.Printf("找不到编号: %v 的客户\n", id)return false}}// 编写 FindById(id) 方法,查找客户,并返回客户切片下标, 和对应的客户信息 (编号个底层切片下标存在不对应的情况)func (customerService *CustomerService) FindById(id int) (int, model.Customer) {// 查找客户切片下标,有则返回,没有则返回 -1index := -1var customer model.Customerfor i := 0; i < len(customerService.customers); i++ {if customerService.customers[i].Id == id {index = icustomer = customerService.customers[i]}}return index, customer}
