go 客户信息关系系统

项目需求分析

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

项目的界面设计

  • 主菜单界面

在这里插入图片描述

  • 添加客户界面

在这里插入图片描述

  • 修改客户界面

在这里插入图片描述

  • 删除客户界面

在这里插入图片描述

  • 客户列表界面

在这里插入图片描述

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

在这里插入图片描述

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

  • 功能的说明
    当用户运行程序时,可以看到主菜单,当输入5时,退出软件
    思路:
    编写customerView.go,另外把customer.go 和 customerService.go写上

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,
		name,
		gender,
		age,
		phone,
		email,
	}
}

customerManage/service/customerService.go

package service

import "go_code/customerManage/model"

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

customerManage/main/customerView.go

package main

import "fmt"

type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环
}

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()
}

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

  • 功能说明

在这里插入图片描述

思路:

在这里插入图片描述

customerManage/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,
		name,
		gender,
		age,
		phone,
		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
}

customerManage/service/customerService.go

package service

import "go_code/customerManage/model"

//CustomerService, 完成对Customer的操作,包括增删改查
type CustomerService struct {
	customers []model.Customer
	//声明一个字段,表示当前切片含有多少个客户
	//该字段后面,还可以作为新客户的id+1
	customerNum int
}
//编写一个方法,可以返回 *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
}

customManage/main/customerView.go

package main

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

type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环

	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].GetInfo())
	}
	fmt.Printf("\n-------------客户列表完成-------------\n\n")
}
//显示主菜单
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":
			this.list()
		case "5":
			this.loop = false
		default:
			fmt.Println("输入有误,请重新输入...")
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("退出了客户关系管理系统...")
}
func main()  {
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//这里完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.mainMenu()
}

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

  • 功能说明

在这里插入图片描述

思路:

在这里插入图片描述

customerManage/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,
		name,
		gender,
		age,
		phone,
		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
}
//第二种创建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,
	}
}

customerManage/service/customerService.go

package service

import "go_code/customerManage/model"

//CustomerService, 完成对Customer的操作,包括增删改查
type CustomerService struct {
	customers []model.Customer
	//声明一个字段,表示当前切片含有多少个客户
	//该字段后面,还可以作为新客户的id+1
	customerNum int
}
//编写一个方法,可以返回 *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
}
func (this *CustomerService) Add(customer model.Customer) bool {
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers, customer)
	return true
}

customerManage/main/customerView.go

package main

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

type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环

	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].GetInfo())
	}
	fmt.Printf("\n-------------客户列表完成-------------\n\n")
}
//得到用户的输入,信息构建新的客户,并完成添加
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实例
	customer := model.NewCustomer2(name, gender, age, phone, email)
	//调用
	if this.customerService.Add(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.Print("请选择(1-5): ")
		fmt.Scanln(&this.key)
		switch this.key {
		case "1":
			this.add()
		case "2":
			fmt.Println("修改客户")
		case "3":
			fmt.Println("删除客户")
		case "4":
			this.list()
		case "5":
			this.loop = false
		default:
			fmt.Println("输入有误,请重新输入...")
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("退出了客户关系管理系统...")
}
func main()  {
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//这里完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.mainMenu()
}

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

  • 功能说明

在这里插入图片描述

思路:

在这里插入图片描述

customerManage/service/customerService.go

package service

import "go_code/customerManage/model"

//CustomerService, 完成对Customer的操作,包括增删改查
type CustomerService struct {
	customers []model.Customer
	//声明一个字段,表示当前切片含有多少个客户
	//该字段后面,还可以作为新客户的id+1
	customerNum int
}
//编写一个方法,可以返回 *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
}
func (this *CustomerService) Add(customer model.Customer) bool {
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers, customer)
	return true
}
func (this *CustomerService) Delete(id int) bool {
	index := this.FindById(id)
	if index == -1 {
		return false
	}
	//从切片中删除一个元素
	this.customers = append(this.customers[:index], this.customers[index+1:]...)
	return true
}
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
}

customerManage/main/customerView.go

package main

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

