项目需求

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

image.png

  1. 添加客户

image.png

  1. 修改客户

image.png

  1. 删除客户

image.png

  1. 客户列表

image.png

  1. 退出

程序框架图

image.png

MVC架构

image.png

代码实现

Model层(M)

go_code/customerItem/model/customer.go

  1. package model
  2. import "fmt"
  3. // 定义 Customer 结构体
  4. type Customer struct {
  5. Id int
  6. Name string
  7. Gender string
  8. Age int
  9. Phone string
  10. Email string
  11. }
  12. // 工厂模式的函数,创建 Customer 实例, 并返回 Customer 实例
  13. func NewCustomer(id int, name string, gender string,
  14. age int, phone string, email string) Customer {
  15. return Customer{
  16. Id: id,
  17. Name: name,
  18. Gender: gender,
  19. Age: age,
  20. Phone: phone,
  21. Email: email,
  22. }
  23. }
  24. // 第二种创建 Customer 实例的方法,不穿 Id, 由系统分配
  25. func NewCustomer2(name string, gender string,
  26. age int, phone string, email string) Customer {
  27. return Customer{
  28. Name: name,
  29. Gender: gender,
  30. Age: age,
  31. Phone: phone,
  32. Email: email,
  33. }
  34. }
  35. // 编写 GetInfo() 方法,返回当前用户的信息
  36. func (customer Customer) GetInfo() string {
  37. info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t",
  38. customer.Id,
  39. customer.Name,
  40. customer.Gender,
  41. customer.Age,
  42. customer.Phone,
  43. customer.Email)
  44. return info
  45. }

视图层(V)

go_code/customerItem/view/customerView.go

  1. package main
  2. import (
  3. "fmt"
  4. "go_code/customerItem/model"
  5. "go_code/customerItem/service"
  6. )
  7. type customerView struct {
  8. key string
  9. loop bool
  10. customerService *service.CustomerService
  11. }
  12. // 编写 list() 方法,调用 CustomerService.List() 展示所有客户信息
  13. func (customer *customerView) list() {
  14. // 获取当前客户信息
  15. customers := customer.customerService.List()
  16. // 显示出来
  17. fmt.Println("***********************客户列表***********************")
  18. fmt.Println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱")
  19. for i := 0; i < len(customers); i++ {
  20. fmt.Println(customers[i].GetInfo())
  21. }
  22. fmt.Println("*********************客户列表完成*********************")
  23. }
  24. // 编写 add() 方法,调用 CustomerService.Add() 方法,新增客户
  25. func (customer *customerView) add() {
  26. fmt.Println("***********************添加客户***********************")
  27. fmt.Println("姓名: ")
  28. name := ""
  29. fmt.Scanln(&name)
  30. fmt.Println("性别: ")
  31. gender := ""
  32. fmt.Scanln(&gender)
  33. fmt.Println("年龄: ")
  34. age := 0
  35. fmt.Scanln(&age)
  36. fmt.Println("电话: ")
  37. phone := ""
  38. fmt.Scanln(&phone)
  39. fmt.Println("邮箱: ")
  40. email := ""
  41. fmt.Scanln(&email)
  42. newCustomer := model.NewCustomer2(name, gender, age, phone, email)
  43. isAdd := customer.customerService.Add(newCustomer)
  44. if isAdd {
  45. fmt.Println("*********************客户添加完成*********************")
  46. } else {
  47. fmt.Println("*********************客户添加失败*********************")
  48. }
  49. }
  50. // 编写 delete() 方法,调用 CustomerService.Delete() 方法,删除客户
  51. func (customer *customerView) delete() {
  52. fmt.Println("***********************删除客户***********************")
  53. fmt.Println("请输入要删除的客户编号(-1:退出):")
  54. id := -1
  55. fmt.Scanln(&id)
  56. if id == -1 {
  57. return
  58. }
  59. fmt.Println("确认是否删除(Y/N): ")
  60. choice := ""
  61. fmt.Scanln(&choice)
  62. if choice == "Y" {
  63. // 执行删除
  64. if customer.customerService.Delete(id) {
  65. fmt.Println("***********************删除成功***********************")
  66. } else {
  67. fmt.Println("***********************删除失败***********************")
  68. }
  69. }
  70. }
  71. // 编写 update() 方法,调用 CustomerService.Update() 方法,修改客户
  72. func (customer *customerView) update() {
  73. fmt.Println("***********************修改客户***********************")
  74. fmt.Println("请输入要删除的客户编号(-1:退出):")
  75. id := -1
  76. fmt.Scanln(&id)
  77. if id == -1 {
  78. return
  79. }
  80. // 输入回车,不修改
  81. _, selectCustomer := customer.customerService.FindById(id)
  82. fmt.Printf("姓名(%v): ", selectCustomer.Name)
  83. name := ""
  84. fmt.Scanln(&name)
  85. fmt.Printf("性别(%v): ", selectCustomer.Gender)
  86. gender := ""
  87. fmt.Scanln(&gender)
  88. fmt.Printf("年龄(%v): ", selectCustomer.Age)
  89. age := 0
  90. fmt.Scanln(&age)
  91. fmt.Printf("电话(%v): ", selectCustomer.Phone)
  92. phone := ""
  93. fmt.Scanln(&phone)
  94. fmt.Printf("邮箱(%v): ", selectCustomer.Email)
  95. email := ""
  96. fmt.Scanln(&email)
  97. // 回车表示不修改
  98. if name == "" {
  99. name = selectCustomer.Name
  100. }
  101. if gender == "" {
  102. gender = selectCustomer.Gender
  103. }
  104. if age == 0 {
  105. age = selectCustomer.Age
  106. }
  107. if phone == "" {
  108. phone = selectCustomer.Phone
  109. }
  110. if email == "" {
  111. email = selectCustomer.Email
  112. }
  113. newCustomer := model.NewCustomer(id, name, gender, age, phone, email)
  114. isUpdate := customer.customerService.Update(id, newCustomer)
  115. if isUpdate {
  116. fmt.Println("*********************客户修改完成*********************")
  117. } else {
  118. fmt.Println("*********************客户修改失败*********************")
  119. }
  120. }
  121. // 退出方法
  122. func (customer *customerView) exit() {
  123. fmt.Println("你确定退出吗,请输入:Y(是)/N(否)")
  124. choice := ""
  125. for {
  126. fmt.Scanln(&choice)
  127. if choice == "Y" || choice == "N" {
  128. break
  129. }
  130. fmt.Println("你的输入有误,请重新输入:Y/N")
  131. }
  132. if choice == "Y" {
  133. customer.loop = false
  134. }
  135. }
  136. // 显示主菜单
  137. func (customer *customerView) mainView() {
  138. for {
  139. fmt.Println("***********************客户信息管理软件***********************")
  140. fmt.Println(" 1、添加客户")
  141. fmt.Println(" 2、修改客户")
  142. fmt.Println(" 3、删除客户")
  143. fmt.Println(" 4、客户列表")
  144. fmt.Println(" 5、退出")
  145. fmt.Print("请选择(1-5): ")
  146. fmt.Scanln(&customer.key)
  147. switch customer.key {
  148. case "1":
  149. customer.add()
  150. case "2":
  151. customer.update()
  152. case "3":
  153. customer.delete()
  154. case "4":
  155. customer.list()
  156. case "5":
  157. customer.exit()
  158. default:
  159. fmt.Println("输入有误,请重新输入")
  160. }
  161. if !customer.loop {
  162. break
  163. }
  164. fmt.Println()
  165. fmt.Println()
  166. }
  167. fmt.Println("你已退出客户关系管理系统")
  168. }
  169. func main() {
  170. fmt.Println("ok")
  171. // 初始化创建一个客户
  172. customerView := customerView{
  173. key: "",
  174. loop: true,
  175. }
  176. customerView.customerService = service.NewCustomerService()
  177. customerView.mainView()
  178. }

