Go语言 客户信息关系系统

该项目旨在实现一个基于文本界面的客户信息管理系统,使用Go语言开发。系统包括添加、修改和删除客户信息功能,并能打印客户明细表。设计了Customer结构体,通过工厂模式创建实例,并在service层的CustomerService中管理Customer的切片,实现了增删改查操作。customerView负责显示主菜单和处理用户交互,提供显示客户列表的功能。
摘要由CSDN通过智能技术生成
  • 项目需求分析

模拟实现基于文本界面的《客户信息管理软件

②该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表

  • 项目界面设计

主菜单界面

添加客户界面

修改客户界面

客户列表界面

删除客户界面

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

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

功能说明:当用户运行程序时,可以看到主菜单,当输入 5 时,可以退出该软件

思路分析:编写 customerView.go ,另外可以把 customer.go customerService.go 写上

首先从model层开始写,在model层中主要是声明一个customer结构体

type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

将来这个结构体会在customerService中使用,所以我们应该在这个结构体中提供一个工厂模式的方法,返回一个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,
	}
}

service层先写一个大体的结构,在该层中明一个CustomerService结构体,完成对customer的操作,包括增删改查,因为要对customer进行操作,CustomerService结构体中要有一个字段,该字段是一个customer切面,把所有管理的customer信息保存到切片中,在切片中完成增删改查,同时在该结构体中需要声明一个字段表示当前切片含有多少个客户,该字段后面还可以作为新客户的 id+1

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

接下来就是编写customerView,如果我们想要把显示功能封装到customerView中去,我们就要定义一个结构体,然后编写显示主菜单功能,主体还是和上个案例一个,使用for循环和switch结构,又因为我们需要一个变量来接收输入的1-5,所以在上面的结构体中定义一个字段,还需要一个loop字段表示是否循环的显示主菜单,该方法是和customerView类型的this变量绑定的

type customerView struct {
	//定义必要字段
	key string //接收用户输入...
	loop bool //表示是否循环的显示主菜单
}
//显示主菜单
func (this *customerView){
	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("你退出了客户关系管理系统...")
}

我们想要在主函数中调用该方法首先需要将结构体实例化,就是创建一个 customerView,通过它来调用并运行显示主菜单方法

customerView := customerView{
		key : "", 
		loop : true, 
	}
		//显示主菜单.. 
	customerView.mainMenu()

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

功能说明:

 思路分析:

我们想要完成此功能首先要在customerService中写一个List方法,该方法能够将客户的信息显示出来,即返回客户信息(切片),然后在customerView中编写一个list方法调用customerService中的List方法并显示客户列表,我们需要customerView中包含customerService的字段,通过该字段调用List方法,因此我们需要在customerService中编写一个NewCustomService方法返回一个customerService实例。

代码实现:

为了能够看到有客户在切片中,我们初始化一个客户,然后创建一个客户加到切片中去,通过append方法

func NewCustomerService() *CustomerService {
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customers := NewCustomer{1, "张山", "男", 20, "112", "244957@qq.com"}
	customerService.customers = append(customerService.customers, customers)
	return customerService
}

我们还需要编写List方法返回客户切片

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

然后继续编写customerView文件,因为我要通过customerService文件中的方法来调用后面的修改客户信息的操作,因此customerView中要含有customerService的字段,我们选择在customerView结构体中增加customerService的字段

customerService *service.customerService

并在main方法中对该字段初始化

customerView.customerService = service.NewCustomerService()

然后我们在customerView文件中编写一个list方法调用customerService中的List方法并显示客户信息,那list方法如何实现呢,我们只要获取存储客户信息的切片即可,为了方便打印我们在customer文件中编写一个GetInfo方法显示用户信息

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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值