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





功能说明
当用户运行程序时,可以看到主菜单,当输入 5 时,可以退出该软件
思路分析
编写customerView.go,另外可以把customer.go和customerService.go写出来
- package model
-
- import (
- "fmt"
- )
-
- // 声明一个customer结构体,表示一个客户信息
- type Customer struct {
- Id int
- Name string
- Gender string
- Age int
- Phone string
- Email string
- }
-
- //使用工厂模式,返回一个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,
- }
- }
- package service
-
- import (
- "go_code/customerManage/model"
- )
-
- //声明一个customerServive结构体,完成对Customer的操作,包括
- //增删改查
- type CustomerService struct {
- customers []model.Customer
- //声明一个字段,表示当前切片含有多少个用户
- //该字段后面还可以做为新客户的id+1
- customerNum int
- }
- package main
-
- import (
- "fmt"
- )
-
- type customerView struct {
- //定义必要的字段
- key string //接收用户输入
- loop bool //表示是否循环显示主菜单
- }
-
- //显示主菜单
- func (this *customerView) mainMenu() {
- for {
- fmt.Println("\n----------------客户信息管理软件------------------")
- fmt.Println(" 1.添加客户")
- fmt.Println(" 2.修改客户")
- fmt.Println(" 3.删除客户")
- fmt.Println(" 4.客户列表")
- fmt.Println(" 5.退 出")
- fmt.Println("请选择(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()
- }
功能说明
思路分析: 见 二.程序框架图
- package model
-
- import (
- "fmt"
- )
-
- // 声明一个customer结构体,表示一个客户信息
- type Customer struct {
- Id int
- Name string
- Gender string
- Age int
- Phone string
- Email string
- }
-
- //使用工厂模式,返回一个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,
- }
- }
-
- //返回用户信息
- 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
- }
- package service
-
- import (
- "go_code/chapter12/customerManage/model"
- )
-
- //声明一个customerServive结构体,完成对Customer的操作,包括
- //增删改查
- type CustomerService struct {
- customers []model.Customer
- //声明一个字段,表示当前切片含有多少个用户
- //该字段后面还可以做为新客户的id+1
- customerNum int
- }
-
- //编写一个方法,可以返回*CustomerService实例
- func NewCustomerService() *CustomerService {
- //为了能够看到客户在切片中,初始化一个客户
- customerService := &CustomerService{}
- customerService.customerNum = 1
- customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
- //把customer切片追加到customers切片中
- customerService.customers = append(customerService.customers, customer)
- return customerService
- }
-
- //返回客户切片
- func (this *CustomerService) List() []model.Customer {
- return this.customers
- }
- package main
-
- import (
- "fmt"
- "go_code/customerManage/service"
- )
-
- type customerView struct {
- //定义必要的字段
- key string //接收用户输入
- loop bool //表示是否循环显示主菜单
- //增加一个customerService
- customerService *service.CustomerService
- }
-
- //显示所有的客户信息
- func (this *customerView) list() {
- //获取到当前所有客户信息(在切片中)
- customers := this.customerService.List()
- //显示
- fmt.Println("\n----------------客户列表------------------\n")
- fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
- for i := 0; i < len(customers); i++ {
- fmt.Println(customers[i].GetInfo())
- }
- fmt.Println("----------------客户列表完成------------------")
- }
-
- //显示主菜单
- func (this *customerView) mainMenu() {
- for {
- fmt.Println("\n----------------客户信息管理软件------------------")
- fmt.Println(" 1.添加客户")
- fmt.Println(" 2.修改客户")
- fmt.Println(" 3.删除客户")
- fmt.Println(" 4.客户列表")
- fmt.Println(" 5.退 出")
- fmt.Println("请选择(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结构体的customerService字段初始化
- customerView.customerService = service.NewCustomerService()
- //显示主菜单
- customerView.mainMenu()
- }
功能说明
思路分析
见 二.程序框架图,需要编写customerView和customerService,customer类型,规定:新添加的成员的id就是第几个添加的id
- package model
-
- import (
- "fmt"
- )
-
- // 声明一个customer结构体,表示一个客户信息
- type Customer struct {
- Id int
- Name string
- Gender string
- Age int
- Phone string
- Email string
- }
-
- //使用工厂模式,返回一个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,
- }
- }
-
- //第二种:不带id创建: 使用工厂模式,返回一个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
- }
- package service
-
- import (
- "go_code/chapter12/customerManage/model"
- )
-
- //声明一个customerServive结构体,完成对Customer的操作,包括
- //增删改查
- type CustomerService struct {
- customers []model.Customer
- //声明一个字段,表示当前切片含有多少个用户
- //该字段后面还可以做为新客户的id+1
- customerNum int
- }
-
- //编写一个方法,可以返回*CustomerService实例
- func NewCustomerService() *CustomerService {
- //为了能够看到客户在切片中,初始化一个客户
- customerService := &CustomerService{}
- customerService.customerNum = 1
- customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
- //把customer切片追加到customers切片中
- 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
- //把customer切片追加到customers切片中
- this.customers = append(this.customers, customer)
- //判断
- return true
- }
- package main
-
- import (
- "fmt"
- "go_code/chapter12/customerManage/service"
- "go_code/chapter12/customerManage/model"
- )
-
- type customerView struct {
- //定义必要的字段
- key string //接收用户输入
- loop bool //表示是否循环显示主菜单
- //增加一个customerService
- customerService *service.CustomerService
- }
-
- //显示所有的客户信息
- func (this *customerView) list() {
- //获取到当前所有客户信息(在切片中)
- customers := this.customerService.List()
- //显示
- fmt.Println("----------------客户列表------------------")
- fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
- for i := 0; i < len(customers); i++ {
- fmt.Println(customers[i].GetInfo())
- }
- fmt.Println("----------------客户列表完成------------------")
- }
-
- //添加客户信息:构建新的客户,并完成添加
- func (this *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)
-
- //构建一个新的Customer实例
- //注意:id号是系统分配的
- 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("\n----------------客户信息管理软件------------------")
- fmt.Println(" 1.添加客户")
- fmt.Println(" 2.修改客户")
- fmt.Println(" 3.删除客户")
- fmt.Println(" 4.客户列表")
- fmt.Println(" 5.退 出")
- fmt.Println("请选择(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结构体的customerService字段初始化
- customerView.customerService = service.NewCustomerService()
- //显示主菜单
- customerView.mainMenu()
- }
功能说明
思路分析
见 二.程序框架图
- package service
-
- import (
- "go_code/chapter12/customerManage/model"
- )
-
- //声明一个customerServive结构体,完成对Customer的操作,包括
- //增删改查
- type CustomerService struct {
- customers []model.Customer
- //声明一个字段,表示当前切片含有多少个用户
- //该字段后面还可以做为新客户的id+1
- customerNum int
- }
-
- //编写一个方法,可以返回*CustomerService实例
- func NewCustomerService() *CustomerService {
- //为了能够看到客户在切片中,初始化一个客户
- customerService := &CustomerService{}
- customerService.customerNum = 1
- customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
- //把customer切片追加到customers切片中
- 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
- //把customer切片追加到customers切片中
- this.customers = append(this.customers, customer)
- //判断
- return true
- }
-
- //根据id删除客户(从切片中删除)
- func (this *CustomerService) Delete(id int) bool {
- index := this.FindById(id)
- if index == -1 {
- return false
- }
- //从切片中删除客户
- this.customers = append(this.customers[:index], this.customers[index + 1:]...)
- return true
- }
-
- //根据id查找客户在切片中的下标,如果没有,返回-1
- func (this *CustomerService) FindById(id int) int {
- index := -1
- //遍历this.customers
- for i := 0; i < len(this.customers); i++ {
- if this.customers[i].Id == id {
- //找到
- return i
- }
- }
-
- return index
- }
- 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() {
- //获取到当前所有客户信息(在切片中)
- customers := this.customerService.List()
- //显示
- fmt.Println("----------------客户列表------------------")
- fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
- for i := 0; i < len(customers); i++ {
- fmt.Println(customers[i].GetInfo())
- }
- fmt.Println("----------------客户列表完成------------------")
- }
-
- //添加客户信息:构建新的客户,并完成添加
- func (this *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)
-
- //构建一个新的Customer实例
- //注意:id号是系统分配的
- customer := model.NewCustomer2(name, gender, age, phone, email)
- //调用
- if this.customerService.Add(customer) {
- fmt.Println("----------------添加成功------------------")
- } else {
- fmt.Println("----------------添加失败------------------")
- }
- }
- //得到用户输入的id,删除该id对应的客户
- func (this *customerView) delete() {
- fmt.Println("----------------删除客户------------------")
- fmt.Println("客户id(-1退出):")
- id := -1
- fmt.Scanln(&id)
- if id == -1 {
- return // 放弃删除
- }
-
- fmt.Println("你确定要退出吗? y/n")
- choice := ""
- for {
- fmt.Scanln(&choice)
- if choice == "y" || choice == "n" {
- break
- }
- fmt.Println("你输入有误,请重新输入 y/n")
- }
-
- //调用customerService.Delete
- if this.customerService.Delete(id) {
- fmt.Println("------------------删除成功------------------")
- } else {
- fmt.Println("------------------删除失败------------------")
- }
-
-
- }
- //显示主菜单
- func (this *customerView) mainMenu() {
- for {
- fmt.Println("\n----------------客户信息管理软件------------------")
- fmt.Println(" 1.添加客户")
- fmt.Println(" 2.修改客户")
- fmt.Println(" 3.删除客户")
- fmt.Println(" 4.客户列表")
- fmt.Println(" 5.退 出")
- fmt.Println("请选择(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("已退出软件的使用")
- }
-
- func main() {
- //在主函数中创建一个CustomerView实例,并运行主菜单
- customerView := customerView {
- key : "",
- loop : true,
- }
- //完成对customerView结构体的customerService字段初始化
- customerView.customerService = service.NewCustomerService()
- //显示主菜单
- customerView.mainMenu()
- }
功能说明
要求用户在退出时提示"确认是否退出(Y/N)",用户必须输入Y/N,否则循环提示
思路分析
需要编写customerView
- package main
-
- import (
- "fmt"
- "go_code/chapter12/customerManage/service"
- "go_code/chapter12/customerManage/model"
- )
-
- type customerView struct {
- //定义必要的字段
- key string //接收用户输入
- loop bool //表示是否循环显示主菜单
- //增加一个customerService
- customerService *service.CustomerService
- }
-
- //显示所有的客户信息
- func (this *customerView) list() {
- //获取到当前所有客户信息(在切片中)
- customers := this.customerService.List()
- //显示
- fmt.Println("----------------客户列表------------------")
- fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
- for i := 0; i < len(customers); i++ {
- fmt.Println(customers[i].GetInfo())
- }
- fmt.Println("----------------客户列表完成------------------")
- }
-
- //添加客户信息:构建新的客户,并完成添加
- func (this *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)
-
- //构建一个新的Customer实例
- //注意:id号是系统分配的
- customer := model.NewCustomer2(name, gender, age, phone, email)
- //调用
- if this.customerService.Add(customer) {
- fmt.Println("----------------添加成功------------------")
- } else {
- fmt.Println("----------------添加失败------------------")
- }
- }
- //得到用户输入的id,删除该id对应的客户
- func (this *customerView) delete() {
- fmt.Println("----------------删除客户------------------")
- fmt.Println("客户id(-1退出):")
- id := -1
- fmt.Scanln(&id)
- if id == -1 {
- return // 放弃删除
- }
-
- fmt.Println("你确定要删除吗? y/n")
- choice := ""
- for {
- fmt.Scanln(&choice)
- if choice == "y" || choice == "n" {
- break
- }
- fmt.Println("你输入有误,请重新输入 y/n")
- }
-
- //调用customerService.Delete
- if this.customerService.Delete(id) {
- fmt.Println("------------------删除成功------------------")
- } else {
- fmt.Println("------------------删除失败------------------")
- }
- }
-
- //退出
- func (this *customerView) exit() {
- fmt.Println("你确定要退出吗? y/n")
- fmt.Scanln(&this.key)
- for {
- 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
- }
- }
-
- //显示主菜单
- func (this *customerView) mainMenu() {
- for {
- fmt.Println("\n----------------客户信息管理软件------------------")
- fmt.Println(" 1.添加客户")
- fmt.Println(" 2.修改客户")
- fmt.Println(" 3.删除客户")
- fmt.Println(" 4.客户列表")
- fmt.Println(" 5.退 出")
- fmt.Println("请选择(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.exit()
- default :
- fmt.Println("输入有误,请重新输入")
- }
-
- if !this.loop {
- break
- }
- }
- fmt.Println("已退出软件的使用")
- }
-
- func main() {
- //在主函数中创建一个CustomerView实例,并运行主菜单
- customerView := customerView {
- key : "",
- loop : true,
- }
- //完成对customerView结构体的customerService字段初始化
- customerView.customerService = service.NewCustomerService()
- //显示主菜单
- customerView.mainMenu()
- }
功能说明
思路分析
需要编写customerView.go和customerService.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() {
- //获取到当前所有客户信息(在切片中)
- customers := this.customerService.List()
- //显示
- fmt.Println("----------------客户列表------------------")
- fmt.Println("\n编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱")
- for i := 0; i < len(customers); i++ {
- fmt.Println(customers[i].GetInfo())
- }
- fmt.Println("----------------客户列表完成------------------")
- }
-
- //添加客户信息:构建新的客户,并完成添加
- func (this *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)
-
- //构建一个新的Customer实例
- //注意:id号是系统分配的
- customer := model.NewCustomer2(name, gender, age, phone, email)
- //调用
- if this.customerService.Add(customer) {
- fmt.Println("----------------添加成功------------------")
- } else {
- fmt.Println("----------------添加失败------------------")
- }
- }
-
- //得到用户输入的id,删除该id对应的客户
- func (this *customerView) delete() {
- fmt.Println("----------------删除客户------------------")
- fmt.Println("客户id(-1退出):")
- id := -1
- fmt.Scanln(&id)
- if id == -1 {
- return // 放弃删除
- }
- 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)
-
- fmt.Println("你确定要删除吗? y/n")
- choice := ""
- for {
- fmt.Scanln(&choice)
- if choice == "y" || choice == "n" {
- break
- }
- fmt.Println("你输入有误,请重新输入 y/n")
- }
-
- //调用customerService.Delete
- if this.customerService.Delete(id) {
- fmt.Println("------------------删除成功------------------")
- } else {
- fmt.Println("------------------删除失败------------------")
- }
- }
-
- //得到用户输入的id,修改该id对应的客户
- func (this *customerView) update() {
- fmt.Println("----------------修改客户------------------")
- fmt.Println("客户id(-1退出):")
- id := -1
- fmt.Scanln(&id)
- if id == -1 {
- return // 放弃修改
- }
-
- //获取要修改的数据,并显示
- index := this.customerService.FindById(id)
- if index == -1 {
- fmt.Println("------------------客户id不存在------------------")
- return
- }
- 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)
-
- fmt.Println("你确定要修改吗? y/n")
- choice := ""
- for {
- fmt.Scanln(&choice)
- if choice == "y" || choice == "n" {
- break
- }
- fmt.Println("你输入有误,请重新输入 y/n")
- }
-
- customer := model.NewCustomer2(name, gender, age, phone, email)
-
- //调用customerService.Update
- if this.customerService.Update(index, customer) {
- fmt.Println("------------------修改成功------------------")
- } else {
- fmt.Println("------------------修改失败------------------")
- }
- }
-
- //退出
- func (this *customerView) exit() {
- fmt.Println("你确定要退出吗? y/n")
- fmt.Scanln(&this.key)
- for {
- 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
- }
- }
-
- //显示主菜单
- func (this *customerView) mainMenu() {
- for {
- fmt.Println("\n----------------客户信息管理软件------------------")
- fmt.Println(" 1.添加客户")
- fmt.Println(" 2.修改客户")
- fmt.Println(" 3.删除客户")
- fmt.Println(" 4.客户列表")
- fmt.Println(" 5.退 出")
- fmt.Println("请选择(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结构体的customerService字段初始化
- customerView.customerService = service.NewCustomerService()
- //显示主菜单
- customerView.mainMenu()
- }
- package service
-
- import (
- "go_code/chapter12/customerManage/model"
- )
-
- //声明一个customerServive结构体,完成对Customer的操作,包括
- //增删改查
- type CustomerService struct {
- customers []model.Customer
- //声明一个字段,表示当前切片含有多少个用户
- //该字段后面还可以做为新客户的id+1
- customerNum int
- }
-
- //编写一个方法,可以返回*CustomerService实例
- func NewCustomerService() *CustomerService {
- //为了能够看到客户在切片中,初始化一个客户
- customerService := &CustomerService{}
- customerService.customerNum = 1
- customer := model.NewCustomer(1, "customer1", "男", 12,"15555555555","15555555555@qq.com")
- //把customer切片追加到customers切片中
- 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
- //把customer切片追加到customers切片中
- this.customers = append(this.customers, customer)
- //判断
- return true
- }
-
- //根据id修改客户(从切片中修改)
- func (this *CustomerService) Update(index int, customer model.Customer) bool {
- //从切片中修改客户
- this.customers[index].Name= customer.Name
- this.customers[index].Age= customer.Age
- this.customers[index].Gender= customer.Gender
- this.customers[index].Phone= customer.Phone
- this.customers[index].Email= customer.Email
- return true
- }
-
- //根据id删除客户(从切片中删除)
- func (this *CustomerService) Delete(id int) bool {
- index := this.FindById(id)
- if index == -1 {
- return false
- }
- //从切片中删除客户
- this.customers = append(this.customers[:index], this.customers[index + 1:]...)
- return true
- }
-
- //根据id查找客户在切片中的下标,如果没有,返回-1
- func (this *CustomerService) FindById(id int) int {
- index := -1
- //遍历this.customers
- for i := 0; i < len(this.customers); i++ {
- if this.customers[i].Id == id {
- //找到
- return i
- }
- }
-
- return index
- }