go-客户信息关系系统

2022-10-13,,,

客户信息关系系统

项目需求分析
1) 模拟实现基于文本界面的《 客户信息管理软件》。
2) 该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表

项目的界面设计

见代码的运行结果

项目功能实现-显示主菜单和完成退出软件功能

功能的说明
当用户运行程序时,可以看到主菜单,当输入 5 时,可以退出该软件.
思路分析
编写 customerview.go ,另外可以把 customer.go 和 customerservice.go 写上.

项目功能实现-完成显示客户列表的功能

项目功能实现-添加客户的功能

项目功能实现-完成删除客户的功能

代码实现:customerservice.go

package service
import (
    "go_code/code/customermanage/model"

)

//该customerservice, 完成对customer的操作,包括
//增删改查
type customerservice struct {
    customers []model.customer
    //声明一个字段,表示当前切片含有多少个客户
    //该字段后面,还可以作为新客户的id+1
    customernum int 
}

//编写一个方法,可以返回 *customerservice
func newcustomerservice() *customerservice {
    //为了能够看到有客户在切片中,我们初始化一个客户
    customerservice := &customerservice{}
    customerservice.customernum = 1
    customer := model.newcustomer(1, "张三", "男", 20, "112", "zs@sohu.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.customernum++
    customer.id = this.customernum
    this.customers = append(this.customers, customer)
    return true
}

//根据id删除客户(从切片中删除)
func (this *customerservice) delete(id int) bool {
    index := this.findbyid(id)
    //如果index == -1, 说明没有这个客户
    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 {
    index := -1
    //遍历this.customers 切片
    for i := 0; i < len(this.customers); i++ {
        if this.customers[i].id == id {
            //找到
            index = i
        }
    }
    return index
}

代码实现:customer.go

package model
import (
    "fmt"
)
//声明一个customer结构体,表示一个客户信息 

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

//第二种创建customer实例方法,不带id
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

}

代码实现:customerview.go

package main

import (
    "fmt"
    "go_code/code/customermanage/service"
    "go_code/code/customermanage/model"
)

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.printf("\n-------------------------客户列表完成-------------------------\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)
    //构建一个新的customer实例
    //注意: id号,没有让用户输入,id是唯一的,需要系统分配
    customer := model.newcustomer2(name, gender, age, phone, email)
    //调用
    if this.customerservice.add(customer) {
        fmt.println("---------------------添加完成---------------------")
    } else {
        fmt.println("---------------------添加失败---------------------")
    }
}

//得到用户的输入id,删除该id对应的客户
func (this *customerview) delete() {
    fmt.println("---------------------删除客户---------------------")
    fmt.println("请选择待删除客户编号(-1退出):")
    id := -1
    fmt.scanln(&id)
    if id == -1 {
        return //放弃删除操作
    }
    fmt.println("确认是否删除(y/n):")
    //这里同学们可以加入一个循环判断,直到用户输入 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("你的输入有误,确认是否退出(y/n):")
    }

    if this.key == "y" || this.key == "y" {
        this.loop = false
    }

}

//显示主菜单
func (this *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(&this.key)
        switch this.key {
            case "1" :
                this.add()
            case "2" :
                fmt.println("修 改 客 户")
            case "3" :
                this.delete()
            case "4" :
                this.list()
            case "5" :
                this.exit()
            default :
                fmt.println("你的输入有误,请重新输入...")
        }

        if !this.loop {
            break
        }

    }
    fmt.println("你退出了客户关系管理系统...")
}



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

}

《go-客户信息关系系统.doc》

下载本文的Word格式文档,以方便收藏与打印。