客戶資訊關系系統
專案需求分析
- 模擬實作基于文本界面的《 客戶資訊管理軟體》,
- 該軟體能夠實作對客戶物件的插入、修改和洗掉(用切片實作),并能夠列印客戶明細表
專案的界面設計
見代碼的運行結果
專案功能實作-顯示主選單和完成退出軟體功能
功能的說明
當用戶運行程式時,可以看到主選單,當輸入 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", "[email protected]")
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()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63363.html
標籤:Go
上一篇:go-家庭收支記賬軟體例子
