第一个golang小项目--客户管理

参考韩顺平老师教程,自己摸索出来的适合go入门的mvc小项目。

文件结构如下:

main.go为入口文件,modelCustomer.go,customerView.go,serviceCustomer.go分别为mvc文件。

main.go:

package main

import (

    "learn/customerManage/view"

)

 

func main() {

        view.GetCustomerView().MainMenu()

}

modelCustomer.go:

package model

type Customer struct {

    Id int

    Name string

    Sex string

    Age int

    Phone string

    Email string

}

 

func (this *Customer) Add(name string, sex string, age int, phone string, email string) *Customer {

        this.Name = name

        this.Sex = sex

        this.Age = age

        this.Phone = phone

        this.Email = email

        return this

}

func (this *Customer) AddWithInt(id int, name string, sex string, age int, phone string, email string) *Customer {

    this.Id = id

    this.Name = name

    this.Sex = sex

    this.Age = age

    this.Phone = phone

    this.Email = email

    return this

}

customerView.go:

package view

 

import (

    "fmt"

    "learn/customerManage/service"

)

 

type CustomerView struct {

    key int

    service *service.ServiceCustomer

}

func  GetCustomerView() CustomerView {

    return CustomerView{}

}

func (this *CustomerView) Edit() {

    id := 0

    name := ""

    age := 0

    sex := ""

    phone := ""

    email := ""

    this.List()

    fmt.Println("请输入要编辑的客户id")

    fmt.Scanln(&id)

    fmt.Println("请输入姓名")

    fmt.Scanln(&name)

    fmt.Println("请输入年龄")

    fmt.Scanln(&age)

    fmt.Println("请输入性别")

    fmt.Scanln(&sex)

    fmt.Println("请输入手机")

    fmt.Scanln(&phone)

    fmt.Println("请输入邮箱")

    fmt.Scanln(&email)

    this.service.Edit(id, name, sex, age, phone, email)

    fmt.Println("编辑成功")

}

 

func (this *CustomerView) Del() {

    this.List()

    fmt.Println("请输入要删除的顾客id")

    id := 0

    fmt.Scanln(&id)

    this.service.Del(id)

    fmt.Println("删除成功")

}

func (this *CustomerView) Add() {

    name := ""

    age := 0

    sex := ""

    phone := ""

    email := ""

    fmt.Println("请输入姓名")

    fmt.Scanln(&name)

    fmt.Println("请输入年龄")

    fmt.Scanln(&age)

    fmt.Println("请输入性别")

    fmt.Scanln(&sex)

    fmt.Println("请输入手机")

    fmt.Scanln(&phone)

    fmt.Println("请输入邮箱")

    fmt.Scanln(&email)

    this.service.Add(name, sex, age, phone, email)

    fmt.Println("新增成功!")

}

func (this *CustomerView) List() {

    list := this.service.List()

    fmt.Println("Id\tName\tSex\tAge\tPhone\tEmail")

    for _, customer := range list {

        fmt.Printf("%v\t%v\t%v\t%v\t%v\t%v\n", customer.Id, customer.Name, customer.Sex, customer.Age, customer.Phone, customer.Email)

    }

}

 

func (this CustomerView) MainMenu() {

    service := service.GetServiceCustomer()

    this.service = service

    labelBreak:

    for {

        fmt.Println("-------------客户信息管理软件--------------")

        fmt.Println("             1 添加客户")

        fmt.Println("             2 修改客户")

        fmt.Println("             3 删除客户")

        fmt.Println("             4 客户列表")

        fmt.Println("             5 退   出")

        fmt.Println("请选择:1-5")

        fmt.Scanln(&this.key)

        switch this.key {

            case 1 :

                this.Add()

            case 2 :

                this.Edit()

            case 3 :

                this.Del()

            case 4 :

                this.List()

            case 5 :

                break labelBreak

            default :

                fmt.Println("输入有误。。。")

        }

    }

 

}

serviceCustomer.go:

package service

 

import (

    "learn/customerManage/model"

    "fmt"

)

type ServiceCustomer struct {

    CustomerNum int

    Customers []*model.Customer

}

func GetServiceCustomer() *ServiceCustomer {

    return &ServiceCustomer{CustomerNum : 0}

}

func (this *ServiceCustomer) Edit(id int, name string, sex string, age int, phone string, email string) {

    boo := false

    for _, customer := range this.Customers {

        if customer.Id == id {

            boo = true

            this.Del(id)

        }

    }

    if !boo {

        fmt.Println("请检查输入的客户id,未找到相关客户")

        return

    }

    this.AddWithInt(id, name , sex, age, phone, email)

}

func (this *ServiceCustomer) Del(id int) {

    var customers []*model.Customer

    for _, customer := range this.Customers {

        if customer.Id != id {

            customers = append(customers, customer)

        }

    } 

    this.Customers = customers

}

 

func (this *ServiceCustomer) AddWithInt(id int, name string, sex string, age int, phone string, email string) {

    customer := &model.Customer{}

    customer.AddWithInt(id, name, sex, age, phone, email)

    this.Customers = append(this.Customers, customer)

}

func (this *ServiceCustomer) Add(name string, sex string, age int, phone string, email string) {

    this.CustomerNum += 1

    customer := model.Customer{

        Id : this.CustomerNum,

    }

    cusNew := (&customer).Add(name, sex, age, phone, email)

    this.Customers = append(this.Customers, cusNew)

}

func (this *ServiceCustomer) List() []*model.Customer {

    if this.CustomerNum < 1 {//没有数据添加一行

        this.CustomerNum = 1

        customer := &model.Customer{

            Id : this.CustomerNum,

            Name : "zdd",

            Sex : "man",

            Age : 28,

            Phone : "15900000000",

            Email : "[email protected]",

        }

        this.Customers = append(this.Customers, customer)

    }

    return this.Customers

}

终端效果如下:

发布了29 篇原创文章 · 获赞 6 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章