• [go学习笔记.第十一章.项目案例] 2.客户信息管理系统


    一.基本介绍

    1.需求说明

    项目需求分析

            1.模拟实现基于文本界面的 《 客户信息管理软件 》

            2.该软件实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表

    2.界面设计

    (1).主菜单界面

    (2).添加客户界面

    (3).修改客户界面

    (4).删除客户界面

     

    (5).客户列表界面

      

    二.程序框架图

    三.代码实现

    项目功能实现

    1.显示主菜单和完成退出软件功能

    功能说明

            当用户运行程序时,可以看到主菜单,当输入 5 时,可以退出该软件

    思路分析

            编写customerView.go,另外可以把customer.go和customerService.go写出来

    customer.go

    1. package model
    2. import (
    3. "fmt"
    4. )
    5. // 声明一个customer结构体,表示一个客户信息
    6. type Customer struct {
    7. Id int
    8. Name string
    9. Gender string
    10. Age int
    11. Phone string
    12. Email string
    13. }
    14. //使用工厂模式,返回一个Customer的实例
    15. func NewCustomer(id int, name string, gender string, age int, phone string, email string) *Customer {
    16. return Customer{
    17. Id: id,
    18. Name : name,
    19. Gender : gender,
    20. Age : age,
    21. Phone : phone,
    22. Email: email,
    23. }
    24. }

     customerService.go

    1. package service
    2. import (
    3. "go_code/customerManage/model"
    4. )
    5. //声明一个customerServive结构体,完成对Customer的操作,包括
    6. //增删改查
    7. type CustomerService struct {
    8. customers []model.Customer
    9. //声明一个字段,表示当前切片含有多少个用户
    10. //该字段后面还可以做为新客户的id+1
    11. customerNum int
    12. }

    customerView.go

    1. package main
    2. import (
    3. "fmt"
    4. )
    5. type customerView struct {
    6. //定义必要的字段
    7. key string //接收用户输入
    8. loop bool //表示是否循环显示主菜单
    9. }
    10. //显示主菜单
    11. func (this *customerView) mainMenu() {
    12. for {
    13. fmt.Println("\n----------------客户信息管理软件------------------")
    14. fmt.Println(" 1.添加客户")
    15. fmt.Println(" 2.修改客户")
    16. fmt.Println(" 3.删除客户")
    17. fmt.Println(" 4.客户列表")
    18. fmt.Println(" 5.退 出")
    19. fmt.Println("请选择(1~5):")
    20. fmt.Scanln(&this.key)
    21. switch this.key {
    22. case "1":
    23. fmt.Println("添加客户")
    24. case "2":
    25. fmt.Println("修改客户")
    26. case "3":
    27. fmt.Println("删除客户")
    28. case "4":
    29. fmt.Println("客户列表")
    30. case "5":
    31. this.loop = false
    32. default :
    33. fmt.Println("输入有误,请重新输入")
    34. }
    35. if !this.loop {
    36. break
    37. }
    38. }
    39. fmt.Println("已退出软件的使用")
    40. }
    41. func main() {
    42. //在主函数中创建一个CustomerView实例,并运行主菜单
    43. customerView := customerView {
    44. key : "",
    45. loop : true,
    46. }
    47. //显示主菜单
    48. customerView.mainMenu()
    49. }

    2.完成显示客户列表的功能

    功能说明

    思路分析: 见 二.程序框架图

    代码如下:

    customer.go

    1. package model
    2. import (
    3. "fmt"
    4. )
    5. // 声明一个customer结构体,表示一个客户信息
    6. type Customer struct {
    7. Id int
    8. Name string
    9. Gender string
    10. Age int
    11. Phone string
    12. Email string
    13. }
    14. //使用工厂模式,返回一个Customer的实例
    15. func NewCustomer(id int, name string, gender string, age int, phone string, email string) Customer {
    16. return Customer{
    17. Id: id,
    18. Name : name,
    19. Gender : gender,
    20. Age : age,
    21. Phone : phone,
    22. Email: email,
    23. }
    24. }
    25. //返回用户信息
    26. func (this *Customer) GetInfo() string {
    27. 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)
    28. return info
    29. }

    customerService.go

    1. package service
    2. import (
    3. "go_code/chapter12/customerManage/model"
    4. )
    5. //声明一个customerServive结构体,完成对Customer的操作,包括
    6. //增删改查
    7. type CustomerService struct {
    8. customers []model.Customer
    9. //声明一个字段,表示当前切片含有多少个用户
    10. //该字段后面还可以做为新客户的id+1
    11. customerNum int
    12. }
    13. //编写一个方法,可以返回*CustomerService实例
    14. func NewCustomerService() *CustomerService {
    15. //为了能够看到客户在切片中,初始化一个客户
    16. customerService := &CustomerService{}
    17. customerService.customerNum = 1
    18. customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
    19. //把customer切片追加到customers切片中
    20. customerService.customers = append(customerService.customers, customer)
    21. return customerService
    22. }
    23. //返回客户切片
    24. func (this *CustomerService) List() []model.Customer {
    25. return this.customers
    26. }

    customerView.go

    1. package main
    2. import (
    3. "fmt"
    4. "go_code/customerManage/service"
    5. )
    6. type customerView struct {
    7. //定义必要的字段
    8. key string //接收用户输入
    9. loop bool //表示是否循环显示主菜单
    10. //增加一个customerService
    11. customerService *service.CustomerService
    12. }
    13. //显示所有的客户信息
    14. func (this *customerView) list() {
    15. //获取到当前所有客户信息(在切片中)
    16. customers := this.customerService.List()
    17. //显示
    18. fmt.Println("\n----------------客户列表------------------\n")
    19. fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
    20. for i := 0; i < len(customers); i++ {
    21. fmt.Println(customers[i].GetInfo())
    22. }
    23. fmt.Println("----------------客户列表完成------------------")
    24. }
    25. //显示主菜单
    26. func (this *customerView) mainMenu() {
    27. for {
    28. fmt.Println("\n----------------客户信息管理软件------------------")
    29. fmt.Println(" 1.添加客户")
    30. fmt.Println(" 2.修改客户")
    31. fmt.Println(" 3.删除客户")
    32. fmt.Println(" 4.客户列表")
    33. fmt.Println(" 5.退 出")
    34. fmt.Println("请选择(1~5):")
    35. fmt.Scanln(&this.key)
    36. switch this.key {
    37. case "1":
    38. fmt.Println("添加客户")
    39. case "2":
    40. fmt.Println("修改客户")
    41. case "3":
    42. fmt.Println("删除客户")
    43. case "4":
    44. this.list()
    45. case "5":
    46. this.loop = false
    47. default :
    48. fmt.Println("输入有误,请重新输入")
    49. }
    50. if !this.loop {
    51. break
    52. }
    53. }
    54. fmt.Println("已退出软件的使用")
    55. }
    56. func main() {
    57. //在主函数中创建一个CustomerView实例,并运行主菜单
    58. customerView := customerView {
    59. key : "",
    60. loop : true,
    61. }
    62. //完成对customerView结构体的customerService字段初始化
    63. customerView.customerService = service.NewCustomerService()
    64. //显示主菜单
    65. customerView.mainMenu()
    66. }

    3.添加客户的功能

    功能说明

    思路分析

            见 二.程序框架图,需要编写customerView和customerService,customer类型,规定:新添加的成员的id就是第几个添加的id

    customer.go

    1. package model
    2. import (
    3. "fmt"
    4. )
    5. // 声明一个customer结构体,表示一个客户信息
    6. type Customer struct {
    7. Id int
    8. Name string
    9. Gender string
    10. Age int
    11. Phone string
    12. Email string
    13. }
    14. //使用工厂模式,返回一个Customer的实例
    15. func NewCustomer(id int, name string, gender string, age int, phone string, email string) Customer {
    16. return Customer{
    17. Id: id,
    18. Name : name,
    19. Gender : gender,
    20. Age : age,
    21. Phone : phone,
    22. Email: email,
    23. }
    24. }
    25. //第二种:不带id创建: 使用工厂模式,返回一个Customer的实例
    26. func NewCustomer2(name string, gender string, 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. //返回用户信息
    36. func (this *Customer) GetInfo() string {
    37. 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)
    38. return info
    39. }

    customerService.go

    1. package service
    2. import (
    3. "go_code/chapter12/customerManage/model"
    4. )
    5. //声明一个customerServive结构体,完成对Customer的操作,包括
    6. //增删改查
    7. type CustomerService struct {
    8. customers []model.Customer
    9. //声明一个字段,表示当前切片含有多少个用户
    10. //该字段后面还可以做为新客户的id+1
    11. customerNum int
    12. }
    13. //编写一个方法,可以返回*CustomerService实例
    14. func NewCustomerService() *CustomerService {
    15. //为了能够看到客户在切片中,初始化一个客户
    16. customerService := &CustomerService{}
    17. customerService.customerNum = 1
    18. customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
    19. //把customer切片追加到customers切片中
    20. customerService.customers = append(customerService.customers, customer)
    21. return customerService
    22. }
    23. //返回客户切片
    24. func (this *CustomerService) List() []model.Customer {
    25. return this.customers
    26. }
    27. //添加一个客户到切片
    28. func (this *CustomerService) Add(customer model.Customer) bool {
    29. //确定一个id的规则
    30. this.customerNum++
    31. customer.Id = this.customerNum
    32. //把customer切片追加到customers切片中
    33. this.customers = append(this.customers, customer)
    34. //判断
    35. return true
    36. }

    customerView.go

    1. package main
    2. import (
    3. "fmt"
    4. "go_code/chapter12/customerManage/service"
    5. "go_code/chapter12/customerManage/model"
    6. )
    7. type customerView struct {
    8. //定义必要的字段
    9. key string //接收用户输入
    10. loop bool //表示是否循环显示主菜单
    11. //增加一个customerService
    12. customerService *service.CustomerService
    13. }
    14. //显示所有的客户信息
    15. func (this *customerView) list() {
    16. //获取到当前所有客户信息(在切片中)
    17. customers := this.customerService.List()
    18. //显示
    19. fmt.Println("----------------客户列表------------------")
    20. fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
    21. for i := 0; i < len(customers); i++ {
    22. fmt.Println(customers[i].GetInfo())
    23. }
    24. fmt.Println("----------------客户列表完成------------------")
    25. }
    26. //添加客户信息:构建新的客户,并完成添加
    27. func (this *customerView) add() {
    28. fmt.Println("----------------添加客户------------------")
    29. fmt.Println("姓名:")
    30. name := ""
    31. fmt.Scanln(&name)
    32. fmt.Println("性别:")
    33. gender := ""
    34. fmt.Scanln(&gender)
    35. fmt.Println("年龄:")
    36. age := 0
    37. fmt.Scanln(&age)
    38. fmt.Println("电话:")
    39. phone := ""
    40. fmt.Scanln(&phone)
    41. fmt.Println("邮箱:")
    42. email := ""
    43. fmt.Scanln(&email)
    44. //构建一个新的Customer实例
    45. //注意:id号是系统分配的
    46. customer := model.NewCustomer2(name, gender, age, phone, email)
    47. //调用
    48. if this.customerService.Add(customer) {
    49. fmt.Println("----------------添加成功------------------")
    50. } else {
    51. fmt.Println("----------------添加失败------------------")
    52. }
    53. }
    54. //显示主菜单
    55. func (this *customerView) mainMenu() {
    56. for {
    57. fmt.Println("\n----------------客户信息管理软件------------------")
    58. fmt.Println(" 1.添加客户")
    59. fmt.Println(" 2.修改客户")
    60. fmt.Println(" 3.删除客户")
    61. fmt.Println(" 4.客户列表")
    62. fmt.Println(" 5.退 出")
    63. fmt.Println("请选择(1~5):")
    64. fmt.Scanln(&this.key)
    65. switch this.key {
    66. case "1":
    67. this.add()
    68. case "2":
    69. fmt.Println("修改客户")
    70. case "3":
    71. fmt.Println("删除客户")
    72. case "4":
    73. this.list()
    74. case "5":
    75. this.loop = false
    76. default :
    77. fmt.Println("输入有误,请重新输入")
    78. }
    79. if !this.loop {
    80. break
    81. }
    82. }
    83. fmt.Println("已退出软件的使用")
    84. }
    85. func main() {
    86. //在主函数中创建一个CustomerView实例,并运行主菜单
    87. customerView := customerView {
    88. key : "",
    89. loop : true,
    90. }
    91. //完成对customerView结构体的customerService字段初始化
    92. customerView.customerService = service.NewCustomerService()
    93. //显示主菜单
    94. customerView.mainMenu()
    95. }

    4.完成删除客户的功能

    功能说明

            

    思路分析

            见 二.程序框架图 

    customerService.go

    1. package service
    2. import (
    3. "go_code/chapter12/customerManage/model"
    4. )
    5. //声明一个customerServive结构体,完成对Customer的操作,包括
    6. //增删改查
    7. type CustomerService struct {
    8. customers []model.Customer
    9. //声明一个字段,表示当前切片含有多少个用户
    10. //该字段后面还可以做为新客户的id+1
    11. customerNum int
    12. }
    13. //编写一个方法,可以返回*CustomerService实例
    14. func NewCustomerService() *CustomerService {
    15. //为了能够看到客户在切片中,初始化一个客户
    16. customerService := &CustomerService{}
    17. customerService.customerNum = 1
    18. customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
    19. //把customer切片追加到customers切片中
    20. customerService.customers = append(customerService.customers, customer)
    21. return customerService
    22. }
    23. //返回客户切片
    24. func (this *CustomerService) List() []model.Customer {
    25. return this.customers
    26. }
    27. //添加一个客户到切片
    28. func (this *CustomerService) Add(customer model.Customer) bool {
    29. //确定一个id的规则
    30. this.customerNum++
    31. customer.Id = this.customerNum
    32. //把customer切片追加到customers切片中
    33. this.customers = append(this.customers, customer)
    34. //判断
    35. return true
    36. }
    37. //根据id删除客户(从切片中删除)
    38. func (this *CustomerService) Delete(id int) bool {
    39. index := this.FindById(id)
    40. if index == -1 {
    41. return false
    42. }
    43. //从切片中删除客户
    44. this.customers = append(this.customers[:index], this.customers[index + 1:]...)
    45. return true
    46. }
    47. //根据id查找客户在切片中的下标,如果没有,返回-1
    48. func (this *CustomerService) FindById(id int) int {
    49. index := -1
    50. //遍历this.customers
    51. for i := 0; i < len(this.customers); i++ {
    52. if this.customers[i].Id == id {
    53. //找到
    54. return i
    55. }
    56. }
    57. return index
    58. }

     customerView.go

    1. package main
    2. import (
    3. "fmt"
    4. "go_code/customerManage/service"
    5. "go_code/customerManage/model"
    6. )
    7. type customerView struct {
    8. //定义必要的字段
    9. key string //接收用户输入
    10. loop bool //表示是否循环显示主菜单
    11. //增加一个customerService
    12. customerService *service.CustomerService
    13. }
    14. //显示所有的客户信息
    15. func (this *customerView) list() {
    16. //获取到当前所有客户信息(在切片中)
    17. customers := this.customerService.List()
    18. //显示
    19. fmt.Println("----------------客户列表------------------")
    20. fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
    21. for i := 0; i < len(customers); i++ {
    22. fmt.Println(customers[i].GetInfo())
    23. }
    24. fmt.Println("----------------客户列表完成------------------")
    25. }
    26. //添加客户信息:构建新的客户,并完成添加
    27. func (this *customerView) add() {
    28. fmt.Println("----------------添加客户------------------")
    29. fmt.Println("姓名:")
    30. name := ""
    31. fmt.Scanln(&name)
    32. fmt.Println("性别:")
    33. gender := ""
    34. fmt.Scanln(&gender)
    35. fmt.Println("年龄:")
    36. age := 0
    37. fmt.Scanln(&age)
    38. fmt.Println("电话:")
    39. phone := ""
    40. fmt.Scanln(&phone)
    41. fmt.Println("邮箱:")
    42. email := ""
    43. fmt.Scanln(&email)
    44. //构建一个新的Customer实例
    45. //注意:id号是系统分配的
    46. customer := model.NewCustomer2(name, gender, age, phone, email)
    47. //调用
    48. if this.customerService.Add(customer) {
    49. fmt.Println("----------------添加成功------------------")
    50. } else {
    51. fmt.Println("----------------添加失败------------------")
    52. }
    53. }
    54. //得到用户输入的id,删除该id对应的客户
    55. func (this *customerView) delete() {
    56. fmt.Println("----------------删除客户------------------")
    57. fmt.Println("客户id(-1退出):")
    58. id := -1
    59. fmt.Scanln(&id)
    60. if id == -1 {
    61. return // 放弃删除
    62. }
    63. fmt.Println("你确定要退出吗? y/n")
    64. choice := ""
    65. for {
    66. fmt.Scanln(&choice)
    67. if choice == "y" || choice == "n" {
    68. break
    69. }
    70. fmt.Println("你输入有误,请重新输入 y/n")
    71. }
    72. //调用customerService.Delete
    73. if this.customerService.Delete(id) {
    74. fmt.Println("------------------删除成功------------------")
    75. } else {
    76. fmt.Println("------------------删除失败------------------")
    77. }
    78. }
    79. //显示主菜单
    80. func (this *customerView) mainMenu() {
    81. for {
    82. fmt.Println("\n----------------客户信息管理软件------------------")
    83. fmt.Println(" 1.添加客户")
    84. fmt.Println(" 2.修改客户")
    85. fmt.Println(" 3.删除客户")
    86. fmt.Println(" 4.客户列表")
    87. fmt.Println(" 5.退 出")
    88. fmt.Println("请选择(1~5):")
    89. fmt.Scanln(&this.key)
    90. switch this.key {
    91. case "1":
    92. this.add()
    93. case "2":
    94. fmt.Println("修改客户")
    95. case "3":
    96. this.delete()
    97. case "4":
    98. this.list()
    99. case "5":
    100. this.loop = false
    101. default :
    102. fmt.Println("输入有误,请重新输入")
    103. }
    104. if !this.loop {
    105. break
    106. }
    107. }
    108. fmt.Println("已退出软件的使用")
    109. }
    110. func main() {
    111. //在主函数中创建一个CustomerView实例,并运行主菜单
    112. customerView := customerView {
    113. key : "",
    114. loop : true,
    115. }
    116. //完成对customerView结构体的customerService字段初始化
    117. customerView.customerService = service.NewCustomerService()
    118. //显示主菜单
    119. customerView.mainMenu()
    120. }

    5.完善退出确认功能

    功能说明

            要求用户在退出时提示"确认是否退出(Y/N)",用户必须输入Y/N,否则循环提示

    思路分析

            需要编写customerView

    customerView.go

    1. package main
    2. import (
    3. "fmt"
    4. "go_code/chapter12/customerManage/service"
    5. "go_code/chapter12/customerManage/model"
    6. )
    7. type customerView struct {
    8. //定义必要的字段
    9. key string //接收用户输入
    10. loop bool //表示是否循环显示主菜单
    11. //增加一个customerService
    12. customerService *service.CustomerService
    13. }
    14. //显示所有的客户信息
    15. func (this *customerView) list() {
    16. //获取到当前所有客户信息(在切片中)
    17. customers := this.customerService.List()
    18. //显示
    19. fmt.Println("----------------客户列表------------------")
    20. fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
    21. for i := 0; i < len(customers); i++ {
    22. fmt.Println(customers[i].GetInfo())
    23. }
    24. fmt.Println("----------------客户列表完成------------------")
    25. }
    26. //添加客户信息:构建新的客户,并完成添加
    27. func (this *customerView) add() {
    28. fmt.Println("----------------添加客户------------------")
    29. fmt.Println("姓名:")
    30. name := ""
    31. fmt.Scanln(&name)
    32. fmt.Println("性别:")
    33. gender := ""
    34. fmt.Scanln(&gender)
    35. fmt.Println("年龄:")
    36. age := 0
    37. fmt.Scanln(&age)
    38. fmt.Println("电话:")
    39. phone := ""
    40. fmt.Scanln(&phone)
    41. fmt.Println("邮箱:")
    42. email := ""
    43. fmt.Scanln(&email)
    44. //构建一个新的Customer实例
    45. //注意:id号是系统分配的
    46. customer := model.NewCustomer2(name, gender, age, phone, email)
    47. //调用
    48. if this.customerService.Add(customer) {
    49. fmt.Println("----------------添加成功------------------")
    50. } else {
    51. fmt.Println("----------------添加失败------------------")
    52. }
    53. }
    54. //得到用户输入的id,删除该id对应的客户
    55. func (this *customerView) delete() {
    56. fmt.Println("----------------删除客户------------------")
    57. fmt.Println("客户id(-1退出):")
    58. id := -1
    59. fmt.Scanln(&id)
    60. if id == -1 {
    61. return // 放弃删除
    62. }
    63. fmt.Println("你确定要删除吗? y/n")
    64. choice := ""
    65. for {
    66. fmt.Scanln(&choice)
    67. if choice == "y" || choice == "n" {
    68. break
    69. }
    70. fmt.Println("你输入有误,请重新输入 y/n")
    71. }
    72. //调用customerService.Delete
    73. if this.customerService.Delete(id) {
    74. fmt.Println("------------------删除成功------------------")
    75. } else {
    76. fmt.Println("------------------删除失败------------------")
    77. }
    78. }
    79. //退出
    80. func (this *customerView) exit() {
    81. fmt.Println("你确定要退出吗? y/n")
    82. fmt.Scanln(&this.key)
    83. for {
    84. if this.key == "y" || this.key == "Y" || this.key == "n" || this.key == "N" {
    85. break
    86. }
    87. fmt.Println("你输入有误,请重新输入y/n")
    88. }
    89. if this.key == "y" || this.key == "Y" {
    90. this.loop = false
    91. }
    92. }
    93. //显示主菜单
    94. func (this *customerView) mainMenu() {
    95. for {
    96. fmt.Println("\n----------------客户信息管理软件------------------")
    97. fmt.Println(" 1.添加客户")
    98. fmt.Println(" 2.修改客户")
    99. fmt.Println(" 3.删除客户")
    100. fmt.Println(" 4.客户列表")
    101. fmt.Println(" 5.退 出")
    102. fmt.Println("请选择(1~5):")
    103. fmt.Scanln(&this.key)
    104. switch this.key {
    105. case "1":
    106. this.add()
    107. case "2":
    108. fmt.Println("修改客户")
    109. case "3":
    110. this.delete()
    111. case "4":
    112. this.list()
    113. case "5":
    114. this.exit()
    115. default :
    116. fmt.Println("输入有误,请重新输入")
    117. }
    118. if !this.loop {
    119. break
    120. }
    121. }
    122. fmt.Println("已退出软件的使用")
    123. }
    124. func main() {
    125. //在主函数中创建一个CustomerView实例,并运行主菜单
    126. customerView := customerView {
    127. key : "",
    128. loop : true,
    129. }
    130. //完成对customerView结构体的customerService字段初始化
    131. customerView.customerService = service.NewCustomerService()
    132. //显示主菜单
    133. customerView.mainMenu()
    134. }

     6.完成修改客户的功能

    功能说明

    思路分析

            需要编写customerView.go和customerService.go

    customerView.go

    1. package main
    2. import (
    3. "fmt"
    4. "go_code/customerManage/service"
    5. "go_code/customerManage/model"
    6. )
    7. type customerView struct {
    8. //定义必要的字段
    9. key string //接收用户输入
    10. loop bool //表示是否循环显示主菜单
    11. //增加一个customerService
    12. customerService *service.CustomerService
    13. }
    14. //显示所有的客户信息
    15. func (this *customerView) list() {
    16. //获取到当前所有客户信息(在切片中)
    17. customers := this.customerService.List()
    18. //显示
    19. fmt.Println("----------------客户列表------------------")
    20. fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
    21. for i := 0; i < len(customers); i++ {
    22. fmt.Println(customers[i].GetInfo())
    23. }
    24. fmt.Println("----------------客户列表完成------------------")
    25. }
    26. //添加客户信息:构建新的客户,并完成添加
    27. func (this *customerView) add() {
    28. fmt.Println("----------------添加客户------------------")
    29. fmt.Println("姓名:")
    30. name := ""
    31. fmt.Scanln(&name)
    32. fmt.Println("性别:")
    33. gender := ""
    34. fmt.Scanln(&gender)
    35. fmt.Println("年龄:")
    36. age := 0
    37. fmt.Scanln(&age)
    38. fmt.Println("电话:")
    39. phone := ""
    40. fmt.Scanln(&phone)
    41. fmt.Println("邮箱:")
    42. email := ""
    43. fmt.Scanln(&email)
    44. //构建一个新的Customer实例
    45. //注意:id号是系统分配的
    46. customer := model.NewCustomer2(name, gender, age, phone, email)
    47. //调用
    48. if this.customerService.Add(customer) {
    49. fmt.Println("----------------添加成功------------------")
    50. } else {
    51. fmt.Println("----------------添加失败------------------")
    52. }
    53. }
    54. //得到用户输入的id,删除该id对应的客户
    55. func (this *customerView) delete() {
    56. fmt.Println("----------------删除客户------------------")
    57. fmt.Println("客户id(-1退出):")
    58. id := -1
    59. fmt.Scanln(&id)
    60. if id == -1 {
    61. return // 放弃删除
    62. }
    63. fmt.Println("姓名:")
    64. name := ""
    65. fmt.Scanln(&name)
    66. fmt.Println("性别:")
    67. gender := ""
    68. fmt.Scanln(&gender)
    69. fmt.Println("年龄:")
    70. age := 0
    71. fmt.Scanln(&age)
    72. fmt.Println("电话:")
    73. phone := ""
    74. fmt.Scanln(&phone)
    75. fmt.Println("邮箱:")
    76. email := ""
    77. fmt.Scanln(&email)
    78. fmt.Println("你确定要删除吗? y/n")
    79. choice := ""
    80. for {
    81. fmt.Scanln(&choice)
    82. if choice == "y" || choice == "n" {
    83. break
    84. }
    85. fmt.Println("你输入有误,请重新输入 y/n")
    86. }
    87. //调用customerService.Delete
    88. if this.customerService.Delete(id) {
    89. fmt.Println("------------------删除成功------------------")
    90. } else {
    91. fmt.Println("------------------删除失败------------------")
    92. }
    93. }
    94. //得到用户输入的id,修改该id对应的客户
    95. func (this *customerView) update() {
    96. fmt.Println("----------------修改客户------------------")
    97. fmt.Println("客户id(-1退出):")
    98. id := -1
    99. fmt.Scanln(&id)
    100. if id == -1 {
    101. return // 放弃修改
    102. }
    103. //获取要修改的数据,并显示
    104. index := this.customerService.FindById(id)
    105. if index == -1 {
    106. fmt.Println("------------------客户id不存在------------------")
    107. return
    108. }
    109. fmt.Println("姓名:")
    110. name := ""
    111. fmt.Scanln(&name)
    112. fmt.Println("性别:")
    113. gender := ""
    114. fmt.Scanln(&gender)
    115. fmt.Println("年龄:")
    116. age := 0
    117. fmt.Scanln(&age)
    118. fmt.Println("电话:")
    119. phone := ""
    120. fmt.Scanln(&phone)
    121. fmt.Println("邮箱:")
    122. email := ""
    123. fmt.Scanln(&email)
    124. fmt.Println("你确定要修改吗? y/n")
    125. choice := ""
    126. for {
    127. fmt.Scanln(&choice)
    128. if choice == "y" || choice == "n" {
    129. break
    130. }
    131. fmt.Println("你输入有误,请重新输入 y/n")
    132. }
    133. customer := model.NewCustomer2(name, gender, age, phone, email)
    134. //调用customerService.Update
    135. if this.customerService.Update(index, customer) {
    136. fmt.Println("------------------修改成功------------------")
    137. } else {
    138. fmt.Println("------------------修改失败------------------")
    139. }
    140. }
    141. //退出
    142. func (this *customerView) exit() {
    143. fmt.Println("你确定要退出吗? y/n")
    144. fmt.Scanln(&this.key)
    145. for {
    146. if this.key == "y" || this.key == "Y" || this.key == "n" || this.key == "N" {
    147. break
    148. }
    149. fmt.Println("你输入有误,请重新输入y/n")
    150. }
    151. if this.key == "y" || this.key == "Y" {
    152. this.loop = false
    153. }
    154. }
    155. //显示主菜单
    156. func (this *customerView) mainMenu() {
    157. for {
    158. fmt.Println("\n----------------客户信息管理软件------------------")
    159. fmt.Println(" 1.添加客户")
    160. fmt.Println(" 2.修改客户")
    161. fmt.Println(" 3.删除客户")
    162. fmt.Println(" 4.客户列表")
    163. fmt.Println(" 5.退 出")
    164. fmt.Println("请选择(1~5):")
    165. fmt.Scanln(&this.key)
    166. switch this.key {
    167. case "1":
    168. this.add()
    169. case "2":
    170. this.update()
    171. case "3":
    172. this.delete()
    173. case "4":
    174. this.list()
    175. case "5":
    176. this.exit()
    177. default :
    178. fmt.Println("输入有误,请重新输入")
    179. }
    180. if !this.loop {
    181. break
    182. }
    183. }
    184. fmt.Println("已退出软件的使用")
    185. }
    186. func main() {
    187. //在主函数中创建一个CustomerView实例,并运行主菜单
    188. customerView := customerView {
    189. key : "",
    190. loop : true,
    191. }
    192. //完成对customerView结构体的customerService字段初始化
    193. customerView.customerService = service.NewCustomerService()
    194. //显示主菜单
    195. customerView.mainMenu()
    196. }

     customerService.go

    1. package service
    2. import (
    3. "go_code/chapter12/customerManage/model"
    4. )
    5. //声明一个customerServive结构体,完成对Customer的操作,包括
    6. //增删改查
    7. type CustomerService struct {
    8. customers []model.Customer
    9. //声明一个字段,表示当前切片含有多少个用户
    10. //该字段后面还可以做为新客户的id+1
    11. customerNum int
    12. }
    13. //编写一个方法,可以返回*CustomerService实例
    14. func NewCustomerService() *CustomerService {
    15. //为了能够看到客户在切片中,初始化一个客户
    16. customerService := &CustomerService{}
    17. customerService.customerNum = 1
    18. customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
    19. //把customer切片追加到customers切片中
    20. customerService.customers = append(customerService.customers, customer)
    21. return customerService
    22. }
    23. //返回客户切片
    24. func (this *CustomerService) List() []model.Customer {
    25. return this.customers
    26. }
    27. //添加一个客户到切片
    28. func (this *CustomerService) Add(customer model.Customer) bool {
    29. //确定一个id的规则
    30. this.customerNum++
    31. customer.Id = this.customerNum
    32. //把customer切片追加到customers切片中
    33. this.customers = append(this.customers, customer)
    34. //判断
    35. return true
    36. }
    37. //根据id修改客户(从切片中修改)
    38. func (this *CustomerService) Update(index int, customer model.Customer) bool {
    39. //从切片中修改客户
    40. this.customers[index].Name= customer.Name
    41. this.customers[index].Age= customer.Age
    42. this.customers[index].Gender= customer.Gender
    43. this.customers[index].Phone= customer.Phone
    44. this.customers[index].Email= customer.Email
    45. return true
    46. }
    47. //根据id删除客户(从切片中删除)
    48. func (this *CustomerService) Delete(id int) bool {
    49. index := this.FindById(id)
    50. if index == -1 {
    51. return false
    52. }
    53. //从切片中删除客户
    54. this.customers = append(this.customers[:index], this.customers[index + 1:]...)
    55. return true
    56. }
    57. //根据id查找客户在切片中的下标,如果没有,返回-1
    58. func (this *CustomerService) FindById(id int) int {
    59. index := -1
    60. //遍历this.customers
    61. for i := 0; i < len(this.customers); i++ {
    62. if this.customers[i].Id == id {
    63. //找到
    64. return i
    65. }
    66. }
    67. return index
    68. }

    [上一节][go学习笔记.第十一章.项目案例] 1.家庭收支记账软件项目

    [下一节][go学习笔记.第十二章.文件操作] 1.文件的基本介绍以及基本操作 

  • 相关阅读:
    识别评估项目风险常用6大方法
    java在cmd中乱码的问题解决
    获取阿里云Docker镜像加速器
    [Linux入门]---Linux项目自动化构建工具-make/Makefile
    Spring(十三)- Spring 配置类的注解
    [附源码]java毕业设计中达小区物业管理系统
    小小逻辑判断符的错误使用,资损几万块
    Ubuntu安装postgresql并连接navicat
    基于ssm教学评价管理系统获取(java毕业设计)
    博途PLC 1200/1500PLC开放式以太网通信TSEND_C通信
  • 原文地址:https://blog.csdn.net/zhoupenghui168/article/details/127717459