下面是一个简要的客服系统,主要是演示分层计。。
model : 数据部份:
package model import "fmt" //声明一个结构体,表示一个客户信息 type Customer struct{ Id int Name string Gender string Age int Phone string Emaill string } 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, } } func NewCustomer2(name string, gender string, age int, phone string,email string) Customer{ return Customer{ Name : name, Gender : gender, Age : age, Phone:phone, } } 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.Emaill) return info }
控制层,这儿名为服务层 customerService 代码如下:
package model import "fmt" //声明一个结构体,表示一个客户信息 type Customer struct{ Id int Name string Gender string Age int Phone string Emaill string } 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, } } func NewCustomer2(name string, gender string, age int, phone string,email string) Customer{ return Customer{ Name : name, Gender : gender, Age : age, Phone:phone, } } 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.Emaill) return info }
最后是视图层:第一个版本没有加注释,以后有空成全功能后在认真写好注释
package main import ( "fmt" "awesomeProject/service" "awesomeProject/model" ) type customerView struct{ key string loop bool customerService *service.CustomerService } //显示用户例表 func(this *customerView) list(){ customers:=this.customerService.List() fmt.Println("——————————————————客户例表————————————————————————————") fmt.Println("编号 \t 姓名 \t \t性别 \t年龄 \t电话 \t邮箱") for i:=0; i < len(customers);i++{ fmt.Println(customers[i].GetInfo()) } fmt.Println("++++++++++++++客户列表完成+++++++++++++++") } //jo fi add 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 :=model.NewCustomer2(name,gender,age, phone,email) this.customerService.Add(customer) } 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"{ 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("输入有误 ") }//switch循环结束 if !this.loop{ break }//for } fmt.Println("你退出了客户系统") }//主函数 func main(){ customerView := customerView{ key: "", loop : true, } customerView.customerService = service.NewcustomerService() customerView.mainMenu() }
原文地址:https://www.cnblogs.com/fgxwan/p/10111556.html
时间: 2024-10-12 07:31:53