Go-客户信息关系系统的实现

 更新时间:2023年01月18日 14:31:58   作者:飞哥亡命天涯  
这篇文章主要介绍了Go-客户信息关系系统的实现,本文章内容详细,具有很好的参考价值,希望对大家有所帮助,需要的朋友可以参考下

项目需求分析

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

项目的界面设计

主菜单界面

请添加图片描述

添加客户界面

请添加图片描述

删除客户界面

请添加图片描述

客户列表界面

请添加图片描述

客户关系管理系统的程序框架图

请添加图片描述

项目功能实现-显示主菜单和完成退出软件功能

请添加图片描述

代码实现 customerManage/model/customer.go

package model


//声明一个 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,
	}
}

customerManage/service/customerService.go

package service

import "go_code/go_code/chapter13/customerManage/model"

type CustomerService struct{
	customers []model.Customer
	//声明一个字段,表示当前切片含有多少个客户
	//该字段后面,还可以作为新客户的 id+1
	customerNum int
}

customerManage/view/customerView.go

package main

import "fmt"

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.Println()
		fmt.Println("        请选择(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(){
	//在 main 函数中,创建一个 customerView,并运行显示主菜单..
	customerView:=customerView{
		key:"",
		loop:true,
	}
	//显示主菜单
	customerView.mainMenu()
}

项目功能实现-完成显示客户列表的功能

请添加图片描述

请添加图片描述

请添加图片描述

请添加图片描述

customerManage/view/customerView.go

package main

import (
	"fmt"
	"go_code/go_code/chapter13/customerManage/service"
)

type customerView struct {
	//定义必要字段
	key  string //接收客户输入...
	loop bool   //表示是否循环的显示主菜单
	//增加一个字段 customerService
	customerService *service.CustomerService
}

//显示所有客户信息
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.Println("--------客户列表完成-----------")
}

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

		fmt.Scanln(&this.key)

		switch this.key {
		case "1":
			fmt.Println("添加客户")
		case "2":
			fmt.Println("修改客户")
		case "3":
			this.list()
		case "4":
			fmt.Println("客户客户")
		case "5":
			this.loop = false
		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()
}

项目功能实现-添加客户的功能

请添加图片描述

请添加图片描述

请添加图片描述

customerManage/service/customerService.go

请添加图片描述

customerManage/service/customerView.go

package main

import (
	"fmt"
	"go_code/go_code/chapter13/customerManage/model"
	"go_code/go_code/chapter13/customerManage/service"
)

type customerView struct {
	//定义必要字段
	key  string //接收客户输入...
	loop bool   //表示是否循环的显示主菜单
	//增加一个字段 customerService
	customerService *service.CustomerService
}

//显示所有客户信息
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.Println("--------客户列表完成-----------")
}
//得到用户的输入,信息构建新的客户,并完成添加
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 是唯一的,需要系统分配
	custmoer:=model.NewCustomer2(name,gender,age,phone,email)
	//调用
	if this.customerService.Add(custmoer){
		fmt.Println("--------添加完成------------")
	}else{
		fmt.Println("--------添加失败------------")
	}
}

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

		fmt.Scanln(&this.key)

		switch this.key {
		case "1":
			fmt.Println("添加客户")
		case "2":
			fmt.Println("修改客户")
		case "3":
			this.list()
		case "4":
			fmt.Println("客户客户")
		case "5":
			this.loop = false
		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()
}

项目功能实现-完成删除客户的功能

请添加图片描述

请添加图片描述

customerManage/model/customer.go [没有变化]

customerManage/service/customerService.go

请添加图片描述

customerManage/view/customerView.go

package main

import (
	"fmt"
	"go_code/go_code/chapter13/customerManage/model"
	"go_code/go_code/chapter13/customerManage/service"
)

type customerView struct {
	//定义必要字段
	key  string //接收客户输入...
	loop bool   //表示是否循环的显示主菜单
	//增加一个字段 customerService
	customerService *service.CustomerService
}

//显示所有客户信息
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.Println("--------客户列表完成-----------")
}

