• Go语言之客户关系管理系统


    一、需求分析

    该程序实现对客户的增、删、查、改功能。其主要的界面实现:

    • 主菜单界面
    ---------客户关系管理系统---------
                1 添加客户
                2 修改客户
                3 删除客户
                4 客户列表
                5 退   出
                请选择(1-5):
    • 添加客户界面
    ...
    请选择(1-5): 1
    ---------添加客户---------
    姓名:张三
    性别:男
    年龄:25
    电话:15269354825
    邮箱:zhangsan@xf.com
    ---------添加完成---------
    • 修改客户界面
    ...
    请选择(1-5): 2
    ---------修改客户---------
    请输入待修改的客户编号:1
    姓名:
    性别:
    年龄:
    电话:182693548258
    邮箱:zhangsan1@xf.com
    ---------修改完成---------
    • 删除客户界面
    ...
    请选择(1-5): 3
    ---------删除客户---------
    请输入待删除的客户编号:1
    确认是否删除(Y/N): y
    ---------删除完成---------
    • 客户列表界面
    ...
    请选择(1-5): 4 ---------客户列表--------- 编号 姓名 性别 年龄 电话 邮箱 1 张三 男 15 127536915 zhangsan@xf.com ---------客户列表完成------

    二、程序框架图

    在上面的需求中需要实现的功能有界面功能、业务逻辑(客户的增、删、查、改)、客户数据的模型:

    三、实现

    实现中先按照自下而上的顺序来实现,先定义customer结构体,然后再对结构体进行一系列的操作,最后通过前端客户来触发操作。

    •  项目结构
    ├─crm
    │  ├─model
    │  │      customer.go // 客户模型表
    │  │
    │  ├─service
    │  │      customerService.go // 业务逻辑操作
    │  │
    │  └─view
    │          customerView.go //页面控制
    │
    └─document
            menu.md //项目文档customer.go
    • customer.go
    package model
    
    import "fmt"
    
    // 声明一个customer结构体
    type Customer struct {
        Id     int
        Name   string
        Gender string
        Age    int
        Phone  string
        Email  string
    }
    
    // 定义一个结构体变量创建的工厂方法,其中Id让其自动生成, 生成的实例中id默认为0
    func CreateCustomer(name string, gender string, age int, phone string, email string) Customer {
        return Customer{
            Name:   name,
            Gender: gender,
            Age:    age,
            Phone:  phone,
            Email:  email,
        }
    }
    
    // 显示customer信息
    func (customer *Customer) ShowInfo() string {
        res := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t", customer.Id,
            customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email)
        return res
    
    }
    • customerService.go
    package service
    
    import (
        "go_tutorial/day15/crm/model"
    )
    
    type CustomerService struct {
        // 声明一个切片,用于存放customer
        CustomerSlice []model.Customer
    
        // 定义一个用于保存customer自增Id的变量,下一个Id就是Id+1
        CustomerNum int
    }
    
    // 向切片中添加customer
    func (cs *CustomerService) Add(customer model.Customer) bool {
        cs.CustomerNum++
        customer.Id = cs.CustomerNum
        cs.CustomerSlice = append(cs.CustomerSlice, customer)
        return true
    }
    
    // 查询切片中的customer
    func (cs *CustomerService) List() []model.Customer {
    
        return cs.CustomerSlice
    }
    
    // 查询切片中的编号
    func (cs *CustomerService) FindById(id int) int {
    
        for index, value := range cs.CustomerSlice {
            if value.Id == id {
                return index
            }
        }
        return -1
    }
    
    // 删除customer
    func (cs *CustomerService) Delete(id int) bool {
        index := cs.FindById(id)
        if index == -1 {
            // 说明不存在这个customer
            return false
        }
        cs.CustomerSlice = append(cs.CustomerSlice[:index], cs.CustomerSlice[index+1:]...)
        return true
    }
    
    // 获取一个customer
    func (cs *CustomerService) GetCustomer(id int) (model.Customer, bool) {
        index := cs.FindById(id)
        if index != -1 {
            return cs.CustomerSlice[index], true
        }
        return model.Customer{}, false
    }
    
    // 修改customer
    func (cs *CustomerService) Update(id int, name string, gender string, age int, phone string, email string) bool {
        index := cs.FindById(id)
        if index == -1 {
            // 说明不存在这个customer
            return false
        }
        cs.CustomerSlice[index].Name = name
        cs.CustomerSlice[index].Gender = gender
        cs.CustomerSlice[index].Age = age
        cs.CustomerSlice[index].Phone = phone
        cs.CustomerSlice[index].Email = email
        return true
    }
    • customerView.go
    package main
    
    import (
        "fmt"
        "go_tutorial/day15/crm/model"
        "go_tutorial/day15/crm/service"
    )
    
    type customerView struct {
        key             string //获取客户端的输入
        forloop         bool   // 判断 是否跳出for循环
        serviceCustomer service.CustomerService
    }
    
    // 主页面中添加customer,调用customerService中的Add方法
    func (cv *customerView) add() {
        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.CreateCustomer(name, gender, age, phone, email)
        if cv.serviceCustomer.Add(customer) {
            fmt.Println("---------添加完成---------")
        } else {
            fmt.Println("---------添加失败---------")
        }
    
    }
    
    // 主页面中查看customer,调用customerService中的List方法
    func (cv *customerView) list() {
        fmt.Println("---------客户列表---------")
        fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
        customers := cv.serviceCustomer.List()
        for i := 0; i < len(customers); i++ {
            fmt.Println(customers[i].ShowInfo())
        }
        fmt.Println("-------客户列表完成-------")
    }
    
    // 主页面中更新customer,调用customerService中的Update方法
    func (cv *customerView) update() {
        fmt.Println("---------修改客户---------")
        fmt.Println("请输入待修改的客户编号:")
        Id := -1
        fmt.Scanln(&Id)
        // 这里需要判断是否存在,如果存在获取当前customer
        customer, flag := cv.serviceCustomer.GetCustomer(Id)
        if !flag {
            return
        }
        name := customer.Name
        fmt.Println("姓名:")
    
        fmt.Scanln(&name)
        gender := customer.Gender
        fmt.Println("性别:")
    
        fmt.Scanln(&gender)
        age := customer.Age
        fmt.Println("年龄:")
    
        fmt.Scanln(&age)
        phone := customer.Phone
        fmt.Println("电话:")
        fmt.Scanln(&phone)
        email := customer.Email
        fmt.Println("邮箱:")
        fmt.Scanln(&email)
        flag1 := cv.serviceCustomer.Update(Id, name, gender, age, phone, email)
        if flag1 {
            fmt.Println("---------修改完成---------")
        } else {
            fmt.Println("---------修改失败---------")
        }
    
    }
    
    // 主页面中删除customer,调用customerService中的Delete方法
    func (cv *customerView) delete() {
        fmt.Println("---------删除客户---------")
        fmt.Println("请输入待删除的客户编号:")
        Id := -1
        fmt.Scanln(&Id)
        cv.serviceCustomer.Delete(Id)
        fmt.Println("---------删除完成---------")
    
    }
    
    // 主页面退出循环,通过修改forloop状态,退出循环
    func (cv *customerView) exit() {
        fmt.Println("请确认是否退出(Y/N):")
        for {
            fmt.Scanln(&cv.key)
            if cv.key == "Y" || cv.key == "y" || cv.key == "N" || cv.key == "n" {
                break
            }
            fmt.Println("你的输入有误,请重新输入(Y/N):")
        }
        if cv.key == "Y" || cv.key == "y" {
            cv.forloop = false
        }
    
    }
    
    // 主页面
    func (cv *customerView) mainMenu() {
        for {
            fmt.Println("---------客户关系管理系统---------")
            fmt.Println("            1 添加客户")
            fmt.Println("            2 修改客户")
            fmt.Println("            3 删除客户")
            fmt.Println("            4 客户列表")
            fmt.Println("            5 退   出")
            fmt.Println("            请选择(1-5):")
    
            // 获取用户输入
            fmt.Scanln(&cv.key)
    
            switch cv.key {
    
            case "1":
                cv.add()
            case "2":
                cv.update()
            case "3":
                cv.delete()
            case "4":
                cv.list()
            case "5":
                cv.exit()
            default:
                fmt.Println("你的输入有误,请重新输入...")
            }
    
            if !cv.forloop {
                break
            }
    
        }
    
    }
    
    // 主方法,初始化变量
    func main() {
        cs := service.CustomerService{CustomerNum: 0}
        cv := customerView{
            key:             " ",
            forloop:         true,
            serviceCustomer: cs,
        }
    
        cv.mainMenu()

    在该文件下执行go run customerView.go命令即可。

  • 相关阅读:
    win7-64位,vs32位,odbc 连接oracle问题总结
    vscode 格式化代码
    vue 自动切换导航图
    Unexpected console statement
    css flex 布局之---骰子
    vue百度地图在IE11下空白
    css使用font-face
    centos7计划任务
    MySQL(Mariadb)主从同步基础
    Ubuntu(16.04) 常见问题
  • 原文地址:https://www.cnblogs.com/shenjianping/p/15830537.html
Copyright © 2020-2023  润新知