240_尚硅谷_客户管理系统-删除客户
1.客户关系管理的程序框架图_删除用户
2.model下的customer的客户信息结构体不做改变
package model
import (
"fmt"
)
// todo 声明一个Customer结构体, 表示一个客户信息
type Customer struct {
Id int
Name string
Gender string
Age int
Phone string
Email string
}
// todo 方式一: 使用一个工厂模式,返回一个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,
}
}
// todo 2.2 方式二: 使用一个工厂模式,返回一个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,
}
}
// todo 返回用户的信息, 格式化的字符串
func (this Customer) GetInfo() string {
info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v",
this.Id, this.Name, this.Gender, this.Age, this.Phone, this.Email)
return info
}
3.service下的customerService中执行查询客户信息方法FindById和删除客户信息方法Delete
package service
import (
"customerMange/model"
"fmt"
)
// todo 该CustomerService, 完成对Customer的操作, 包括
// todo 增删改查
type CustomerService struct {
customers []model.Customer // * 定义切片,customers
customerNum int // * 声明一个字段, 表示当前切片含有多少个客户, 该字段后续还可以作为新客户的id+1
}
// todo 1.1. 编写一个方法,可以返回 *CustomerService 实例和信息初始化的工作
func NewCustomerService() *CustomerService {
// 为了能看到初始客户信息,初始化一个客户
customerService := &CustomerService{}
customerService.customerNum = 1
// * NewCustomer是model包下customer.go中的工厂模式方法
customer := model.NewCustomer(1, "name1", "男", 20, "111111111", "name1@qq.com")
customerService.customers = append(customerService.customers, customer)
return customerService
}
// todo 1.2. 返回客户切片
func (this *CustomerService) List() []model.Customer {
return this.customers
}
// todo 2.3. 添加客户到CustomerService中的customers切片中, 数据入库操作
// !!! *CustomerService是指向结构体CustomerService的指针类型
func (this *CustomerService) Add(customer model.Customer) bool {
// ! 默认把id当作自增主键,没有主动传参,所以确定一个分配id的规则,
// !即添加用户时自动生成id
this.customerNum++
customer.Id = this.customerNum
this.customers = append(this.customers, customer)
return true
}
// todo 3.1. 根据id查找客户在切片中对应下标,如果没有该客户,返回-1
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
fmt.Println(this.customers)
}
}
return index
}
// todo 3.2. FindById查到id后,删除该用户信息
func (this *CustomerService) Delete(id int) bool {
index := this.FindById(id)
if index == -1 {
return false
}
// ! 如何切片删除单个元素,使用切片,从append([:目标删除索引], [目标索引+1:]...)
this.customers = append(this.customers[:index], this.customers[index+1:]...)
return true
}
4.view下的customerView中获取终端交互的id,并在主菜单中调用删除方法实现调用后续数据操作
package main
import (
"customerMange/model"
"customerMange/service"
"fmt"
)
type customerView struct {
// 定义必要的字段
key string // * 接收用户输入
loop bool // * 表示是否循环显示主菜单
customerService *service.CustomerService // * 新增一个字段customerService
}
// todo 显示所有的客户信息
func (this *customerView) list() {
// 1. 获取到当前所有的客户信息(在切片中)
customer_list := this.customerService.List()
// 2. 显示
fmt.Println("---------------------------客户列表---------------------------")
fmt.Println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱")
// 遍历切片
for i := 0; i < len(customer_list); i++ {
// * 调用 customer.go中格式化输出
fmt.Println(customer_list[i].GetInfo())
}
fmt.Println("-------------------------客户列表完成-------------------------")
}
// todo 2.1. 得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add() {
fmt.Println("---------------------------添加客户---------------------------")
fmt.Printf("Input name:")
var name string
fmt.Scanln(&name)
fmt.Printf("Input gender:")
var gender string
fmt.Scanln(&gender)
fmt.Printf("Input age:")
var age int
fmt.Scanln(&age)
fmt.Printf("Input phone:")
var phone string
fmt.Scanln(&phone)
fmt.Printf("Input email:")
var email string
fmt.Scanln(&email)
// * 构建一个新的Customer实例
// * 注意: id作为主键自动分配,不需要传参,
// * 将输入的内容传参给model.NewCustomer2获得实例返回,
// * 返回结果作为customerService.Add()传参
customer := model.NewCustomer2(name, gender, age, phone, email)
// * 调用, 判断是否添加成功
if this.customerService.Add(customer) {
fmt.Println("---------------添加完成---------------")
} else {
fmt.Println("---------------添加失败---------------")
}
}
// todo 3.3. 得到用户的输入id,
func (this *customerView) delete() {
var id int = -1
fmt.Println("---------------------------删除客户---------------------------")
fmt.Printf("删除用户编号(-1退出):")
fmt.Scanln(&id)
if id == -1 {
return // 放弃后续操作
}
fmt.Printf("确认是否删除(Y/N):")
choice := ""
fmt.Scanln(&choice)
if choice == "y" || choice == "Y" {
// 调用customerService的delete方法
if this.customerService.Delete(id) {
fmt.Println("---------------------------删除完成---------------------------")
} else {
fmt.Println("---------------------------删除失败, id不存在---------------------------")
}
}
}
// todo 显示主菜单
func (this *customerView) mainMenu() {
for {
fmt.Println("---------------客户信息管理软件---------------")
fmt.Println(" 1 添加客户 ")
fmt.Println(" 2 修改客户 ")
fmt.Println(" 3 删除客户 ")
fmt.Println(" 4 客户列表 ")
fmt.Println(" 5 退出 ")
fmt.Printf(" 请选择(1-5): ")
// todo 执行终端输入
fmt.Scanln(&this.key)
switch this.key {
case "1":
// todo 2.4. 调用add() 方法,测试数据如果是否正常
this.add()
case "2":
fmt.Println("2 修改客户")
case "3":
// todo 3.4. 调用delete() 方法,测试是否正常删除
this.delete()
case "4":
this.list()
case "5":
fmt.Println("5 退出")
this.loop = false
default:
fmt.Println("输入有误, 请重新输入!")
}
// todo this.loop默认数true, 如果为取反为false, 退出程序,终止循环
if !this.loop {
break
}
}
fmt.Println("`客户关系管理系统`已退出")
}
func main() {
// 在主函数中,创建一个customerView, 并运行显示主菜单......
customerView := customerView{
key: "",
loop: true,
}
// 完成对customerView结构体的customerService字段初始化
customerView.customerService = service.NewCustomerService()
// 调用显示主菜单
customerView.mainMenu()
}
5.删除功能运行结果
浙公网安备 33010602011771号