第十三章 Golang客户信息关系系统

简介: 第十三章 Golang客户信息关系系统

1.项目需求分析

  1. 模拟实现基于文本界面的《客户信息管理软件》
  2. 该软件能够实现对客户对象的插入,修改和删除(用切片实现),并能够打印客户明细表

2.软件的界面设计

主菜单界面

思路实现

// customer.go

package model 
import (
  "fmt"
)
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\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 
}

// customerService.go

package service
import (
  "go_code/customerManage/model"
)
// 该CustomerService,完成对Customer的操作,包括  增删改查
type CustomerService struct{
  customers []model.Customer
  // 声明一个字段,表示当前切片含有多少个客户
  // 该字段后面,还可以作为新客户的id+1
  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{
  // 我们确定一个分配id的规则,就是添加的顺序
  this.customerNum++
  customer.Id = this.customerNum
  this.customers = append(this.customers,customer)
  return true 
}
// 根据ID删除客户(从切片删除)
func (this *CustomersService) 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
    }
  }
}

// customerView.go

package main
import (
   "fmt"
   "go_code/customerManage/Service"
   "go_code/customerManage/model"
)
type customerView struct{
  // 定义必要字段
  key string // 接受用户输入...
  loop bool // 表示是否循环的显示主菜单
  // 增加一个字段customerService
  customerService *service.CustomerService 
}
// 得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add(){
  fmt.Println("--------------------添加客户---------------")
  fmt.Println("姓名:")
  name := ""
  fmt.Scanln(&name)
  fmt.Println("性别:")
  gender := ""
  fmt.Scanln(&gender)
  fmt.Println("年龄:")
  age := ""
  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) 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.Print("-----------客户列表完成-------------")
}
// 得到用户的ID,删除该ID对应的客户
func (this *customerView) delete(){
  
  fmt.Println("-----------删除客户---------------")
  fmt.Println("请选择待删除的客户编号(-1退出):")
  id := -1
  fmt.Scanln(&id)
  if id == -1{
    return //放弃删除操作
  }
  
  fmt.Println("确认是否删除(Y/N):")
  //这里可以加入一个循环判断,直到用户输入y或者是n,才退出..
  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"{
      break
    }
    fmt.Println("你的输入有误,请重新输入(Y/N):")
  }
  
  if this.key == "Y" || this.key == "y"{
    this.loop = false
  }
}
//显示主菜单
func (cv *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()
}

感谢大家观看,我们下次见

目录
相关文章
|
6月前
|
存储 Go 数据安全/隐私保护
Golang 语言怎么使用 Viper 管理配置信息?(下)
Golang 语言怎么使用 Viper 管理配置信息?(下)
24 0
|
6月前
|
存储 JSON 监控
Golang 语言怎么使用 Viper 管理配置信息?(上)
Golang 语言怎么使用 Viper 管理配置信息?
23 1
在Golang中添加打印文件、行号等信息
调试过程中发现log不带打印行号和文件信息功能
|
JSON 测试技术 Go
Golang使用协程进行mqtt的publish信息性能测试
开发语言:golang 目的:并发10000个mqtt连接,循环发送publish信息,当时间戳小于某个值的时候,中止循环,退出连接 publish内容是json格式的,未设置时,有默认值,可以通过golang代码修改json内容 登录信息存取在csv文件中,csv文件有多少列,就并发多少个设备连接
807 0
|
Go Python PHP
golang 请求带验证信息的坑
最近用golang 和python对接接口,由于之前验证那块没有设置好,然后又为了进度,最近决定用http自带的basic 验证, php的代码很快就验证通过了 /** * @param $url * @param $filename * @param $...
1371 0
|
6月前
|
存储 编译器 Go
Golang 语言的多种变量声明方式和使用场景
Golang 语言的多种变量声明方式和使用场景
32 0
|
1月前
|
SQL 前端开发 Go
编程笔记 GOLANG基础 001 为什么要学习Go语言
编程笔记 GOLANG基础 001 为什么要学习Go语言
|
6月前
|
存储 JSON Go
Golang 语言 gRPC 服务怎么同时支持 gRPC 和 HTTP 客户端调用?
Golang 语言 gRPC 服务怎么同时支持 gRPC 和 HTTP 客户端调用?
76 0
|
3月前
|
物联网 Go 网络性能优化
使用Go语言(Golang)可以实现MQTT协议的点对点(P2P)消息发送。MQTT协议本身支持多种消息收发模式
使用Go语言(Golang)可以实现MQTT协议的点对点(P2P)消息发送。MQTT协议本身支持多种消息收发模式【1月更文挑战第21天】【1月更文挑战第104篇】
101 1
|
1天前
|
安全 Go 开发者
Golang深入浅出之-Go语言并发编程面试:Goroutine简介与创建
【4月更文挑战第22天】Go语言的Goroutine是其并发模型的核心,是一种轻量级线程,能低成本创建和销毁,支持并发和并行执行。创建Goroutine使用`go`关键字,如`go sayHello(&quot;Alice&quot;)`。常见问题包括忘记使用`go`关键字、不正确处理通道同步和关闭、以及Goroutine泄漏。解决方法包括确保使用`go`启动函数、在发送完数据后关闭通道、设置Goroutine退出条件。理解并掌握这些能帮助开发者编写高效、安全的并发程序。
10 1