go学习 --- 客户管理系统

一、model层

package model

import "fmt"

//创建客户信息结构体
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  {
	customer := Customer{
		id,
		name,
		gender,
		age,
		phone,
		email,
	}
	return customer
}
提供工厂模式,返回Customer,不携带id
func NewCustomerNotID(name string,gender string,age int,phone string,email string) Customer  {
	customer := Customer{
		Name:name,
		Gender:gender,
		Age:age,
		Phone:phone,
		Email:email,
	}
	return customer
}
//返回用户信息
func (this Customer) GetInfo() string  {
	info := fmt.Sprintf("%v\t\t%v\t%v\t\t%v\t\t%v\t\t%v\t",this.Id,this.Name,this.Gender,this.Age,this.Phone,this.Email)
	return info
}

二、service

package service

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

//增删改查
type  CustomerService struct {
    Customers []model.Customer
    //表示当前切片有多少客户
    customerNum int
}
//创建一个工厂模式,返回CustomerService实例
func NewCustomerService() *CustomerService  {
    customerService := CustomerService{}
    customerService.customerNum = 1
    customer := model.NewCustomer(1, "老王", "男", 20, "666", "111111@qq.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查找客户下标,没有就返回一个-1
func (this *CustomerService) FindById(id int) int  {
    index := -1
    //遍历切片
    for key, _ := range this.Customers {
        if this.Customers[key].Id == id{
            index = key
        }
    }
    return index
}
//根据id删除客户
func (this *CustomerService) Delete(id int) bool {
    index := this.FindById(id)
    //判断index是否为-1
    if index == -1 {
        return false
    } else {
        this.Customers = append(this.Customers[:index],this.Customers[index+1:]...)
        return true
    }
}
//修改客户
func (this *CustomerService) Update(id int,customers model.Customer) bool {
      index := this.FindById(id)
    if index == -1 {
        return false
    } else {
        customer := this.Customers[index]

       fmt.Println(len(customers.Name))
        if len(customers.Name) == 0 {
            customers.Name = customer.Name
        }
        if len(customers.Gender) == 0 {
            customers.Gender =  customer.Gender
        }
        if customers.Age == 0 {
            customers.Age =  customer.Age
        }
        if len(customers.Phone) == 0 {
            customers.Phone =  customer.Phone
        }
        if len(customers.Email) == 0 {
            customers.Email =  customer.Email
        }
        customer = customers
        customer.Id = id
        this.Customers[index] = customer
        return true
    }
}

三、view

package main

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

type CustomerView struct {
	//用于菜单选项
	key string
	//用于退出程序
	loop bool
	//用于添加客户名
	name string
	//用于添加客户性别
	gender string
	//用于添加客户年龄
	age int
	//用于添加客户电话
	phone string
	//用于添加客户邮箱号
	email string
	//用于删除客户的id
	id int
	//用于删除客户的确认
	choice string
	customerService *service.CustomerService
}
//显示所有客户信息
func (this *CustomerView) List()  {
	list := this.customerService.List()
	fmt.Println("----------客户列表----------")
	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
	for key, _ := range list {
		fmt.Println(list[key].GetInfo())
	}
	fmt.Println("----------客户列表完成----------")
}
//添加用户
func (this *CustomerView) Add()  {
	fmt.Println("----------添加客户----------")
	fmt.Println("姓名:")
	fmt.Scanln(&this.name)
	fmt.Println("性别:")
	fmt.Scanln(&this.gender)
	fmt.Println("年龄:")
	fmt.Scanln(&this.age)
	fmt.Println("电话:")
	fmt.Scanln(&this.phone)
	fmt.Println("邮箱号:")
	fmt.Scanln(&this.email)
	//id是唯一的,由系统分配
	costomer := model.NewCustomerNotID(this.name,this.gender,this.age,this.phone,this.email)
	if this.customerService.Add(costomer){
		fmt.Println("----------添加成功----------")
	} else {
		fmt.Println("----------添加失败----------")
	}
}
//删除客户
func (this *CustomerView) Delete()  {
	fmt.Println("----------删除客户----------")
	fmt.Print("请选择要删除的客户编号(-1退出):")
	fmt.Scanln(&this.id)
	if this.id == -1 {
		return
	}
	//判断是否确认删除
	for  {
		fmt.Println("确认是否删除(y/n):")
		fmt.Scanln(&this.choice)
		if this.choice == "Y" || this.choice == "y"{
			if this.customerService.Delete(this.id){
				fmt.Println("----------删除成功----------")
				return
			}else {
				fmt.Println("----------删除失败,id不存在----------")
				return
			}
		} else if this.choice == "n" || this.choice == "N" {
			fmt.Println("成功取消删除")
			return
		} else {
			fmt.Println("请输入正确的选项")
		}
	}
}
//修改客户
func (this *CustomerView) Update()  {
	fmt.Println("----------修改客户----------")
	fmt.Print("请选择要修改的客户编号(-1退出):")
	fmt.Scanln(&this.id)
	if this.id == -1 {
		return
	}
	fmt.Println("姓名:")
	fmt.Scanln(&this.name)
	fmt.Println("性别:")
	fmt.Scanln(&this.gender)
	fmt.Println("年龄:")
	fmt.Scanln(&this.age)
	fmt.Println("电话:")
	fmt.Scanln(&this.phone)
	fmt.Println("邮箱号:")
	fmt.Scanln(&this.email)
	costomer := model.NewCustomerNotID(this.name,this.gender,this.age,this.phone,this.email)
	if this.customerService.Update(this.id,costomer){
		fmt.Println("----------修改成功----------")
		return
	}else {
		fmt.Println("----------修改失败,id不存在----------")
		return
	}
}
//退出客户系统
func (this *CustomerView) Exit() {
	for {
		fmt.Println("是否确认退出系统(y/n):")
		fmt.Scanln(&this.choice)
		if this.choice == "y" || this.choice == "Y" {
			fmt.Println("您退出客户信息管理软件")
			this.loop = false
			return
		} else if this.choice == "n" || this.choice == "N" {
			break
		} 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":
			this.Update()
		case "3":
			this.Delete()
		case "4":
			this.List()
		case "5":
		    this.Exit()
		default:
			fmt.Println("请输入正确的选项")
		}
		if this.loop == false {
			return
		}
	}
}
func main() {
	view := CustomerView{
		key:"",
		loop:true,
		name:"",
		gender:"",
		age:0,
		phone:"",
		id:-1,
		choice:"",
	}
	//对customerService初始化
	 view.customerService = service.NewCustomerService()
	 //显示主菜单
	view.mainMenu()
}

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一些指导。 首先,您需要确定客户管理系统需要哪些功能。这可能包括添加、编辑和删除客户信息,记录客户订单和交易历史记录,以及生成报告和分析数据等。 接下来,您可以使用Go语言编写客户管理系统。以下是可能用到的一些关键库和框架: 1. Gorilla Mux:用于构建RESTful API和处理HTTP请求路由的Web框架。 2. GORM:用于ORM(对象关系映射)的库,可以轻松地将Go结构体映射到数据库表。 3. JWT-go:用于生成和验证JWT(JSON Web Tokens)的库,可以帮助您实现用户身份验证和授权。 4. Gin:另一个轻量级的Web框架,可以帮助您快速构建API。 一旦您选择了所需的库和框架,您可以开始编写代码。以下是可能的代码结构: ``` - main.go - handlers/ - customer.go - models/ - customer.go - routes/ - customer.go - utils/ - database.go - auth.go ``` 在 `main.go` 中,您可以初始化Web服务器并加载路由。在 `handlers` 文件夹中,您可以编写处理客户请求的函数。在 `models` 文件夹中,您可以定义Go结构体,它们将映射到数据库表。在 `routes` 文件夹中,您可以定义路由和处理程序之间的映射。最后,在 `utils` 文件夹中,您可以编写用于操作数据库和处理身份验证的实用程序函数。 当您完成编写代码后,您可以将其部署到服务器上,并使用Postman或其他HTTP客户端测试API。 希望这些指导可以帮助您开始编写客户管理系统

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鸭鸭老板

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

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

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

打赏作者

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

抵扣说明:

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

余额充值