1. 程式人生 > >go案例:客戶管理系統流程 mvc模式 分層設計

go案例:客戶管理系統流程 mvc模式 分層設計

pri 結構 mep service 循環 刪除 phone 列表 bre

下面是一個簡要的客服系統,主要是演示分層計。。

model : 數據部份:

技術分享圖片
package model

import "fmt"

//聲明一個結構體,表示一個客戶信息
type Customer struct{
    Id int
    Name string
    Gender string
    Age int
    Phone string
    Emaill 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, } } func NewCustomer2(name string, gender string, age int, phone string,email string) Customer{ return Customer{ Name : name, Gender : gender, Age : age, Phone:phone, } } 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.Emaill) return info }
View Code

控制層,這兒名為服務層 customerService 代碼如下:

技術分享圖片
package model

import "fmt"

//聲明一個結構體,表示一個客戶信息
type Customer struct{
    Id int
    Name 
string Gender string Age int Phone string Emaill 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, } } func NewCustomer2(name string, gender string, age int, phone string,email string) Customer{ return Customer{ Name : name, Gender : gender, Age : age, Phone:phone, } } 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.Emaill) return info }
View Code

最後是視圖層:第一個版本沒有加註釋,以後有空成全功能後在認真寫好註釋

技術分享圖片
package main

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

type customerView struct{
    key string
    loop bool
    customerService *service.CustomerService
}
//顯示用戶例表
func(this *customerView) list(){
    customers:=this.customerService.List()
    fmt.Println("——————————————————客戶例表————————————————————————————")
    fmt.Println("編號 \t 姓名 \t \t性別 \t年齡 \t電話 \t郵箱")
    for i:=0; i < len(customers);i++{
        fmt.Println(customers[i].GetInfo())
    }
    fmt.Println("++++++++++++++客戶列表完成+++++++++++++++")
}
//jo fi add
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 :=model.NewCustomer2(name,gender,age, phone,email)
    this.customerService.Add(customer)
}
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"{
        if this.customerService.Delete(id){
            fmt.Println("____________刪 除成功__________________")
        }else{
            fmt.Println("刪 除失敗,可能是輸入的Id不存在")

        }
    }


}







func(this *customerView)mainMenu(){
    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":
            this.list()
        case "5":
            this.loop = false
        default:
            fmt.Println("輸入有誤 ")
        }//switch循環結束
        if !this.loop{
            break
        }//for
      }
    fmt.Println("你退出了客戶系統")

}//主函數

func main(){
     customerView := customerView{
        key: "",
        loop : true,
    }
     customerView.customerService = service.NewcustomerService()
    customerView.mainMenu()
}
View Code

go案例:客戶管理系統流程 mvc模式 分層設計