2.24 Go之信息管理系统
信息管理系统
基于文本界面的客户关系管理软件,该软件可以实现对客户的插入、修改和删除,并且可以打印客户信息明细表
组成模块
-
主模块--->
customerView
--->菜单的显示和处理用户操作 -
管理模块--->
customerService
--->提供增、删、改、查功能 -
用户结构体--->封装用户信息
项目目录结构
项目代码
Model层
package model
import "fmt"
/*
1、定义customer结构体
2、提供工厂构建结构体实例的函数
3、提供类似java的toString方法,获取结构体信息
*/
type Customer struct {
Id int
Name string
Sex string
Age int
Phone string
Email string
}
// 提供工厂构建customer结构体的函数
func NewCustomerByAll(id int, name string, sex string, age int, phone string, email string) Customer {
// 返回结构体信息
return Customer{
Id: id,
Name: name,
Sex: sex,
Age: age,
Phone: phone,
Email: email,
}
}
func NewCustomerByPart(name string, sex string, age int, phone string, email string) Customer {
// 返回结构体
return Customer{
Name: name,
Sex: sex,
Age: age,
Phone: phone,
Email: email,
}
}
// 提供获取结构体信息的函数
func (this Customer) GetInfo() string {
info := fmt.Sprintf("%v\t\t %v\t %v\t\t %v\t\t %v\t %v\t", this.Id, this.Name, this.Sex, this.Age, this.Phone, this.Email)
return info
}
Serview层
package service
import (
"GoInformationSys/InformationSys/model"
)
/*
提供对customer结构体的操作
增、删、改、查
*/
// 提供一个结构体,里面包含当前切片
type CustomerService struct {
// 声明一个字段,表示当前切片含有多少个客户--->用户结构体切片
customers []model.Customer
// 用户id
customerNum int
}
// 返回customerservice结构体指针,展示默认的数据
func NewCustomerService() *CustomerService {
// 初始化一个默认的客户
customerService := &CustomerService{}
// 设置默认用户id
customerService.customerNum = 1
// 设置默认用户信息--->调用customer包下的new函数
customer := model.NewCustomerByAll(1, "JunkingBoy", "Man", 22, "18154223156", "Lucifer@test.com")
// 将用户信息放到customerService切片中
customerService.customers = append(customerService.customers, customer)
// 返回customerService结构体
return customerService
}
// 获取customer切片的函数
func (this CustomerService) List() []model.Customer {
return this.customers
}
// 添加客户到customers切片中
func (this *CustomerService) Add(customer model.Customer) bool {
// 首先customerService的id自增
this.customerNum++
// 将传入的customerService的id设置为customer的id
customer.Id = this.customerNum
// 将形参放到切片中
this.customers = append(this.customers, customer)
return true
}
// 根据id查找客户--->没查到返回-1
func (this *CustomerService) FindById(id int) int {
index := -1
if id > 0 {
// 循环遍历切片
for i := 0; i < len(this.customers); i++ {
if this.customers[i].Id == id {
// 有该客户,找到该客户,返回对应的id
index = i
}
}
}
return index
}
// 根据id删除客户
func (this *CustomerService) Delete(id int) bool {
if 0 <