• 【Go语言】项目实战:客户信息管理系统(需求分析、项目设计、功能实现)


    声明:本项目学习自 尚硅谷 的 Go 语言教程:教程链接,本文为项目学习的笔记整理

    1 需求分析

    客户信息管理系统实现

    • 友好的操作界面
    • 对客户对象的插入
    • 对客户对象的修改和删除(用切片实现)
    • 能够打印客户明细表

    2 项目设计

    2.1 模块设计

    在这里插入图片描述
    在这里插入图片描述

    2.2 界面设计

    主菜单界面

    在这里插入图片描述

    添加客户界面

    在这里插入图片描述

    修改客户界面

    删除客户界面

    在这里插入图片描述

    客户列表界面

    在这里插入图片描述

    3 功能实现

    3.1 功能:编写实体对象、初步搭建项目

    在 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,
    	}
    }
    
    //第二种创建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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在 service 层 customerService.go 中

    package service
    
    import (
    	"customerManage/model"
    )
    
    //该CustomerService, 完成对Customer的操作,包括
    //增删改查
    type CustomerService struct {
    	customers []model.Customer
    	//声明一个字段,表示当前切片含有多少个客户
    	//该字段后面,还可以作为新客户的id+1
    	customerNum int
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在 view 层 customerView.go 中

    package main
    
    import "fmt"
    
    //CustomerView【view 层】
    //1、显示界面
    //2、接收用户输入
    //3、根据用户输入,调用 customerService 的方法完成客户的管理【修改,删除,显示等】
    type CustomerView struct {
    	key  string //接收用户输入...
    	loop bool   //表示是否循环的显示主菜单
    	customerService *service.CustomerService
    }
    
    func (this *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":
    			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() {
    	customerView := CustomerView{
    		key:  "",
    		loop: true,
    	}
    
    	customerView.mainMenu()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    初步测试运行

    在这里插入图片描述
    在这里插入图片描述

    3.2 功能:显示客户列表

    在 service 层 customerService.go 中

    //编写一个方法,可以返回 *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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在 view 层 customerView.go 中

    //显示所有的客户信息
    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].Id,"\t", customers[i].Name...)
    		fmt.Println(customers[i].GetInfo())
    	}
    	fmt.Printf("\n-------------------------客户列表完成-------------------------\n\n")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    3.3 功能:添加客户

    在 service 层 customerService.go 中

    //添加客户到customers切片
    //!!!
    func (this *CustomerService) Add(customer model.Customer) bool {
    
    	//我们确定一个分配id的规则,就是添加的顺序
    	this.customerNum++
    	customer.Id = this.customerNum
    	this.customers = append(this.customers, customer)
    	return true
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在 view 层 customerView.go 中

    //得到用户的输入,信息构建新的客户,并完成添加
    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("---------------------添加失败---------------------")
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述
    在这里插入图片描述

    3.4 功能:修改客户信息

    在 service 层 customerService.go 中

    在 view 层 customerView.go 中

    3.5 功能:删除客户

    在 service 层 customerService.go 中

    //根据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
    }
    
    //根据id删除客户(从切片中删除)
    func (this *CustomerService) 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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在 view 层 customerView.go 中

    //得到用户的输入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 := ""
    	fmt.Scanln(&choice)
    	if choice == "y" || choice == "Y" {
    		//调用customerService 的 Delete方法
    		if this.customerService.Delete(id) {
    			fmt.Println("---------------------删除完成---------------------")
    		} else {
    			fmt.Println("---------------------删除失败,输入的id号不存在----")
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述
    在这里插入图片描述

    3.6 功能:退出软件

    在 view 层 customerView.go 中

    //退出软件
    func (this *customerView) exit() {
    
    	fmt.Println("确认是否退出(Y/N):")
    	for {
    		fmt.Scanln(&this.key)
    		if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n" {
    			break
    		}
    
    		fmt.Println("你的输入有误,确认是否退出(Y/N):")
    	}
    
    	if this.key == "Y" || this.key == "y" {
    		this.loop = false
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    3.7 功能:显示主菜单与主函数

    在 view 层 customerView.go 中

    //显示主菜单
    func (this *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()
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

  • 相关阅读:
    分布式应用:从CAP理论到PACELC理论
    Spring+SpringMVC+Mybatis的回顾
    1107 Social Clusters 甲级 xp_xht123
    开源日志分析平台ELK实战应用
    Android ZXing 二维码/条形码 开源库的简单应用
    【Java进阶篇】第七章 多线程
    如何提取 x64 程序那些易失的方法参数
    第十四届蓝桥杯大赛软件赛决赛 C/C++ 大学 B 组 试题 A: 子 2023
    有什么好用的IT资产管理软件
    代码审查问题思考
  • 原文地址:https://blog.csdn.net/m0_46360532/article/details/125593977