软件由一下三个模块组成:
项目结构如下所示:
在 costumer.go 中,代码如下:
package modelimport ( "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实例方法,不带IDfunc NewCustomer2(name string,email string ) Customer { return Customer{ name : name,}}//返回用户的信息,格式化的字符串func (this Customer) GetInfo() string { info := fmt.Sprintf("%v\t %v\t %v\t %v\t %v\t %v\t",this.ID,this.name,this.Gender,this.Age,this.Phone,this.Email) return info }在 costumerService.go 中,代码如下:
package serviceimport ( "../model")//该CustomerService, 完成对Customer的 *** 作,包括//增删改查type CustomerService struct { customers []model.Customer //声明一个字段,表示当前切片含有多少个客户 //该字段后面,还可以作为新客户的ID+1 customerNum int}//编写一个方法,可以返回 *CustomerServicefunc NewCustomerService() *CustomerService { //为了能够看到有客户在切片中,我们初始化一个客户 customerService := &CustomerService{} customerService.customerNum = 1 customer := model.NewCustomer(1,"张三","男",20,"010-56253825","[email protected]") customerService.customers = append(customerService.customers,customer) return customerService}//返回客户切片func (this *CustomerService) List() []model.Customer { return this.customers}//添加客户到customers切片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查找客户在切片中对应下标,如果没有该客户,返回-1func (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}在 costumerVIEw.go 中,代码如下:
package mainimport ( "fmt" "../model" "../service")type customerVIEw struct { //定义必要字段 key string //接收用户输入... loop bool //表示是否循环的显示主菜单 //增加一个字段customerService customerService *service.CustomerService}//显示所有的客户信息func (this *customerVIEw) List() { //首先,获取到当前所有的客户信息(在切片中) customers := this.customerService.List() //显示 fmt.Println("---------------------------客户列表---------------------------") fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱") for i := 0; i < len(customers); i++ { //fmt.Println(customers[i].ID,"\t",customers[i].name...) fmt.Println(customers[i].GetInfo()) } fmt.Printf("\n-------------------------客户列表完成-------------------------\n\n")}//得到用户的输入,信息构建新的客户,并完成添加func (this *customerVIEw) add() { fmt.Println("---------------------添加客户---------------------") fmt.Print("姓名:") name := "" fmt.Scanln(&name) fmt.Print("性别:") gender := "" fmt.Scanln(&gender) fmt.Print("年龄:") age := 0 fmt.Scanln(&age) fmt.Print("电话:") phone := "" fmt.Scanln(&phone) fmt.Print("邮箱:") email := "" fmt.Scanln(&email) //构建一个新的Customer实例 //注意: ID号,没有让用户输入,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.Print("请选择待删除客户编号(-1退出):") ID := -1 fmt.Scanln(&ID) if ID == -1 { return //放弃删除 *** 作 } fmt.Println("确认是否删除(Y/N):") //这里同学们可以加入一个循环判断,直到用户输入 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.Print("确认是否退出(Y/N):") for { fmt.Scanln(&this.key) if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n" { break } fmt.Print("你的输入有误,确认是否退出(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.Print("请选择(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() { //在main函数中,创建一个customerVIEw,并运行显示主菜单.. customerVIEw := customerVIEw{ key : "",loop : true,} //这里完成对customerVIEw结构体的customerService字段的初始化 customerVIEw.customerService = service.NewCustomerService() //显示主菜单.. customerVIEw.mainMenu()}执行结果如下所示:
D:\code\demo\vIEw>go run customerVIEw.go
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):1
---------------------添加客户---------------------
姓名:李四
性别:男
年龄:22
电话:15611112222
邮箱:[email protected]
---------------------添加完成---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):4
---------------------------客户列表---------------------------
编号 姓名 性别 年龄 电话 邮箱
1 张三 男 20 010-56253825 [email protected]
2 李四 男 22 15611112222 [email protected]
-------------------------客户列表完成-------------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):
以上是内存溢出为你收集整理的示例:客户信息管理系统全部内容,希望文章能够帮你解决示例:客户信息管理系统所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)