golang示例项目 客户信息关系系统
1.需求分析
1)模拟实现基于文本界面的《客户信息管理软件》
2)该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表
2.项目界面设计
1)主菜单页面
---------客户信息管理软件--------
1.添加客户
2.修改客户
3.删除客户
4.客户列表
5.退出
请选择(1-5): _
2)添加客户页面
请选择(1-5): 1
---------添加客户---------
姓名:张三
性别:男
年龄:30
电话:010-56238762
邮箱:zhang@abc.com
---------添加完成---------
3)修改客户页面
请选择(1-5): 2
---------修改客户---------
请选择待修改客户编写(-1退出):1
姓名(张三):<直接回车表示不修改>
性别(男):
年龄(30):
电话(010-56238762):
邮箱(zhang@abc.com):zsan@abc.com
---------修改完成---------
4)删除用户界面
请选择(1-5): 3
---------删除客户---------
请选择待删除客户编号(-1退出):1
确认是否删除(Y/N):y
---------删除完成---------
5)客户列表页面
请选择(1-5): 4
---------客户列表---------
编号 姓名 性别 年龄 电话 邮箱
1 张三 男 30 010-56238762 zsan@abc.com
3.客户关系管理程序框架图
customerView.go(界面)v 【含customerService字段】
1)显示界面
2)接收输入
3)根据用户的输入,调用customerService的方法完成客户的管理(修改,删除,显示等待)
constomService(处理业务逻辑)业务处理
1)完成对用户的各种操作
2)对客户的增删改显示
3)会声明一个customer的切片
customer(表示数据) model层
1)表示一个客户
2)客户各种字段
4.项目功能实现
4.1显示主菜单和完成退出软件功能
1)功能的说明
当用户运行程序时,可以看到主菜单,当输入5时,可以退出该软件
2)思路分析
编写customerView.go,另外可以把customer.go和customerService写部分
3)代码实现
model/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, } }
service/customerService.go
package service import "gotest1/src/test/test59_1/model" //该CustomerService,完成对Customer的操作,包括增删改查 type CustomerService struct { customers []model.Customer //声明一个字段,表示当前切片含有多少个客户 //该字段后面,还可以作为新客户的id+1 customerNum int }
view/customerView.go
package main import "fmt" type customerView struct { key string //接收用户输入。。。 loop bool //表示是否循环的显示菜单 } func (this *customerView) mainView() { 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": 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() { cv := customerView{ key: "", loop: true, } cv.mainView() }
4.2完成显示客户列表的功能
功能说明:
思路分析:
需要编写CustomerService的List[返回客户信息,其实就是切片],CustomerView 编写list方法调用customerService的list方法,并显示客户列表
代码实现:
model/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 { customer := 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 customer }
service/customerService.go
package service import "gotest1/src/test/test59_1/model" //该CustomerService,完成对Customer的操作,包括增删改查 type CustomerService struct { customers []model.Customer //声明一个字段,表示当前切片含有多少个客户 //该字段后面,还可以作为新客户的id+1 customerNum int } 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 }
view/customerView.go
package main import ( "fmt" "gotest1/src/test/test59_1/service" ) type customerView struct { key string //接收用户输入。。。 loop bool //表示是否循环的显示菜单 customerService *service.CustomerService //增加一个字段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("---------客户列表完成---------") } func (this *customerView) mainView() { 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": 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() { cv := customerView{ key: "", loop: true, } cv.customerService = service.NewcustomerService() cv.mainView() }
4.3完成所有功能的代码
view/customerView.go
package main import ( "fmt" "gotest1/src/test/test59_1/model" "gotest1/src/test/test59_1/service" ) type customerView struct { key string //接收用户输入。。。 loop bool //表示是否循环的显示菜单 customerService *service.CustomerService //增加一个字段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("---------客户列表完成---------") } //得到用户的输入,信息构建新的客户,并完成添加 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号不让用户输入,id号是唯一的,由系统生成 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/n):") flag := "" for { fmt.Scanln(&flag) if flag == "y" || flag == "Y" || flag == "n" || flag == "N" { break } fmt.Println("输入有误,请重新输入(y/n):") } if flag == "y" || flag == "Y" { if this.customerService.Delete(id) { fmt.Println("--------删除完成---------") } else { fmt.Println("--------删除失败,输入的id号不存在---------") } } } //退出系统 func (this *customerView) exit() { choice := "" fmt.Println("确认是否退出(y/n):") for { fmt.Scanln(&choice) if choice == "y" || choice == "Y" || choice == "n" || choice == "N" { break } fmt.Println("输入有误,请重新输入(y/n):") } if choice == "y" || choice == "Y" { this.loop = false } } func (this *customerView) update() { fmt.Println("---------修改客户---------") fmt.Println("请选择待修改客户编号(-1退出):") id := -1 fmt.Scanln(&id) if id == -1 { return } index := this.customerService.FindById(id) fmt.Println("id:", id) fmt.Printf("姓名(%v)", this.customerService.List()[index].Name) name := "" fmt.Scanln(&name) fmt.Printf("性别(%v)", this.customerService.List()[index].Gender) gender := "" fmt.Scanln(&gender) fmt.Printf("年龄(%v)", this.customerService.List()[index].Age) age := 0 fmt.Scanln(&age) fmt.Printf("电话(%v)", this.customerService.List()[index].Phone) phone := "" fmt.Scanln(&phone) fmt.Printf("邮箱(%v)", this.customerService.List()[index].Email) email := "" fmt.Scanln(&email) customer := model.NewCustomer2(name, gender, age, phone, email) //if this.customerService.Update(id, name, gender, age, phone, email){ if this.customerService.Update(id, customer) { fmt.Println("--------删除完成---------") } else { fmt.Println("--------删除失败,输入的id号不存在---------") } } func (this *customerView) mainView() { 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": 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() { cv := customerView{ key: "", loop: true, } cv.customerService = service.NewcustomerService() cv.mainView() }
service/customerService.go
package service import ( "fmt" "gotest1/src/test/test59_1/model" ) //该CustomerService,完成对Customer的操作,包括增删改查 type CustomerService struct { customers []model.Customer //声明一个字段,表示当前切片含有多少个客户 //该字段后面,还可以作为新客户的id+1 customerNum int } 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 } 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) if index == -1 { return false } this.customers = append(this.customers[:index], this.customers[index+1:]...) return true } //修改客户 //func (this *CustomerService) Update(id int, name string, gender string, age int, phone string, email string) bool { func (this *CustomerService) Update(id int, customer model.Customer) bool { index := this.FindById(id) if index == -1 { return false } fmt.Println("id:", id) fmt.Println("index:", index) this.customers[index].Name = customer.Name this.customers[index].Gender = customer.Gender this.customers[index].Age = customer.Age this.customers[index].Phone = customer.Phone this.customers[index].Email = customer.Email 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 }
model/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, } } //构建一个不带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 { customer := 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 customer }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?