type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环

	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].GetInfo())
	}
	fmt.Printf("\n-------------客户列表完成-------------\n\n")
}
//得到用户的输入,信息构建新的客户,并完成添加
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实例
	customer := model.NewCustomer2(name, gender, age, phone, email)
	//调用
	if this.customerService.Add(customer) {
		fmt.Println("-------------添加完成-------------")
	} else {
		fmt.Println("-------------添加失败------------ ")
	}
}
//得到用户额输入id,删除该id对应的客户
func (this *customerView) delete() {
	fmt.Println("---------------删除客户--------------")
	fmt.Println("请选择待删除客户编号(-1退出):")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	fmt.Println("确认是否删除(y/n): ")
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		if this.customerService.Delete(id) {
			fmt.Println("--------------删除完成-----------")
		} else {
			fmt.Println("----删除失败,输入的id号不存在----")
		}
	}
}
//显示主菜单
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.loop = false
		default:
			fmt.Println("输入有误,请重新输入...")
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("退出了客户关系管理系统...")
}
func main()  {
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//这里完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.mainMenu()
}

项目功能实现-完善退出确认功能

  • 功能说明:
    要求用户在退出时提示 “确认是否退出(Y/N)”,用户必须输入y/n,否则循环提示

customerManage/main/customerView.go

package main

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

type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环

	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].GetInfo())
	}
	fmt.Printf("\n-------------客户列表完成-------------\n\n")
}
//得到用户的输入,信息构建新的客户,并完成添加
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实例
	customer := model.NewCustomer2(name, gender, age, phone, email)
	//调用
	if this.customerService.Add(customer) {
		fmt.Println("-------------添加完成-------------")
	} else {
		fmt.Println("-------------添加失败------------ ")
	}
}
//得到用户额输入id,删除该id对应的客户
func (this *customerView) delete() {
	fmt.Println("---------------删除客户--------------")
	fmt.Println("请选择待删除客户编号(-1退出):")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	fmt.Println("确认是否删除(y/n): ")
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		if this.customerService.Delete(id) {
			fmt.Println("--------------删除完成-----------")
		} else {
			fmt.Println("----删除失败,输入的id号不存在----")
		}
	}
}
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
	}
}
//显示主菜单
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()  {
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//这里完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.mainMenu()
}

项目功能- 修改客户

customerManage/service/customerService.go

func (this *CustomerService) Update(customer model.Customer) bool {
	index := this.FindById(customer.Id)
	//index == -1 ,没有这个客户
	if index == -1 {
		return false
	}
	if customer.Name != "" {
		this.customers[index].Name = customer.Name
	}
	if customer.Gender != "" {
		this.customers[index].Gender = customer.Gender
	}
	if customer.Age != 0 {
		this.customers[index].Age = customer.Age
	}
	if customer.Phone != "" {
		this.customers[index].Phone = customer.Phone
	}
	if customer.Email != "" {
		this.customers[index].Email = customer.Email
	}
	return true
}

customerManage/main/customerView.go

func (this *customerView) update() {
	fmt.Println("------------修改客户------------")
	fmt.Println("请选择待修改客户编号(-1退出): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	index := this.customerService.FindById(id)
	if index == -1 {
		return
	}
	customers := this.customerService.List()
	fmt.Printf("姓名(%v): ", customers[index].Name)
	name := ""
	fmt.Scanln(&name)
	fmt.Printf("性别(%v): ", customers[index].Gender)
	gender := ""
	fmt.Scanln(&gender)
	fmt.Printf("年龄(%v): ", customers[index].Age)
	age := 0
	fmt.Scanln(&age)
	fmt.Printf("电话(%v): ", customers[index].Phone)
	phone := ""
	fmt.Scanln(&phone)
	fmt.Printf("电邮(%v): ", customers[index].Email)
	email := ""
	fmt.Scanln(&email)
	customer := model.NewCustomer(id,name, gender, age, phone, email)
	if this.customerService.Update(customer) {
		fmt.Println("------------修改完成------------")
	} else {
		fmt.Println("-----修改失败, 用户id不存在-----")
	}
}

整理

customerManage/model/customer.go

package model

import "fmt"

//声明一个Customer结构体,表示一个客户信息
type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

//编写一个工厂模式,返回一个Custermer实例
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,
	}
}

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",
		this.Id, this.Name, this.Gender, this.Age, this.Phone, this.Email)
	return info
}

customerManage/service/customerService.go

package service

import "go_code/customerManage/model"

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

