go 实战小项目

2020-03-28  本文已影响0人  乔大叶_803e

客户管理软件

代码实现
customer/model/customer.go

package model

//声明一个customer 结构体  表示一个客户信息

type Customer struct {
    Id int
    Name string
    Gender string
    Age     int
    Phone   string
    Email   string
}

//编写一个工厂模式,返回一个customerd的实例

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,
    }
}

customer/service/customerservice.go

package service

import "awesomeProject/customer/model"

//该实例 完成对 customer的操作  包括增删改查

type CustomerService struct {
    customers []model.Customer
    //声明一个字段  表示当前切片含有多少个客户
    //该字段后面,还可以作为新客户的id + 1
    coustmerNum int


}

customer/view/cutomerview.go

package main

import "fmt"

type customerView struct {
    //定义必要的字段
    key string //接受用户输入.....
    loop bool // 表示是否循环的显示菜单
}

// 显示主菜单
func (this *customerView) mainView(){
    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":
                fmt.Println("添加客户")
        case "2":
            fmt.Println("修改客户")
        case "3":
            fmt.Println("删除客户")
        case "4":
            fmt.Println("客户列表")
        case "5":
            this.loop = false
        default:
            fmt.Println("你的输入有误,请从新输入")
        }
        if !this.loop {
            break
        }
    }
    fmt.Println("已经退出")
}

func main()  {
    //在主函数中创建  创建一个customerView,并运行显示主菜单
    customerView := customerView{
        key:"",
        loop:true,
    }
    //显示菜单1
    customerView.mainView()
}
2 项目功能的实现-完成显示客户列表的功能
//返回用户信息  格式化字符串
func (this *Customer)GetInfo()  string{
    info := 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)
    return info
}

customer/service/customerservice.go

//编写一个方法,可以返回 *CustomerService
func NewCustomerService() *CustomerService{
    customerservice := &CustomerService{}
    customerservice.coustmerNum = 1
    customer  := model.NewCustomer(1,"张三","男",20,"121","zhangshang@xinlaing.com")
    customerservice.customers = append(customerservice.customers,customer)
    return customerservice
}

//返回客户切片

func (this *CustomerService)List() []model.Customer  {
    return this.customers
}

customer/view/cutomerview.go

package main

import (
    "awesomeProject/customer/service"
    "fmt"
)

type customerView struct {
    //定义必要的字段
    key string //接受用户输入.....
    loop bool // 表示是否循环的显示菜单
    //增加一个字段 customerservice

     customerService   *service.CustomerService
}
//显示所有的客户信息
func (this *customerView)list()  {
    //首先,获得当前所有客户信息(在切片中)
    customers := this.customerService.List()
    fmt.Println("-------------------客户列表--------------------")
    fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
    for i:=0;i<len(customers) ;i++  {
        //fmt.Println(customers[i].Id,"\t",customers[i].Name)
        fmt.Println(customers[i].GetInfo())
    }
    fmt.Println("-----------------客户列表完成-------------------")
}


// 显示主菜单
func (this *customerView) mainView(){
    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":
                fmt.Println("添加客户")
        case "2":
            fmt.Println("修改客户")
        case "3":
            fmt.Println("删除客户")
        case "4":
            //fmt.Println("客户列表")
            this.list()
        case "5":
            this.loop = false
        default:
            fmt.Println("你的输入有误,请从新输入")
        }
        if !this.loop {
            break
        }
    }
    fmt.Println("已经退出")
}

func main()  {
    //在主函数中创建  创建一个customerView,并运行显示主菜单
    customerView := customerView{
        key:"",
        loop:true,
    }
    //这里完成对customerView结构体的customerService字段的初始化
    customerView.customerService = service.NewCustomerService()
    //显示菜单1
    customerView.mainView()
}

添加客户功能
思路分析

customer/model/customer.go

package model

import "fmt"

//声明一个customer 结构体  表示一个客户信息

type Customer struct {
    Id int
    Name string
    Gender string
    Age     int
    Phone   string
    Email   string
}

