go实现员工管理系统

go实现员工管理系统

1.本系统分为三层分别为视图层,业务层,数据层代码如下

入口文件

go实现员工管理系统

package main

import (
    "fmt"
    "model"
    "service"
)

type customerView struct {
    key string
    loop bool
    customerservice  * service.CustomerService
}
//展示列表
func (cv * customerView) showList(){
    fmt.Println("--------------客户列表--------------");
    fmt.Println("编号	姓名	性别	年龄	电话	邮箱");
    customersList:=cv.customerservice.List();
    for i:=0;i <len(customersList);i++ {
        info:=customersList[i].GetInfo();
        fmt.Println(info);
    }
    fmt.Println("--------------客户列表完成--------------");
}
//添加客户
func (cv * customerView) Add(){
    fmt.Println("--------------添加客户--------------");
    name:="";
    fmt.Println("请输入名字:");
    fmt.Scanln(&name);
    gener:="";
    fmt.Println("请输入性别:");
    fmt.Scanln(&gener);
    age:=0;
    fmt.Println("请输入年龄:");
    fmt.Scanln(&age);
    phone:="";
    fmt.Println("请输入电话:");
    fmt.Scanln(&phone);
    email:="";
    fmt.Println("请输入邮箱:");
    fmt.Scanln(&email);
    customer:=model.AddNewMember(name,gener,age,phone,email);
    bol:=cv.customerservice.AddData(customer);
    if bol {
        fmt.Println("--------------添加完成--------------");
    }
}
//删除客户
func (cv * customerView ) delUser(){
    fmt.Println("请输入要删除的员工ID:");
    id:=0;
    fmt.Scanln(&id);
    bol:=cv.customerservice.DelUser(id);
    if bol {
        fmt.Println("删除成功");
    }
}
//退出系统
func (cv * customerView) exitUser(){
    if  !cv.loop {
        fmt.Println("你退出了客户管理系统");
    }
}
//修改用户
func (cv * customerView ) updateUser(){
    fmt.Println("请输入修改用户ID:1");
    id:=0;
    UserSlice:=cv.customerservice.GetUserInfo(id);
    fmt.Scanf("姓名:%s",&UserSlice.Name);
    fmt.Println(UserSlice.Name);
}
//展示视图
func (cv * customerView) showView(){
    for {
        fmt.Println("--------------客户信息管理软件---------------");
        fmt.Println("              1 添加用户");
        fmt.Println("              2 修改用户");
        fmt.Println("              3 删除用户");
        fmt.Println("              4 用户列表");
        fmt.Println("              5 退出");
        fmt.Println("请选择(1-5):");
        fmt.Scanln(&cv.key);
        switch cv.key {
        case "1" :
            cv.Add();
        case "2":
            cv.updateUser();
        case "3":
            cv.delUser();
        case "4":
            cv.showList();
        case "5":
            cv.loop=false;
            cv.exitUser();
        default:
            fmt.Println("你输入的有误,请重新输入...");
        }
        if !cv.loop {
            break;
        }
    }
}
func main(){
    cv:=customerView{
        key: "",
        loop: true,
    };
    cv.customerservice = service.NewCustomerService();
    cv.showView();
}

 2.业务逻辑层

go实现员工管理系统

package service

import (
    "model"
)
type CustomerService struct {
    //声明一个切片用户保存用户
    customers []model.Customer
    //作为新客户id
    customer int
}
//初始化
func NewCustomerService() * CustomerService {
    CustomerService:=&CustomerService{};
    CustomerService.customer=1;
    customer:=model.NewMember(1,"lisi","",20,"13001176883","zt@126.com")
    CustomerService.customers=append(CustomerService.customers,customer);
    return CustomerService;
}
//返回切片类型
func ( customers CustomerService) List() []model.Customer {
    return customers.customers;
}
//添加数据
func (customers * CustomerService) AddData( customer model.Customer) bool  {
    customer.Id = len(customers.List())+1;
    customers.customers=append(customers.customers,customer);
    return true;
}
//删除一个用户
func (customers * CustomerService ) DelUser(index int) bool {
    index = index-1;
    customers.customers=append(customers.customers[:index],customers.customers[index+1:]...);
    return true;
}

func (customers * CustomerService ) GetUserInfo(id int) model.Customer {
    return customers.customers[id];
}

3.数据层

go实现员工管理系统

package model
import "fmt"
type Customer struct {
    Id int
    Name string
    Gender string
    Age int
    Phone string
    Email string
}
func NewMember (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 AddNewMember (name string,gender string,age int,phone string,email string) Customer {
    return Customer{
        Name: name,
        Gender: gender,
        Age: age,
        Phone: phone,
        Email: email,
    }
}
//获取用户信息
func (customer * Customer) GetInfo() string {
    info:=fmt.Sprintf("%v	%v	%v	%v	%v	%v",customer.Id,customer.Name,customer.Gender,customer.Age,customer.Phone,customer.Email);
    return info;
}