Press "Enter" to skip to content

37、Go面向对象项目-客户管理系统

Last updated on 2022年2月16日

一、练习题

小练习,直接上代码,写的比较屎,对语言的熟练度不够暂时还不能随心所欲写,使用的MVC模式,初步了解了Golang在开发中的模式和大致逻辑以及写法。

// 项目文件结构
// -- customer/
// ---- model/
// ------ custom.go
// ---- service/
// ------ custom.go
// ---- view/
// ------ custom.go
// ......
// go.mod
// model/custom.go

package model

import (
"fmt"
)

// Customer 定义一个客户结构体
type Customer struct {
Id     int
Name   string
Gender string
Age    int
Phone  string
Email  string
}

// NewCustomer 获取一个实例并返回
func NewCustomer(id, age int, name, gender, phone, email string) *Customer {
return &Customer{
Id:     id,
Name:   name,
Gender: gender,
Age:    age,
Phone:  phone,
Email:  email,
}
}

// GetNilInstance 获取一个空实例
func GetNilInstance() *Customer {
return &Customer{}
}

// GetInfo 获取某个客户的格式化后的信息
func (c *Customer) GetInfo() string {
return fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v", c.Id, c.Name, c.Age, c.Gender, c.Phone, c.Email)
}
// service/custom.go

package service

import (
"go_study/customer/model"
)

// CustomerService 该结构体完成对Customer的操作,包括增删改查
type CustomerService struct {
// 客户,类型为Customer类型的切片
customers []model.Customer

// 当前客户数量,顺便作为新客户的id
customerNum int
}

// NewCustomerService 返回 CustomerService 实例
func NewCustomerService() *CustomerService {
// 为了方便效果展示,初始化一个客户在切片里面
customerService := &CustomerService{}
customerService.customerNum = 1                                                     // 客户数量
customer := model.NewCustomer(1, 18, "张三", "男", "13988889999", "zhangsan@sina.com") // 客户信息
customerService.customers = append(customerService.customers, *customer)

//返回service指针
return customerService
}

// GetNextId 获取下一个客户编号
func (_this *CustomerService) GetNextId() int {
return _this.customerNum + 1
}

// GetCustomerInstance 获取一个Customer空实例
func (_this *CustomerService) GetCustomerInstance() *model.Customer {
return model.GetNilInstance()
}

// FindById 根据id获取某个客户的下标, 并返回下标
func (_this *CustomerService) FindById(id int) int {
index := -1
for i, v := range _this.customers {
if v.Id == id {
index = i
break
}
}
return index
}

// List 返回客户列表
func (_this *CustomerService) List() []model.Customer {
return _this.customers
}

// AddCustomer 添加客户操作
func (_this *CustomerService) AddCustomer(customer *model.Customer) {
customer.Id = _this.GetNextId()
_this.customers = append(_this.customers, *customer)
}

// DeleteCustomer 删除某个客户
func (_this *CustomerService) DeleteCustomer(id int) bool {
// 获取需要删除的切片的下标
index := _this.FindById(id)
if index == -1 {
return false
}
// 删除元素
_this.customers = append(_this.customers[:index], _this.customers[index+1:]...)
return true
}

// UpdateCustomer 需改某个客户
func (_this *CustomerService) UpdateCustomer(id, age int, name, gender, phone, email string) {
var customers = _this.customers
for i, customer := range customers {
if customer.Id == id {
// 进行修改
if age != 0 {
customer.Age = age
}
if name != "" {
customer.Name = name
}
if gender != "" {
customer.Gender = gender
}
if phone != "" {
customer.Phone = phone
}
if email != "" {
customer.Email = email
}
customers[i] = customer
break
}
}
}
// view/custom.go

package main

import (
"fmt"
"go_study/customer/service"
)

type customerView struct {
InputKey        int                      // 选择的key
hasExit         bool                     // 表示是否退出软件
customerService *service.CustomerService // 需要一个customerService
}

// 显示客户列表
func (c *customerView) list() {
customers := c.customerService.List()
fmt.Println("---------------------客户列表---------------------")
fmt.Println("编号\t姓名\t年龄\t性别\t手机号\t\t邮箱")
if len(customers) <= 0 {
fmt.Println("\t\t\t暂无客户")
} else {
for _, v := range customers {
fmt.Println(v.GetInfo())
}
}
fmt.Println("------------------客户列表加载完毕------------------")
}

// 添加客户
func (c *customerView) addCustomer() {
customer := c.customerService.GetCustomerInstance() // 获取一个客户空实例
fmt.Println("请输入姓名")
fmt.Scanln(&customer.Name)
fmt.Println("请输入性别")
fmt.Scanln(&customer.Gender)
fmt.Println("请输入年龄")
fmt.Scanln(&customer.Age)
fmt.Println("请输入手机号")
fmt.Scanln(&customer.Phone)
fmt.Println("请输入邮箱")
fmt.Scanln(&customer.Email)
c.customerService.AddCustomer(customer)
fmt.Println("恭喜您,客户添加成功")
}

// 删除客户
func (c *customerView) deleteCustomer() {
c.list()
fmt.Println("请输入需要删除的客户的编号:")
var id int
fmt.Scanln(&id)

if c.customerService.DeleteCustomer(id) {
fmt.Println("成功删除客户:", id)
} else {
fmt.Println("客户编号:", id, "没找到")
}
}

// 修改客户
func (c *customerView) updateCustomer() {
c.list() // 先展示列表,看看需要改谁
fmt.Println("请输入需要需改的客户编号:")
var id int
fmt.Scanln(&id)
// 获取需要修改的用户信息
customers := c.customerService.List() // 全部客户
var hasFindCustomer bool
for _, v := range customers {
if v.Id == id {
hasFindCustomer = true
// 进行修改
// 客户需要修改的信息
fmt.Printf("请输入姓名(%v)", v.Name)
var name string
fmt.Scanln(&name)
fmt.Printf("请输入性别(%v)", v.Gender)
var gender string
fmt.Scanln(&gender)
fmt.Printf("请输入年龄(%v)", v.Age)
var age int
fmt.Scanln(&age)
fmt.Printf("请输入手机号(%v)", v.Phone)
var phone string
fmt.Scanln(&phone)
fmt.Printf("请输入邮箱(%v)", v.Email)
var email string
fmt.Scanln(&email)

// 进行修改
c.customerService.UpdateCustomer(id, age, name, gender, phone, email)
break
}
}
if !hasFindCustomer {
fmt.Println("未找到您输入的客户id")
}
}

// 退出软件
func (c *customerView) exit() {
c.hasExit = true
}

// Menu 显示主菜单
func (c *customerView) Menu() {
for {
fmt.Println("客户信息管理系统")
fmt.Println("1 添加客户")
fmt.Println("2 修改客户")
fmt.Println("3 删除客户")
fmt.Println("4 客户列表")
fmt.Println("5 退出软件")
fmt.Println("请选择(1-5):")
fmt.Scanln(&c.InputKey)

switch c.InputKey {
case 1:
c.addCustomer()
case 2:
c.updateCustomer()
case 3:
c.deleteCustomer()
case 4:
c.list()
case 5:
c.exit()
if c.hasExit {
fmt.Println("您已退出软件")
return
}
default:
fmt.Println("输入错误,请重新输入")
}
}
}

func main() {
// 创建一个客户实例
view := customerView{}
// 初始化 customerService 字段
view.customerService = service.NewCustomerService()
// 显示主菜单
view.Menu()
}

One Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注