241_尚硅谷_客户管理系统-修改客户和作业

1.完善退出功能和实现修改功能需求1.完善退出功能和实现修改功能需求

2.用户必须输入y或n否则一直循环运行2.用户必须输入y或n否则一直循环运行

// todo 4.1. 将this.loop = false 写进方法中,实现循环退出
func (this *customerView) exit() {
	fmt.Printf("确认是否推退出(Y/N):")
	for {
		fmt.Scanln(&this.key)
		if this.key == "y" || this.key == "Y" || this.key == "n" || this.key == "N" {
			break
		}
		fmt.Printf("输入有误,确认是否推退出(Y/N):")
	}
	if this.key == "y" || this.key == "Y" {
		this.loop = false
	}

}

// 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":
			// todo 5.4. 调用update() 方法,测试是否可以正常修改用户信息
			this.update()
		case "3":
			// todo 3.4. 调用delete() 方法,测试是否正常删除
			this.delete()
		case "4":
			this.list()
		case "5":
			// todo 4.2. 调用exit() 方法,退出主菜单
			this.exit()
		default:
			fmt.Println("输入有误, 请重新输入!")
		}

		// todo this.loop默认数true, 如果为取反为false, 退出程序,终止循环
		if !this.loop {
			break
		}
	}
	fmt.Println("`客户关系管理系统`已退出")
}

3.用户必须输入y或n, 否则循环提示运行结果3.用户必须输入y或n, 否则循环提示运行结果

4.service下的customerService中查询用户信息FindByInfo方法和修改用户信息方法Update4.service下的customerService中查询用户信息FindByInfo方法和修改用户信息方法Update

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
}

// todo 5.1. 根据id查找客户,如果没有该客户,返回nuil和false
func (this *CustomerService) FindByInfo(id int) (*model.Customer, bool) {
	for i := 0; i < len(this.customers); i++ {
		if this.customers[i].Id == id {
			// 找到对应用户,返回用户指针和true
			return &this.customers[i], true
		}
	}
	// 没找到,返回nil和false
	return nil, false
}

// todo 5.2 FindById查到id后,修改该用户信息
func (this *CustomerService) Update(id int, name string, gender string, age int, phone string, email string) bool {
	customer, found := this.FindByInfo(id)
	if !found {
		return false
	}
	// ! 如何切片删除单个元素,使用切片,从append([:目标删除索引], [目标索引+1:]...)
	// this.customers = append(this.customers[:index], this.customers[index+1:]...)
	customer.Name = name
	customer.Gender = gender
	customer.Age = age
	customer.Phone = phone
	customer.Email = email
	return true
}

5.view下的customerView中输入修改用户信息内容,类似新增用户信息,前提需要查询是否有该用户5.view下的customerView中输入修改用户信息内容,类似新增用户信息,前提需要查询是否有该用户

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 5.3 得到用户的输入id, 修改用户信息
func (this *customerView) update() {
	var id int = -1
	fmt.Println("---------------------------修改客户---------------------------")
	fmt.Printf("请选择待修改客户编号(-1退出):")
	fmt.Scanln(&id)
	if id == -1 {
		return // 放弃后续操作
	}
	// 查找客户是否存在
	customer, found := this.customerService.FindByInfo(id)
	if !found {
		fmt.Println("该客户不存在!")
		return
	}

	fmt.Printf("Input name(%v):", customer.Name)
	var name string
	fmt.Scanln(&name)

	fmt.Printf("Input gender(%v):", customer.Gender)
	var gender string
	fmt.Scanln(&gender)

	fmt.Printf("Input age(%v):", customer.Age)
	var age int
	fmt.Scanln(&age)

	fmt.Printf("Input phone(%v):", customer.Phone)
	var phone string
	fmt.Scanln(&phone)

	fmt.Printf("Input email(%v):", customer.Email)
	var email string
	fmt.Scanln(&email)
	if this.customerService.Update(id, name, gender, age, phone, email) {
		fmt.Println("修改成功")

	} else {
		fmt.Println("修改失败")
	}

}

// todo 4.1. 将this.loop = false 写进方法中,实现循环退出
func (this *customerView) exit() {
	fmt.Printf("确认是否推退出(Y/N):")
	for {
		fmt.Scanln(&this.key)
		if this.key == "y" || this.key == "Y" || this.key == "n" || this.key == "N" {
			break
		}
		fmt.Printf("输入有误,确认是否推退出(Y/N):")
	}
	if this.key == "y" || this.key == "Y" {
		this.loop = false
	}

}

// 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":
			// todo 5.4. 调用update() 方法,测试是否可以正常修改用户信息
			this.update()
		case "3":
			// todo 3.4. 调用delete() 方法,测试是否正常删除
			this.delete()
		case "4":
			this.list()
		case "5":
			// todo 4.2. 调用exit() 方法,退出主菜单
			this.exit()
		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()
}

6.修改用户信息的运行结果6.修改用户信息的运行结果

posted on 2026-02-11 17:16  与太阳肩并肩  阅读(3)  评论(0)    收藏  举报

导航