• golang实战--客户信息管理系统


    总计架构图:

    model/customer.go

    package model
    
    import (
        "fmt"
    )
    
    type Customer struct {
        Id     int
        Name   string
        Gender string
        Age    int
        Phone  string
        Email  string
    }
    
    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 NewCustomer2(name string, gender string, age int, phone string, email string) Customer {
        return Customer{
            Name:   name,
            Gender: gender,
            Age:    age,
            Phone:  phone,
            Email:  email,
        }
    }
    
    func (c Customer) GetInfo() string {
        info := fmt.Sprintf("%v	%v	%v	%v	%v	%v	", c.Id, c.Name, c.Gender, c.Age, c.Phone, c.Email)
        return info
    }

    service/customerService.go

    package service
    
    import (
        "go_code/project_6/model"
    )
    
    //增删查改
    type CustomerService struct {
        customers  []model.Customer
        customerId int
    }
    
    func NewCustomerService() *CustomerService {
        customerService := &CustomerService{}
        customerService.customerId = 1
        customer := model.NewCustomer(1, "张三", "", 20, "15927776543", "347688971@qq.com")
        customerService.customers = append(customerService.customers, customer)
        return customerService
    }
    
    func (cs *CustomerService) List() []model.Customer {
        return cs.customers
    }
    
    func (cs *CustomerService) Add(customer model.Customer) bool {
        //确定一个添加id的规则,就是添加的顺序
        cs.customerId++
        customer.Id = cs.customerId
        cs.customers = append(cs.customers, customer)
        return true
    }
    
    //根据Id查找客户在切片中的对应下标,如果没有则返回-1
    func (cs *CustomerService) FindById(id int) int {
        index := -1
        for i := 0; i < len(cs.customers); i++ {
            if cs.customers[i].Id == id {
                //找到了
                index = i
            }
        }
        return index
    }
    
    //根据Id删除客户
    func (cs *CustomerService) Delete(id int) bool {
        index := cs.FindById(id)
        if index == -1 {
            return false
        }
        //从切片中删除元素
        cs.customers = append(cs.customers[:index], cs.customers[index+1:]...)
        return true
    }
    
    func (cs *CustomerService) GetinfoById(id int) model.Customer {
        i := id - 1
        return cs.customers[i]
    }
    
    //根据id修改客户信息
    func (cs *CustomerService) Update(id int, customer model.Customer) bool {
        for i := 0; i < len(cs.customers); i++ {
            if cs.customers[i].Id == id {
                cs.customers[i].Name = customer.Name
                cs.customers[i].Gender = customer.Gender
                cs.customers[i].Age = customer.Age
                cs.customers[i].Phone = customer.Phone
                cs.customers[i].Email = customer.Email
            }
        }
        return true
    }

    view/customerView.go

    package main
    
    import (
        "fmt"
        "go_code/project_6/model"
        "go_code/project_6/service"
    )
    
    type customerView struct {
        //定义必要的字段
        key             string //接受用户输入
        loop            bool   //用于循环判断
        customerService *service.CustomerService
    }
    
    func (cv *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(&cv.key)
            switch cv.key {
            case "1":
                cv.add()
            case "2":
                cv.update()
            case "3":
                cv.delete()
            case "4":
                cv.list()
            case "5":
                cv.logout()
            default:
                fmt.Println("你的输入有误,请重新输入")
            }
            if !cv.loop {
                break
            }
        }
        fmt.Println("你已退出了客户关系管理系统。。。")
    }
    
    func (cv *customerView) list() {
        //首先获取当前客户所有信息
        customers := cv.customerService.List()
        fmt.Println("--------------------客户列表--------------------------")
        fmt.Println("编号	姓名	性别	年龄	电话		邮箱")
        for i := 0; i < len(customers); i++ {
            fmt.Println(customers[i].GetInfo())
        }
        fmt.Println("------------------客户列表完成------------------------")
    }
    
    func (cv *customerView) add() {
        fmt.Println("--------------------添加客户--------------------------")
        fmt.Print("姓名:")
        name := ""
        fmt.Scanln(&name)
        fmt.Print("性别:")
        gender := ""
        fmt.Scanln(&gender)
        fmt.Print("年龄:")
        age := 0
        fmt.Scanln(&age)
        fmt.Print("电话:")
        phone := ""
        fmt.Scanln(&phone)
        fmt.Print("邮箱:")
        email := ""
        fmt.Scanln(&email)
        customer := model.NewCustomer2(name, gender, age, phone, email)
        if cv.customerService.Add(customer) {
            fmt.Println("--------------------添加成功--------------------------")
        } else {
            fmt.Println("--------------------添加失败--------------------------")
        }
    
        fmt.Println("--------------------添加完成--------------------------")
    }
    
    func (cv *customerView) delete() {
        fmt.Println("--------------------删除客户--------------------------")
        fmt.Println("请选择要删除客户的客户编号(-1退出):")
        id := -1
        fmt.Scanln(&id)
        if id == -1 {
            return
        }
        fmt.Println("确认是否删除?y/n")
        choice := ""
        fmt.Scanln(&choice)
        if choice == "y" || choice == "n" {
            if cv.customerService.Delete(id) {
                fmt.Println("--------------------删除完成--------------------------")
            } else {
                fmt.Println("-----------------输入id不存在,请重新输入--------------")
            }
        }
    
    }
    
    func (cv *customerView) update() {
        fmt.Print("请输入要修改的id:")
        id := 0
        fmt.Scanln(&id)
        if cv.customerService.FindById(id) != -1 {
            customer := cv.customerService.GetinfoById(id)
            fmt.Printf("姓名(%v:)", customer.Name)
            name := ""
            fmt.Scanln(&name)
            fmt.Printf("性别(%v):", customer.Gender)
            gender := ""
            fmt.Scanln(&gender)
            fmt.Printf("年龄(%v):", customer.Age)
            age := 0
            fmt.Scanln(&age)
            fmt.Printf("电话(%v):", customer.Phone)
            phone := ""
            fmt.Scanln(&phone)
            fmt.Printf("邮箱(%v):", customer.Email)
            email := ""
            fmt.Scanln(&email)
            customer2 := model.NewCustomer2(name, gender, age, phone, email)
            cv.customerService.Update(id, customer2)
        } else {
            fmt.Println("-----------------输入id不存在,请重新输入--------------")
        }
    }
    
    func (cv *customerView) logout() {
        fmt.Println("确认是否退出?y/n")
        for {
            fmt.Scanln(&cv.key)
            if cv.key == "y" || cv.key == "n" {
                break
            }
            fmt.Println("你的输入有误,请从新输入")
        }
        if cv.key == "y" {
            cv.loop = false
        }
    }
    
    func main() {
        var cv customerView
        cv.loop = true
        cv.customerService = service.NewCustomerService()
        cv.mainMenu()
    }

    由于代码都比较基础,就不一一介绍了,很容易看懂。我们运行程序:

    先选择4:我们已经初始化了一条数据。

    再选择1添加一条数据:

    再选择4查看一下:

    数据确实已经添加成功,我们再选择3,删除一条数据:

     再查看一下:

    确实已经删除,然后我们选择2修改数据:

    再查看一下:

    已经修改了,最后我们选择5进行退出:

    总结:通过golang实现的客户信息管理系统。学习一门语言最好的方式就是通过一个实际的例子。通过这个实例,不仅可以进一步巩固golang的相关基础技能,同时,也能让我们加强自己的逻辑能力,从一步步的调用函数,掌握参数传递和接收技巧。

  • 相关阅读:
    JavaScript实现文本框和密码框placeholder效果(兼容ie8)
    11.24 模拟赛题解
    一句话题解集——口胡万岁
    uTools-插件化定制属于自己的工具集[免费]
    tree
    braintree 支付
    Shopify 接口调用
    TcPlayer腾讯播放器
    微信支付(WeixinJSBridge.invoke、wx.chooseWXPay)
    图片上传(二进制文件流)
  • 原文地址:https://www.cnblogs.com/xiximayou/p/11934896.html
Copyright © 2020-2023  润新知