//得到用户的输入,信息构建新的客户,并完成添加
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 是唯一的,需要系统分配
	custmoer := model.NewCustomer2(name, gender, age, phone, email)
	//调用
	if this.customerService.Add(custmoer) {
		fmt.Println("--------添加完成------------")
	} else {
		fmt.Println("--------添加失败------------")
	}
}
func (this *customerView) delete() {
	flag := false
	fmt.Println("--------删除客户------------")
	for {
		fmt.Println("请选择待删除客户编号(-1)退出:")
		id := -1
		fmt.Scanln(&id)
		if id == -1 {
			return //放弃删除操作
		}
		fmt.Println("确认是否删除(Y/N)")
		//这里同学们可以加入一个循环判断,直到用户输入y或者,才退出
		for {
			choice := ""
			fmt.Scanln(&choice)
			if choice == "y" || choice == "n" {
				if this.customerService.Delete(id) {
					fmt.Println("--------删除完成------------")
					flag = true
				} else {
					fmt.Println("--------删除失败,输入的id号不存在-")
				}
				break
			}
		}
		if flag {
			break
		}
	}
}
func (this *customerView) exit() {
	fmt.Println("确认是否退出(Y/N)")
	for {
		fmt.Scanln(&this.key)
		if this.key == "y" || this.key == "n" {
			break
		} else {
			fmt.Println("输入格式错误,请重新输入!")
		}
	}
	if this.key == "y" {
		this.loop = false
	}
}
func (this *customerView) update() {
	fmt.Println("----------------修改客户------------------")
	fmt.Println("客户id(-1退出):")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return // 放弃修改
	}

	//获取要修改的数据,并显示
	index := this.customerService.FindById(id)
	if index == -1 {
		fmt.Println("------------------客户id不存在------------------")
		return
	}
	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)

	fmt.Println("你确定要修改吗? y/n")
	choice := ""
	for {
		fmt.Scanln(&choice)
		if choice == "y" || choice == "n" {
			break
		}
		fmt.Println("你输入有误,请重新输入 y/n")
	}

	customer := model.NewCustomer2(name, gender, age, phone, email)

	//调用customerService.Update
	if this.customerService.Update(index, customer) {
		fmt.Println("------------------修改成功------------------")
	} else {
		fmt.Println("------------------修改失败------------------")
	}
}

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

		fmt.Scanln(&this.key)

		switch this.key {
		case "1":
			this.add()
		case "2":
			this.update()
		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()
}

项目功能实现-完善退出确认功能(课后作业)

请添加图片描述

请添加图片描述

客户关系管理系统-课后练习

请添加图片描述

CustomerView.go

package main

import (
	"fmt"
	"go_code/go_code/chapter13/customerManage/model"
	"go_code/go_code/chapter13/customerManage/service"
)

type customerView struct {
	//定义必要字段
	key  string //接收客户输入...
	loop bool   //表示是否循环的显示主菜单
	//增加一个字段 customerService
	customerService *service.CustomerService
}

//显示所有客户信息
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.Println("--------客户列表完成-----------")
}

//得到用户的输入,信息构建新的客户,并完成添加
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 是唯一的,需要系统分配
	custmoer := model.NewCustomer2(name, gender, age, phone, email)
	//调用
	if this.customerService.Add(custmoer) {
		fmt.Println("--------添加完成------------")
	} else {
		fmt.Println("--------添加失败------------")
	}
}
func (this *customerView) delete() {
	flag := false
	fmt.Println("--------删除客户------------")
	for {
		fmt.Println("请选择待删除客户编号(-1)退出:")
		id := -1
		fmt.Scanln(&id)
		if id == -1 {
			return //放弃删除操作
		}
		fmt.Println("确认是否删除(Y/N)")
		//这里同学们可以加入一个循环判断,直到用户输入y或者,才退出
		for {
			choice := ""
			fmt.Scanln(&choice)
			if choice == "y" || choice == "n" {
				if this.customerService.Delete(id) {
					fmt.Println("--------删除完成------------")
					flag = true
				} else {
					fmt.Println("--------删除失败,输入的id号不存在-")
				}
				break
			}
		}
		if flag {
			break
		}
	}
}
func (this *customerView) exit() {
	fmt.Println("确认是否退出(Y/N)")
	for {
		fmt.Scanln(&this.key)
		if this.key == "y" || this.key == "n" {
			break
		} else {
			fmt.Println("输入格式错误,请重新输入!")
		}
	}
	if this.key == "y" {
		this.loop = false
	}
}
func (this *customerView) update() {
	fmt.Println("----------------修改客户------------------")
	fmt.Println("客户id(-1退出):")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return // 放弃修改
	}

	//获取要修改的数据,并显示
	index := this.customerService.FindById(id)
	if index == -1 {
		fmt.Println("------------------客户id不存在------------------")
		return
	}
	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)

	fmt.Println("你确定要修改吗? y/n")
	choice := ""
	for {
		fmt.Scanln(&choice)
		if choice == "y" || choice == "n" {
			break
		}
		fmt.Println("你输入有误,请重新输入 y/n")
	}

	customer := model.NewCustomer2(name, gender, age, phone, email)

	//调用customerService.Update
	if this.customerService.Update(index, customer) {
		fmt.Println("------------------修改成功------------------")
	} else {
		fmt.Println("------------------修改失败------------------")
	}
}

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

		fmt.Scanln(&this.key)

		switch this.key {
		case "1":
			this.add()
		case "2":
			this.update()
		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()
}

