golang小案例学习--客户管理系统

2020-01-10  本文已影响0人  韩小禹
项目结构:
      --- models
            --- customer.go
      --- service
            --- customerService.go
      --- views
            --- customerView.go
* models 数据模型
* service 业务逻辑
* views 页面展示
package models

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 {
    return Customer{
        Id:     id,
        Name:   name,
        Gender: gender,
        Age:    age,
        Phone:  phone,
        Email:  email,
    }
}

//
func NewCustomers(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) UserInfo() string {
    return 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)
}

package service

import "customerManage/models"

type CustomerService struct {
    Customer    []models.Customer
    customerInt int //表示有多少个用户,同时可以作为新用户的ID号码
}

// 编写一个方法,可以返回 *CustomerService
func NewCustomerService() *CustomerService {
    return &CustomerService{}
}

// 更新用户信息
func (this *CustomerService) Update(id int, name string, gender string, age int, phone string, email string) bool {
    for k, v := range this.Customer {
        if v.Id == id {
            this.Customer[k].Name = name
            this.Customer[k].Gender = gender
            this.Customer[k].Age = age
            this.Customer[k].Phone = phone
            this.Customer[k].Email = email
        }
    }
    return true
}

// 编写一个方法,可以返回客户切片
func (this *CustomerService) List() []models.Customer {
    return this.Customer
}

// 添加客户
func (this *CustomerService) Add(customer models.Customer) bool {
    this.customerInt++
    customer.Id = this.customerInt
    this.Customer = append(this.Customer, customer)
    return true
}

// 删除客户
func (this *CustomerService) Delete(id int) bool {
    index := this.FindById(id)
    if index == -1 {
        return false
    } else {
        this.Customer = append(this.Customer[:index], this.Customer[index+1:]...)
        return true
    }
}

// 查找客户
func (this *CustomerService) FindById(id int) int {
    index := -1
    for k, v := range this.Customer {
        if v.Id == id {
            index = k
            break
        }
    }
    return index
}

// 返回用户切片
func (this *CustomerService) UserInfoById(id int) models.Customer {
    return this.Customer[id-1]
}

package main

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

type customerView struct {
    key             string //用于接收用户的输入
    loop            bool   //用于判断用户是否退出
    customerService *service.CustomerService
}

// 定一个一个方法,用于显示主菜单
func (this *customerView) mainMenu() {
    for {
        fmt.Println("*****---------- 客户管理系统 ----------*****")
        fmt.Println("*****---------- 1 添加客户 ----------*****")
        fmt.Println("*****---------- 2 修改客户 ----------*****")
        fmt.Println("*****---------- 3 删除客户 ----------*****")
        fmt.Println("*****---------- 4 客户列表 ----------*****")
        fmt.Println("*****---------- 5 退   出 ----------*****")
        fmt.Println("*****---------- 请选择 ----------*****")

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

        if !this.loop {
            break
        }
    }
}

// 更新用户
func (this *customerView) update() {
    fmt.Println("请输入用户ID")
    id := 0
    fmt.Scanln(&id)
    if this.customerService.FindById(id) == -1 {
        fmt.Println("抱歉,用户不存在")
    } else {
        infos := this.customerService.UserInfoById(id)
        fmt.Printf("姓名(%s):(回车键表示跳过)\n", infos.Name)
        name := ""
        fmt.Scanln(&name)
        fmt.Printf("性别(%s):(回车键表示跳过)\n", infos.Gender)
        gender := ""
        fmt.Scanln(&gender)
        fmt.Printf("年龄(%d):(回车键表示跳过)\n", infos.Age)
        age := 0
        fmt.Scanln(&age)
        fmt.Printf("电话(%s):(回车键表示跳过)\n", infos.Phone)
        phone := ""
        fmt.Scanln(&phone)
        fmt.Printf("邮箱(%s):(回车键表示跳过)\n", infos.Email)
        email := ""
        fmt.Scanln(&email)
        if this.customerService.Update(id, name, gender, age, phone, email) {
            fmt.Println("更新成功")
        } else {
            fmt.Println("更新失败")
        }
    }
}

// 退出系统
func (this *customerView) quit() {
    fmt.Println("确定要退出系统么?(y / n)")
    for {
        choise := ""
        fmt.Scanln(&choise)
        if choise == "y" || choise == "Y" {
            this.loop = false
            break
        }
        if choise == "n" || choise == "N" {
            break
        }
        fmt.Println("输入有误,请重新输入。(y / n)")
    }
}

// 删除客户
func (this *customerView) delete() {
    fmt.Println("请输入客户ID(-1退出):")
    id := -1
    fmt.Scanln(&id)
    if id == -1 {
        return
    } else {
        for {
            fmt.Println("确认删除该客户么? y / n")
            choise := ""
            fmt.Scanln(&choise)
            if choise == "y" || choise == "Y" {
                if this.customerService.Delete(id) {
                    fmt.Println("删除成功")
                } else {
                    fmt.Println("没有该客户")
                }
                break
            } else if choise == "n" || choise == "N" {
                break
            }
        }
    }
}

//显示所有的客户信息
func (this *customerView) List() {
    fmt.Print("\n\n")
    fmt.Println("*****---------- 客户列表 ----------*****...")
    fmt.Println("ID\t姓名\t性别\t年龄\t电话\t邮箱")
    for _, v := range this.customerService.List() {
        fmt.Println(v.UserInfo())
    }
    fmt.Println("*****---------- 客户列表完成 ----------*****...")
    fmt.Print("\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)

    new_customer := models.NewCustomers(name, gender, age, phone, email)
    if this.customerService.Add(new_customer) {
        fmt.Println("*****---------- 添加完成 ----------*****")
    } else {
        fmt.Println("*****---------- 添加失败 ----------*****")
    }
}

// view文件夹 负责视图
// service文件夹 负责业务逻辑
// models文件夹 负责数据
func main() {
    var customerView = customerView{
        key:             "",
        loop:            true,
        customerService: service.NewCustomerService(),
    }
    customerView.mainMenu()
}

image.png
上一篇下一篇

猜你喜欢

热点阅读