customerManage/model/customer.go
package model import ( "fmt" ) type Customer struct{ Id int Name string Gender string Age int Phone string Email string } //通过工厂模式,返回一个Customer的实例 ===>被NewCustomerService调用 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==>Add调用 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\t",this.Id,this.Name,this.Gender, this.Age,this.Phone,this.Email) return info }
customerManage/service/customerService.go
package service import ( "fmt" "oopproject/customerManage/model" "strconv" ) //增删改查 //定义一个结构体,记录不同的用户及登录用户的数量 type CustomerService struct{ customers []model.Customer customerNum int } //通过工程模式来创建创建 *CustomerService func NewCustomerService() *CustomerService{ customerService := &CustomerService{} customerService.customerNum = 1 customer :=model.NewCustomer(1,"张三","男",20,"112","zs@sohu.com") 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{ this.customerNum++ customer.Id = this.customerNum this.customers = append(this.customers,customer) return true } //根据id查找用户的在切片中的下标 func (this *CustomerService)FindById(id int)int{ index := -1 //遍历切片 for i:=0;i<len(this.customers);i++{ if this.customers[i].Id == id{ index = i } } return index } //从customers删除用户 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 } //从customers修改指定用户 func (this *CustomerService)Revise(id int)bool{ index := this.FindById(id) if index == -1{ return false } fmt.Printf("姓名(%v):<回车表示不做修改>\n",this.customers[index].Name) name := "" fmt.Scanln(&name) if name != ""{ this.customers[index].Name = name } fmt.Printf("性别(%v):<回车表示不做修改>\n",this.customers[index].Gender) gender := "" fmt.Scanln(&gender) if gender != ""{ //\r this.customers[index].Gender = gender }else{ } fmt.Printf("年龄(%v):<回车表示不做修改>\n",this.customers[index].Age) age := "" fmt.Scanln(&age) //func Atoi(s string) (i int, err error) if age != ""{ //\r this.customers[index].Age,_= strconv.Atoi(age) } fmt.Printf("电话(%v):<回车表示不做修改>\n",this.customers[index].Phone) phone := "" fmt.Scanln(&phone) if phone != ""{ //\r this.customers[index].Phone = phone } fmt.Printf("邮箱(%v):<回车表示不做修改>\n",this.customers[index].Email) email := "" fmt.Scanln(&email) if phone != ""{ //\r this.customers[index].Email = email } return true }
customerManage/view/customerView.go
package main import ( "fmt" "oopproject/customerManage/service" "oopproject/customerManage/model" ) //定义显示菜单 type customerView struct{ //定义必要字段 key string loop bool //获取服务器端 customerService *service.CustomerService } 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": this.revise() case "3": this.delete() case "4": this.list() case "5": this.exit() default: fmt.Println("你的输入有误,请重新输入...") } if !this.loop{ break } } fmt.Println("你退出了客户关系管理系统...") } //显示所有客户信息 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].GetInfo()) } fmt.Println("\n-----------------客户列表完成------------------\n\n") } //添加新用户 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) //创建一个model.Customer实例 customer := model.NewCustomer2(name,gender,age,phone,email) if this.customerService.Add(customer){ fmt.Println("------------------添加成功------------------") }else{ fmt.Println("------------------添加失败------------------") } } //删除用户 func (this *customerView)delete(){ fmt.Println("-------------------删除用户----------------") fmt.Println("请选择待删除客户的编号(-1)退出:") id := -1 fmt.Scanln(&id) if id == -1{ return //放弃删除操作 } fmt.Println("确定是否删除(Y/y):") choice := "" fmt.Scanln(&choice) if choice == "y" || choice == "Y"{ if this.customerService.Delete(id){ fmt.Println("------------------删除成功--------------") }else{ fmt.Println("------------------删除失败,用户不存在--------------") } } } //修改指定id的信息 func (this *customerView)revise(){ fmt.Println("------------------------修改用户------------------") fmt.Println("请选择待删除用户的编号(-1退出):") id := -1 fmt.Scanln(&id) if id == -1{ return //放弃删除操作 } //创建一个model.Customer实例 if this.customerService.Revise(id){ fmt.Println("------------------修改成功------------------") }else{ fmt.Println("------------------修改失败,用户不存在------------------") } } //退出程序 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 main(){ customerView := &customerView{ key : "", loop : true, } //初始化customerService customerView.customerService = service.NewCustomerService() //显示主菜单功能 customerView.mainMenu() }