CustomerService.go

package service

import (
	"go_code/go_code/chapter13/customerManage/model"
)

type CustomerService struct {
	customers []model.Customer
	//声明一个字段,表示当前切片含有多少个客户
	//该字段后面,还可以作为新客户的 id+1
	customerNum int
}

//编写一个方法,可以返回*CustmoerService
func NewCustomerService() *CustomerService {
	//为了能够看到有客户在切片中,我们初始化一个切片
	custmoerService := &CustomerService{}
	custmoerService.customerNum = 1
	custmoer := model.NewCustomer(1, "张三", "男",
		20, "112", "zhangsan@sohu.com")
	custmoerService.customers = append(custmoerService.customers, custmoer)
	return custmoerService
}

//返回客户切片
func (this *CustomerService) List() []model.Customer {
	return this.customers
}

//添加客户到切片
//!!!
func (this *CustomerService) Add(custmoer model.Customer) bool {
	this.customerNum++
	custmoer.Id = this.customerNum
	this.customers = append(this.customers, custmoer)
	return true
}
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
}

func (this *CustomerService)Update(index int,customer model.Customer)bool{
	this.customers[index].Name= customer.Name
	this.customers[index].Age= customer.Age
	this.customers[index].Gender= customer.Gender
	this.customers[index].Phone= customer.Phone
	this.customers[index].Email= customer.Email
	return true
}
//根据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
		}
	}
	return index
}

到此这篇关于Go-客户信息关系系统的实现的文章就介绍到这了,更多相关Go-客户信息关系系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Golang字符串的拼接方法汇总

    Golang字符串的拼接方法汇总

    字符串拼接在日常开发中是很常见的需求,今天我们来探讨下如何用golang来实现字符串的拼接
    2018-10-10
  • GO 切片删除元素的三种方法

    GO 切片删除元素的三种方法

    本文主要介绍了GO 切片删除元素,根据要删除元素的位置有三种情况,分别是从开头位置删除、从中间位置删除和从尾部删除,具有一定的参考价值,感兴趣的可以了解一下
    2024-08-08
  • 深入解析Go语言中crypto/subtle加密库

    深入解析Go语言中crypto/subtle加密库

    本文主要介绍了深入解析Go语言中crypto/subtle加密库,详细介绍crypto/subtle加密库主要函数的用途、工作原理及实际应用,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • go语言make初始化的实现

    go语言make初始化的实现

    Go语言中的make函数用于初始化切片、映射和通道,本文就来介绍一下go语言make初始化的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-12-12
  • Golang极简入门教程(四):编写第一个项目

    Golang极简入门教程(四):编写第一个项目

    这篇文章主要介绍了Golang极简入门教程(四):编写第一个项目,本文讲解了workspace、包路径、第一个可执行命令等内容,需要的朋友可以参考下
    2014-10-10
  • Go tablewriter库提升命令行输出专业度实例详解

    Go tablewriter库提升命令行输出专业度实例详解

    命令行工具大家都用过,如果是运维人员可能会编写命令行工具来完成各种任务,命令行输出的美观和易读性往往容易被忽视,很烂的输出会让人感觉不专业,本文将介绍Go语言中牛逼的实战工具tablewriter库,使你在命令行输出中展现出专业的一面
    2023-11-11
  • golang爬虫colly 发送post请求

    golang爬虫colly 发送post请求

    本文主要介绍了golang爬虫colly 发送post请求实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Golang发送Get和Post请求的实现

    Golang发送Get和Post请求的实现

    做第三方接口有时需要用Get或者Post请求访问,本文主要介绍了Golang发送Get和Post请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • web项目中golang性能监控解析

    web项目中golang性能监控解析

    这篇文章主要为大家介绍了web项目中golang性能监控详细的解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • 深入解析快速排序算法的原理及其Go语言版实现

    深入解析快速排序算法的原理及其Go语言版实现

    这篇文章主要介绍了快速排序算法的原理及其Go语言版实现,文中对于快速算法的过程和效率有较为详细的说明,需要的朋友可以参考下
    2016-04-04

最新评论