控制层/服务层(C)

go_code/customerItem/service/customerService.go

  1. package service
  2. import (
  3. "fmt"
  4. "go_code/customerItem/model"
  5. )
  6. // 封装对 Customer 的操作方法(增删改查)
  7. type CustomerService struct {
  8. customers []model.Customer
  9. // 声明一个字段,表示当前切片有多少客户,
  10. // 该字段还可以作为客户的Id号
  11. customerNum int
  12. }
  13. func NewCustomerService() *CustomerService {
  14. customerService := CustomerService{}
  15. customerService.customerNum = 1
  16. // id int, name string, gender string, age int, phone string, email string
  17. customer := model.NewCustomer(1, "tom", "男", 20, "16000000000", "16000000000@email.com")
  18. customerService.customers = append(customerService.customers, customer)
  19. return &customerService
  20. }
  21. // 编写 List() 方法, 返回客户切片
  22. func (customerService *CustomerService) List() []model.Customer {
  23. return customerService.customers
  24. }
  25. // 编写 Add() 方法,新增客户
  26. // *CustomerService 指针接收者
  27. func (customerService *CustomerService) Add(customer model.Customer) bool {
  28. // 客户编号处理
  29. customerService.customerNum++
  30. customer.Id = customerService.customerNum
  31. customerService.customers = append(customerService.customers, customer)
  32. return true
  33. }
  34. // 编写 Delete() 方法,删除客户
  35. func (customerService *CustomerService) Delete(id int) bool {
  36. index, customer := customerService.FindById(id)
  37. fmt.Println("customer = ", customer)
  38. if index != -1 {
  39. // 删除 id 对应的客户
  40. customerService.customers = append(customerService.customers[:index], customerService.customers[index+1:]...)
  41. return true
  42. } else {
  43. fmt.Printf("找不到编号: %v 的客户\n", id)
  44. return false
  45. }
  46. }
  47. // 编写 Update() 方法,修改客户
  48. func (customerService *CustomerService) Update(id int, customer model.Customer) bool {
  49. index, _ := customerService.FindById(id)
  50. if index != -1 {
  51. // 修改 id 对应的客户
  52. customerService.customers[index] = customer
  53. return true
  54. } else {
  55. fmt.Printf("找不到编号: %v 的客户\n", id)
  56. return false
  57. }
  58. }
  59. // 编写 FindById(id) 方法,查找客户,并返回客户切片下标, 和对应的客户信息 (编号个底层切片下标存在不对应的情况)
  60. func (customerService *CustomerService) FindById(id int) (int, model.Customer) {
  61. // 查找客户切片下标,有则返回,没有则返回 -1
  62. index := -1
  63. var customer model.Customer
  64. for i := 0; i < len(customerService.customers); i++ {
  65. if customerService.customers[i].Id == id {
  66. index = i
  67. customer = customerService.customers[i]
  68. }
  69. }
  70. return index, customer
  71. }