//编写一个工厂模式,返回一个customerd的实例

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 (this *Customer)GetInfo()  string{
    info := 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)
    return info
}

customer/service/customerservice.go

package service

import "awesomeProject/customer/model"

//该实例 完成对 customer的操作  包括增删改查

type CustomerService struct {
    customers []model.Customer
    //声明一个字段  表示当前切片含有多少个客户
    //该字段后面,还可以作为新客户的id + 1
    coustmerNum int


}

//编写一个方法,可以返回 *CustomerService
func NewCustomerService() *CustomerService{
    customerservice := &CustomerService{}
    customerservice.coustmerNum = 1
    customer  := model.NewCustomer2("张三","男",20,"121","zhangshang@xinlaing.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.coustmerNum++
    customer.Id = this.coustmerNum

    //重点
    this.customers = append(this.customers,customer)
    return true
}
//根据ID删除客户(从切片中删除)
func (this *CustomerService)Delete(id int) bool  {
    index := this.FindById(id)
    if index == -1{
        return false
    }
    // 如何从切片中删除一个元素
    this.customers = append(this.customers[:index],this.customers[index+1:]...)
    return true
}


//根据id查找客户在切片中对应的下标,如果没有该客户,返回 -1
func (this *CustomerService)FindById(id int) int {
    //遍历this.customer 切片
    index := -1
    for i:=0;i<len(this.customers) ;i ++  {
        if this.customers[i].Id == id{
            //找到了
            index = i
        }
    }
    return index
}

customer/view/cutomerview.go

package main

import (
    "awesomeProject/customer/model"
    "awesomeProject/customer/service"
    "fmt"
)

type customerView struct {
    //定义必要的字段
    key string //接受用户输入.....
    loop bool // 表示是否循环的显示菜单
    //增加一个字段 customerservice

     customerService   *service.CustomerService
}
//显示所有的客户信息
func (this *customerView)list()  {
    //首先,获得当前所有客户信息(在切片中)
    customers := this.customerService.List()
    fmt.Println("-------------------客户列表--------------------")
    fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
    for i:=0;i<len(customers) ;i++  {
        //fmt.Println(customers[i].Id,"\t",customers[i].Name)
        fmt.Println(customers[i].GetInfo())
    }
    fmt.Println("-----------------客户列表完成-------------------")
}

//得到用户的输入,信息构建新的客户,并完成添加
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)

    //构建一个新的customer实例
    //注意ID号,不用用户输入,id是唯一的,需要系统分配
    customer := model.NewCustomer2(name,gender,age,phone,email)

    if this.customerService.Add(customer){
        fmt.Println("------------------添加成功--------------")
    }else {
        fmt.Println("------------------添加失败--------------")
    }
}

//得到用户的输入ID,删除该IDd对应的客户
func (this *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 == "y"{
        //调用customerservice的delete方法
        if this.customerService.Delete(id){
            fmt.Println("--------------删除成功------------------")
        }else {
            fmt.Println("---------------输入的ID不存在-------------")
        }

    }

}

//退出软件
func (this *customerView)exit()  {
    fmt.Println("确认是否退出(Y/N):")
    for  {
        fmt.Scanln(&this.key)
        if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n"{
            break
        }
        fmt.Println("输入有误请重新输入")
    }
    if this.key == "Y" || this.key == "y" {
        this.loop = false
    }
}


// 显示主菜单
func (this *customerView) mainView(){
    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":
            fmt.Println("修改客户")
        case "3":
            this.delete()
        case "4":
            //fmt.Println("客户列表")
            this.list()
        case "5":
            this.exit()
        default:
            fmt.Println("你的输入有误,请从新输入")
        }
        if !this.loop {
            break
        }
    }
    fmt.Println("已经退出")
}

func main()  {
    //在主函数中创建  创建一个customerView,并运行显示主菜单
    customerView := customerView{
        key:"",
        loop:true,
    }
    //这里完成对customerView结构体的customerService字段的初始化
    customerView.customerService = service.NewCustomerService()
    //显示菜单1
    customerView.mainView()
}
上一篇 下一篇

猜你喜欢

热点阅读