//编写一个方法,可以返回 *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
}
//添加客户到customers切片
func (this *CustomerService) Add(customer model.Customer) bool {
	//分配id规则
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers, customer)
	return true
}
//根据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
}
func (this *CustomerService) Update(customer model.Customer) bool {
	index := this.FindById(customer.Id)
	//index == -1 ,没有这个客户
	if index == -1 {
		return false
	}
	if customer.Name != "" {
		this.customers[index].Name = customer.Name
	}
	if customer.Gender != "" {
		this.customers[index].Gender = customer.Gender
	}
	if customer.Age != 0 {
		this.customers[index].Age = customer.Age
	}
	if customer.Phone != "" {
		this.customers[index].Phone = customer.Phone
	}
	if customer.Email != "" {
		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
}

customerManage/main/customerView.go

package main

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

type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环显示
	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].GetInfo())
	}
	fmt.Println("\n------------客户列表完成------------\n\n")
}
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 := model.NewCustomer2(name, gender, age, phone, email)
	if this.customerService.Add(customer) {
		fmt.Println("------------添加完成------------")
	} else {
		fmt.Println("------------添加失败------------")
	}
}
func (this *customerView) delete() {
	fmt.Println("------------删除客户------------")
	fmt.Println("请选择待删除客户编号(-1退出): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	fmt.Println("确认是否删除(Y/N): ")
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		if this.customerService.Delete(id) {
			fmt.Println("------------删除完成------------")
		} else {
			fmt.Println("-----删除失败, 输入id号不存在----")
		}
	}
}
func (this *customerView) update() {
	fmt.Println("------------修改客户------------")
	fmt.Println("请选择待修改客户编号(-1退出): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	index := this.customerService.FindById(id)
	if index == -1 {
		return
	}
	customers := this.customerService.List()
	fmt.Printf("姓名(%v): ", customers[index].Name)
	name := ""
	fmt.Scanln(&name)
	fmt.Printf("性别(%v): ", customers[index].Gender)
	gender := ""
	fmt.Scanln(&gender)
	fmt.Printf("年龄(%v): ", customers[index].Age)
	age := 0
	fmt.Scanln(&age)
	fmt.Printf("电话(%v): ", customers[index].Phone)
	phone := ""
	fmt.Scanln(&phone)
	fmt.Printf("电邮(%v): ", customers[index].Email)
	email := ""
	fmt.Scanln(&email)
	customer := model.NewCustomer(id,name, gender, age, phone, email)
	if this.customerService.Update(customer) {
		fmt.Println("------------修改完成------------")
	} else {
		fmt.Println("-----修改失败, 用户id不存在-----")
	}
}

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
	}
}

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":
			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()
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Go构建一个前后端分离的客户关系管理系统,您可以按照以下步骤进行操作: 1. 设计数据库模式:首先,确定客户关系管理系统所需的数据模型,包括客户信息、联系人、交易记录等。使用适合您的需求的数据库(如MySQL、PostgreSQL等)创建相应的表结构。 2. 后端开发:使用Go语言编写后端服务,处理与数据库的交互以及业务逻辑。您可以使用框架如Gin或Echo来加速开发过程。实现API端点,以便前端可以通过HTTP请求与后端进行交互,包括创建、读取、更新和删除客户数据等操作。 3. 前端开发:使用现代化的前端框架(如React、Vue.js或Angular)开发用户界面。您可以使用前端框架提供的组件和工具来构建界面元素,以便用户可以浏览和操作客户关系数据。通过HTTP请求与后端API进行数据交互。 4. 跨域处理:由于前后端分离,前端和后端可能在不同的域中运行。为了解决浏览器的跨域限制,需要在后端实现跨域资源共享(CORS)机制,允许来自前端域的请求访问后端API。 5. 认证与授权:根据您的需求,实现用户认证和授权机制,确保只有经过身份验证的用户才能访问和修改客户关系数据。 6. 部署和测试:将后端服务部署到服务器上,并将前端部署到Web服务器或静态文件服务器上。确保系统能够正确运行,并进行全面的功能测试和性能测试。 以上仅为大致的步骤概述,具体的实现细节会根据您的需求和技术栈而有所不同。建议您参考相关的Go开发文档和示例代码,以及前端框架的官方文档和社区资源,来帮助您更好地构建前后端分离的客户关系管理系统
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxingge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值