zoukankan      html  css  js  c++  java
  • Go_客户信息管理系统

    13.1 项目需求

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

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

    13.2 项目界面设计

    主菜单:

    tONSN8.png

    添加客户页面:

    tONtUK.png

    修改客户页面:

    tONd8e.png

    删除客户页面:

    tONg58.png

    客户列表页面:

    tONHaV.png

    13.3 程序框架图

    tOUkGD.png

    曹操.PNG

    13.4 项目功能实现

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

    功能说明:

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

    思路分析:

    ​ 编写 customerView.go,另外可以把 customer.go和customerService.go写一部分

    代码实现

    tOo66H.png

    customer.go

    package model
    
    // 声明一个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,
    		}
    }
    

    customerSerice.go

    package service
    import (
    	"go_code/customerManager/model"
    )
    // 该CustomerSerice,完成对Customer的操作,包括增删改查
    type CustomerService struct {
    	customers []model.Customer
    	// 声明一个字段,表示当前切片含有多少个客户
    	// 该字段后面,还可以作为新客户的id+1
    	custerNum int
    }
    

    customerView.go

    package main
    import (
    	"fmt"
    )
    
    type customerView struct {
    	// 定义必要的字段
    	key string //接收用户输入。。。。
    	loop bool //表示是否循环显示主菜单
    }
    
    //显示主菜单
    func (this *customerView) mainMenu() {
    
    	for {
    		fmt.Println("----------客户信息管理软件----------")
    		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()
    }
    

    13.4.2 完成显示客户列表的功能

    功能说明

    tOTavQ.png

    代码实现(原先基础上做对比,新增了代码)

    customer.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,
    		}
    }
    
    // 返回用户的信息,格式化的字符串
    func (this Customer) GetInfo() string {
    	info := fmt.Sprintf("%v	%v	%v	%v	%v	%v	", 
    	this.Id, this.Name, this.Gender, this.Age, this.Phone, this.Email)
    	return info
    }
    

    customerSerice.go

    package service
    import (
    	"go_code/customerManager/model"
    )
    // 该CustomerSerice,完成对Customer的操作,
    // 包括增删改查
    type CustomerService struct {
    	customers []model.Customer
    	// 声明一个字段,表示当前切片含有多少个客户
    	// 该字段后面,还可以作为新客户的id+1
    	customerNum int
    }
    
    // 编写一个方法,可以返回 *CustomerSerice
    func NewCustomerSerice() *CustomerService {
    	// 为了那个看到有客户在切片中,我们初始化一个客户
    	customerService := &CustomerService{}
    	customerService.customerNum = 1
    	customer := model.NewCustomer(1, "张三", "男", 23, "11111", "zs@sh.com")
    	customerService.customers = append(customerService.customers, customer)
    	return customerService
    }
    
    // 返回客户切片
    func (this *CustomerService) List() []model.Customer {
    	return this.customers
    }
    

    customerView.go

    package main
    import (
    	"fmt"
    	"go_code/customerManager/service"
    )
    
    type customerView struct {
    	// 定义必要的字段
    	key string //接收用户输入。。。。
    	loop bool //表示是否循环显示主菜单
    	// 增加一个字段customerService
    	customerService *service.CustomerService
    }
    
    // 显示所有的客户信息
    func (this *customerView) list() {
    	// 首先,获取到当前所有的客户信息(切片中)
    	customers := this.customerService.List()
    	// 显示
    	fmt.Println("--------客户列表---------")
    	fmt.Println("编号	姓名	性别	年龄	电话	邮箱")
    	for i := 0; i < len(customers); i++ {
    		fmt.Println(customers[i].GetInfo())
    	}
    	fmt.Println("------客户列表完成--------")
    }
    
    // 显示主菜单
    func (this *customerView) mainMenu() {
    
    	for {
    		fmt.Println("----------客户信息管理软件----------")
    		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结构体的customerSerice字段的初始化
    	customerView.customerService = service.NewCustomerSerice()
    	customerView.mainMenu()
    }
    

    13.4.3 添加客户的功能

    customer.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,
    		}
    }
    // 第二种,创建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,
    	   }
    }
    
    // 返回用户的信息,格式化的字符串
    func (this Customer) GetInfo() string {
    	info := fmt.Sprintf("%v	%v	%v	%v	%v	%v	", 
    	this.Id, this.Name, this.Gender, this.Age, this.Phone, this.Email)
    	return info
    }
    

    customerSerice.go

    package service
    import (
    	"go_code/customerManager/model"
    )
    // 该CustomerSerice,完成对Customer的操作,
    // 包括增删改查
    type CustomerService struct {
    	customers []model.Customer
    	// 声明一个字段,表示当前切片含有多少个客户
    	// 该字段后面,还可以作为新客户的id+1
    	customerNum int
    }
    
    // 编写一个方法,可以返回 *CustomerSerice
    func NewCustomerSerice() *CustomerService {
    	// 为了那个看到有客户在切片中,我们初始化一个客户
    	customerService := &CustomerService{}
    	customerService.customerNum = 1
    	customer := model.NewCustomer(1, "张三", "男", 23, "11111", "zs@sh.com")
    	customerService.customers = append(customerService.customers, customer)
    	return customerService
    }
    
    // 返回客户切片
    func (this *CustomerService) List() []model.Customer {
    	return this.customers
    }
    
    // 添加客户到customer切片
    // !!!!!!!!!1
    func (this *CustomerService) Add(customer model.Customer) bool {
    	// 我们确定一个分配id的方法
    	this.customerNum++
    	customer.Id = this.customerNum
    	this.customers = append(this.customers, customer)
    	return true
    }
    

    customerView.go

    package main
    import (
    	"fmt"
    	"go_code/customerManager/service"
    	"go_code/customerManager/model"
    )
    
    type customerView struct {
    	// 定义必要的字段
    	key string //接收用户输入。。。。
    	loop bool //表示是否循环显示主菜单
    	// 增加一个字段customerService
    	customerService *service.CustomerService
    }
    
    // 显示所有的客户信息
    func (this *customerView) list() {
    	// 首先,获取到当前所有的客户信息(切片中)
    	customers := this.customerService.List()
    	// 显示
    	fmt.Println("--------客户列表---------")
    	fmt.Println("编号	姓名	性别	年龄	电话	邮箱")
    	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("----------客户信息管理软件----------")
    		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结构体的customerSerice字段的初始化
    	customerView.customerService = service.NewCustomerSerice()
    	customerView.mainMenu()
    }
    

    13.4.4 删除客户的功能

    customer.go 没有变化

    customerService.go

    package service
    import (
    	"go_code/customerManager/model"
    )
    // 该CustomerSerice,完成对Customer的操作,
    // 包括增删改查
    type CustomerService struct {
    	customers []model.Customer
    	// 声明一个字段,表示当前切片含有多少个客户
    	// 该字段后面,还可以作为新客户的id+1
    	customerNum int
    }
    
    // 编写一个方法,可以返回 *CustomerSerice
    func NewCustomerSerice() *CustomerService {
    	// 为了那个看到有客户在切片中,我们初始化一个客户
    	customerService := &CustomerService{}
    	customerService.customerNum = 1
    	customer := model.NewCustomer(1, "张三", "男", 23, "11111", "zs@sh.com")
    	customerService.customers = append(customerService.customers, customer)
    	return customerService
    }
    
    // 返回客户切片
    func (this *CustomerService) List() []model.Customer {
    	return this.customers
    }
    
    // 添加客户到customer切片
    // !!!!!!!!!1
    func (this *CustomerService) Add(customer model.Customer) bool {
    	// 我们确定一个分配id的方法
    	this.customerNum++
    	customer.Id = this.customerNum
    	this.customers = append(this.customers, customer)
    	return true
    }
    
    // 根据ID删除客户(从切片中删除)
    func (this *CustomerService) Delete(id int) bool {
    	index := this.FindById(id)
    	// 如果index == -1,说明没有这个客户
    	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 {
    			// 找到
    			index = i
    		}
    	}
    	return index
    }
    

    customerView.go

    package main
    import (
    	"fmt"
    	"go_code/customerManager/service"
    	"go_code/customerManager/model"
    )
    
    type customerView struct {
    	// 定义必要的字段
    	key string //接收用户输入。。。。
    	loop bool //表示是否循环显示主菜单
    	// 增加一个字段customerService
    	customerService *service.CustomerService
    }
    
    // 显示所有的客户信息
    func (this *customerView) list() {
    	// 首先,获取到当前所有的客户信息(切片中)
    	customers := this.customerService.List()
    	// 显示
    	fmt.Println("--------客户列表---------")
    	fmt.Println("编号	姓名	性别	年龄	电话	邮箱")
    	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("请选择待删除客户编号(-1退出):")
    	id := -1
    	fmt.Scanln(&id)
    	if id == -1 {
    		return  //放弃删除
    	}
    	fmt.Println("确认是否删除Y/N")
    	choice := ""
    	fmt.Scanln(&choice)
    	if choice == "y" || choice == "Y" {
    		// 调用customerService 的Delete方法
    	if this.customerService.Delete(id) {
    		fmt.Println("----------删除完成-----------")
    		} else {
    			fmt.Println("---------删除失败,输入的ID号不存在-------")
    		}
    	}	
    }
    
    // 显示主菜单
    func (this *customerView) mainMenu() {
    
    	for {
    		fmt.Println("----------客户信息管理软件----------")
    		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结构体的customerSerice字段的初始化
    	customerView.customerService = service.NewCustomerSerice()
    	customerView.mainMenu()
    }
    

    13.4.5 完善退出确认功能

    要求用户在退出时提示,“确认是否退出 Y/N”,用户必须输入“y/n”,否则循环提示

    customerView.go

    package main
    import (
    	"fmt"
    	"go_code/customerManager/service"
    	"go_code/customerManager/model"
    )
    
    type customerView struct {
    	// 定义必要的字段
    	key string //接收用户输入。。。。
    	loop bool //表示是否循环显示主菜单
    	// 增加一个字段customerService
    	customerService *service.CustomerService
    }
    
    // 显示所有的客户信息
    func (this *customerView) list() {
    	// 首先,获取到当前所有的客户信息(切片中)
    	customers := this.customerService.List()
    	// 显示
    	fmt.Println("--------客户列表---------")
    	fmt.Println("编号	姓名	性别	年龄	电话	邮箱")
    	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("请选择待删除客户编号(-1退出):")
    	id := -1
    	fmt.Scanln(&id)
    	if id == -1 {
    		return  //放弃删除
    	}
    	fmt.Println("确认是否删除Y/N")
    	choice := ""
    	fmt.Scanln(&choice)
    	if choice == "y" || choice == "Y" {
    		// 调用customerService 的Delete方法
    	if this.customerService.Delete(id) {
    		fmt.Println("----------删除完成-----------")
    		} else {
    			fmt.Println("---------删除失败,输入的ID号不存在-------")
    		}
    	}	
    }
    
    // 退出软件
    func (this *customerView) exit() {
    	fmt.Println("确认是否退出 Y/N:")
    	for {
    		fmt.Scanln(&this.key)
    		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("----------客户信息管理软件----------")
    		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结构体的customerSerice字段的初始化
    	customerView.customerService = service.NewCustomerSerice()
    	customerView.mainMenu()
    }
    
  • 相关阅读:
    runas/cpau/lsrunase使用小结(以管理员运行指定程序)
    XP安装IIS来加载aspx页面(Web调用SAP数据)
    HTML Select 标签选择后触发jQuery事件代码实例
    文件夹设置“以前的版本”功能(配置卷影副本)
    AD域-让共享目录只显示用户有权限访问的文件夹
    <Bootstrap> 学习笔记一. 配置环境, 简单使用, 响应式表格, 响应式图片
    <Bootstrap> 学习笔记二. 栅格系统的使用
    <jQuery> <方法> 十九. each()方法(遍历方法)
    <jQuery> <方法> 十八. 移除事件, 触发事件, 事件对象(阻止冒泡, 阻止跳转)
    <jQuery> <方法> 十七. on注册事件的两种方式(简单事件和委托事件)
  • 原文地址:https://www.cnblogs.com/jiaxiaozia/p/13109637.html
Copyright © 2011-2022 走看看