Go-客戶信息關系系統(tǒng)的實現(xiàn)
項目需求分析
- 模擬實現(xiàn)基于文本界面的《客戶信息管理軟件》。
- 該軟件能夠實現(xiàn)對客戶對象的插入、修改和刪除(用切片實現(xiàn)),并能夠打印客戶明細表
項目的界面設計
主菜單界面

添加客戶界面

刪除客戶界面

客戶列表界面

客戶關系管理系統(tǒng)的程序框架圖

項目功能實現(xiàn)-顯示主菜單和完成退出軟件功能

代碼實現(xiàn) customerManage/model/customer.go
package model
//聲明一個 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,
}
}customerManage/service/customerService.go
package service
import "go_code/go_code/chapter13/customerManage/model"
type CustomerService struct{
customers []model.Customer
//聲明一個字段,表示當前切片含有多少個客戶
//該字段后面,還可以作為新客戶的 id+1
customerNum int
}customerManage/view/customerView.go
package main
import "fmt"
type customerView struct{
//定義必要字段
key string//接收客戶輸入...
loop bool//表示是否循環(huán)的顯示主菜單
//customerService *service.CustomerService
}
//顯示主菜單
func (this *customerView) mainMenu(){
for {
fmt.Println("--------客戶信息管理軟件--------")
fmt.Println(" 1、添加客戶")
fmt.Println(" 2、修改客戶")
fmt.Println(" 3、刪除客戶")
fmt.Println(" 4、客戶列表")
fmt.Println(" 5、退出")
fmt.Println()
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("你退出了客戶管理系統(tǒng)...")
}
}
func main(){
//在 main 函數(shù)中,創(chuàng)建一個 customerView,并運行顯示主菜單..
customerView:=customerView{
key:"",
loop:true,
}
//顯示主菜單
customerView.mainMenu()
}項目功能實現(xiàn)-完成顯示客戶列表的功能




customerManage/view/customerView.go
package main
import (
"fmt"
"go_code/go_code/chapter13/customerManage/service"
)
type customerView struct {
//定義必要字段
key string //接收客戶輸入...
loop bool //表示是否循環(huán)的顯示主菜單
//增加一個字段 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) mainMenu() {
for {
fmt.Println("--------客戶信息管理軟件--------")
fmt.Println(" 1、添加客戶")
fmt.Println(" 2、修改客戶")
fmt.Println(" 3、刪除客戶")
fmt.Println(" 4、客戶列表")
fmt.Println(" 5、退出")
fmt.Println()
fmt.Println(" 請選擇(1-5):")
fmt.Scanln(&this.key)
switch this.key {
case "1":
fmt.Println("添加客戶")
case "2":
fmt.Println("修改客戶")
case "3":
this.list()
case "4":
fmt.Println("客戶客戶")
case "5":
this.loop = false
default:
fmt.Println("你的輸入有誤,請重新輸入...")
}
if !this.loop {
break
}
fmt.Println("你退出了客戶管理系統(tǒng)...")
}
}
func main() {
//在 main 函數(shù)中,創(chuàng)建一個 customerView,并運行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 customerView 結構體的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//顯示主菜單
customerView.mainMenu()
}項目功能實現(xiàn)-添加客戶的功能



customerManage/service/customerService.go

customerManage/service/customerView.go
package main
import (
"fmt"
"go_code/go_code/chapter13/customerManage/model"
"go_code/go_code/chapter13/customerManage/service"
)
type customerView struct {
//定義必要字段
key string //接收客戶輸入...
loop bool //表示是否循環(huán)的顯示主菜單
//增加一個字段 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 是唯一的,需要系統(tǒng)分配
custmoer:=model.NewCustomer2(name,gender,age,phone,email)
//調用
if this.customerService.Add(custmoer){
fmt.Println("--------添加完成------------")
}else{
fmt.Println("--------添加失敗------------")
}
}
//顯示主菜單
func (this *customerView) mainMenu() {
for {
fmt.Println("--------客戶信息管理軟件--------")
fmt.Println(" 1、添加客戶")
fmt.Println(" 2、修改客戶")
fmt.Println(" 3、刪除客戶")
fmt.Println(" 4、客戶列表")
fmt.Println(" 5、退出")
fmt.Println()
fmt.Println(" 請選擇(1-5):")
fmt.Scanln(&this.key)
switch this.key {
case "1":
fmt.Println("添加客戶")
case "2":
fmt.Println("修改客戶")
case "3":
this.list()
case "4":
fmt.Println("客戶客戶")
case "5":
this.loop = false
default:
fmt.Println("你的輸入有誤,請重新輸入...")
}
if !this.loop {
break
}
fmt.Println("你退出了客戶管理系統(tǒng)...")
}
}
func main() {
//在 main 函數(shù)中,創(chuàng)建一個 customerView,并運行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 customerView 結構體的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//顯示主菜單
customerView.mainMenu()
}項目功能實現(xiàn)-完成刪除客戶的功能


customerManage/model/customer.go [沒有變化]
customerManage/service/customerService.go

customerManage/view/customerView.go
package main
import (
"fmt"
"go_code/go_code/chapter13/customerManage/model"
"go_code/go_code/chapter13/customerManage/service"
)
type customerView struct {
//定義必要字段
key string //接收客戶輸入...
loop bool //表示是否循環(huán)的顯示主菜單
//增加一個字段 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 是唯一的,需要系統(tǒng)分配
custmoer := model.NewCustomer2(name, gender, age, phone, email)
//調用
if this.customerService.Add(custmoer) {
fmt.Println("--------添加完成------------")
} else {
fmt.Println("--------添加失敗------------")
}
}
func (this *customerView) delete() {
flag := false
fmt.Println("--------刪除客戶------------")
for {
fmt.Println("請選擇待刪除客戶編號(-1)退出:")
id := -1
fmt.Scanln(&id)
if id == -1 {
return //放棄刪除操作
}
fmt.Println("確認是否刪除(Y/N)")
//這里同學們可以加入一個循環(huán)判斷,直到用戶輸入y或者,才退出
for {
choice := ""
fmt.Scanln(&choice)
if choice == "y" || choice == "n" {
if this.customerService.Delete(id) {
fmt.Println("--------刪除完成------------")
flag = true
} else {
fmt.Println("--------刪除失敗,輸入的id號不存在-")
}
break
}
}
if flag {
break
}
}
}
func (this *customerView) exit() {
fmt.Println("確認是否退出(Y/N)")
for {
fmt.Scanln(&this.key)
if this.key == "y" || this.key == "n" {
break
} else {
fmt.Println("輸入格式錯誤,請重新輸入!")
}
}
if this.key == "y" {
this.loop = false
}
}
func (this *customerView) update() {
fmt.Println("----------------修改客戶------------------")
fmt.Println("客戶id(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return // 放棄修改
}
//獲取要修改的數(shù)據(jù),并顯示
index := this.customerService.FindById(id)
if index == -1 {
fmt.Println("------------------客戶id不存在------------------")
return
}
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)
fmt.Println("你確定要修改嗎? y/n")
choice := ""
for {
fmt.Scanln(&choice)
if choice == "y" || choice == "n" {
break
}
fmt.Println("你輸入有誤,請重新輸入 y/n")
}
customer := model.NewCustomer2(name, gender, age, phone, email)
//調用customerService.Update
if this.customerService.Update(index, customer) {
fmt.Println("------------------修改成功------------------")
} else {
fmt.Println("------------------修改失敗------------------")
}
}
//顯示主菜單
func (this *customerView) mainMenu() {
for {
fmt.Println("--------客戶信息管理軟件--------")
fmt.Println(" 1、添加客戶")
fmt.Println(" 2、修改客戶")
fmt.Println(" 3、刪除客戶")
fmt.Println(" 4、客戶列表")
fmt.Println(" 5、退出")
fmt.Println()
fmt.Println(" 請選擇(1-5):")
fmt.Scanln(&this.key)
switch this.key {
case "1":
this.add()
case "2":
this.update()
case "3":
this.delete()
case "4":
this.list()
case "5":
this.exit()
default:
fmt.Println("你的輸入有誤,請重新輸入...")
}
if !this.loop {
break
}
fmt.Println("你退出了客戶管理系統(tǒng)...")
}
}
func main() {
//在 main 函數(shù)中,創(chuàng)建一個 customerView,并運行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 customerView 結構體的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//顯示主菜單
customerView.mainMenu()
}項目功能實現(xiàn)-完善退出確認功能(課后作業(yè))


客戶關系管理系統(tǒng)-課后練習

CustomerView.go
package main
import (
"fmt"
"go_code/go_code/chapter13/customerManage/model"
"go_code/go_code/chapter13/customerManage/service"
)
type customerView struct {
//定義必要字段
key string //接收客戶輸入...
loop bool //表示是否循環(huán)的顯示主菜單
//增加一個字段 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 是唯一的,需要系統(tǒng)分配
custmoer := model.NewCustomer2(name, gender, age, phone, email)
//調用
if this.customerService.Add(custmoer) {
fmt.Println("--------添加完成------------")
} else {
fmt.Println("--------添加失敗------------")
}
}
func (this *customerView) delete() {
flag := false
fmt.Println("--------刪除客戶------------")
for {
fmt.Println("請選擇待刪除客戶編號(-1)退出:")
id := -1
fmt.Scanln(&id)
if id == -1 {
return //放棄刪除操作
}
fmt.Println("確認是否刪除(Y/N)")
//這里同學們可以加入一個循環(huán)判斷,直到用戶輸入y或者,才退出
for {
choice := ""
fmt.Scanln(&choice)
if choice == "y" || choice == "n" {
if this.customerService.Delete(id) {
fmt.Println("--------刪除完成------------")
flag = true
} else {
fmt.Println("--------刪除失敗,輸入的id號不存在-")
}
break
}
}
if flag {
break
}
}
}
func (this *customerView) exit() {
fmt.Println("確認是否退出(Y/N)")
for {
fmt.Scanln(&this.key)
if this.key == "y" || this.key == "n" {
break
} else {
fmt.Println("輸入格式錯誤,請重新輸入!")
}
}
if this.key == "y" {
this.loop = false
}
}
func (this *customerView) update() {
fmt.Println("----------------修改客戶------------------")
fmt.Println("客戶id(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return // 放棄修改
}
//獲取要修改的數(shù)據(jù),并顯示
index := this.customerService.FindById(id)
if index == -1 {
fmt.Println("------------------客戶id不存在------------------")
return
}
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)
fmt.Println("你確定要修改嗎? y/n")
choice := ""
for {
fmt.Scanln(&choice)
if choice == "y" || choice == "n" {
break
}
fmt.Println("你輸入有誤,請重新輸入 y/n")
}
customer := model.NewCustomer2(name, gender, age, phone, email)
//調用customerService.Update
if this.customerService.Update(index, customer) {
fmt.Println("------------------修改成功------------------")
} else {
fmt.Println("------------------修改失敗------------------")
}
}
//顯示主菜單
func (this *customerView) mainMenu() {
for {
fmt.Println("--------客戶信息管理軟件--------")
fmt.Println(" 1、添加客戶")
fmt.Println(" 2、修改客戶")
fmt.Println(" 3、刪除客戶")
fmt.Println(" 4、客戶列表")
fmt.Println(" 5、退出")
fmt.Println()
fmt.Println(" 請選擇(1-5):")
fmt.Scanln(&this.key)
switch this.key {
case "1":
this.add()
case "2":
this.update()
case "3":
this.delete()
case "4":
this.list()
case "5":
this.exit()
default:
fmt.Println("你的輸入有誤,請重新輸入...")
}
if !this.loop {
break
}
fmt.Println("你退出了客戶管理系統(tǒng)...")
}
}
func main() {
//在 main 函數(shù)中,創(chuàng)建一個 customerView,并運行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 customerView 結構體的 customerService 字段的初始化
customerView.customerService = service.NewCustomerService()
//顯示主菜單
customerView.mainMenu()
}CustomerService.go
package service
import (
"go_code/go_code/chapter13/customerManage/model"
)
type CustomerService struct {
customers []model.Customer
//聲明一個字段,表示當前切片含有多少個客戶
//該字段后面,還可以作為新客戶的 id+1
customerNum int
}
//編寫一個方法,可以返回*CustmoerService
func NewCustomerService() *CustomerService {
//為了能夠看到有客戶在切片中,我們初始化一個切片
custmoerService := &CustomerService{}
custmoerService.customerNum = 1
custmoer := model.NewCustomer(1, "張三", "男",
20, "112", "zhangsan@sohu.com")
custmoerService.customers = append(custmoerService.customers, custmoer)
return custmoerService
}
//返回客戶切片
func (this *CustomerService) List() []model.Customer {
return this.customers
}
//添加客戶到切片
//!!!
func (this *CustomerService) Add(custmoer model.Customer) bool {
this.customerNum++
custmoer.Id = this.customerNum
this.customers = append(this.customers, custmoer)
return true
}
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
}
func (this *CustomerService)Update(index int,customer model.Customer)bool{
this.customers[index].Name= customer.Name
this.customers[index].Age= customer.Age
this.customers[index].Gender= customer.Gender
this.customers[index].Phone= customer.Phone
this.customers[index].Email= customer.Email
return true
}
//根據(jù)id查找客戶在切片中對應的下表,如果沒有該客戶,返回-1
func (this *CustomerService) FindById(id int) int {
index := -1
for i := 0; i < len(this.customers); i++ {
if this.customers[i].Id == id {
index = i
}
}
return index
}到此這篇關于Go-客戶信息關系系統(tǒng)的實現(xiàn)的文章就介紹到這了,更多相關Go-客戶信息關系系統(tǒng)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Go語言Slice作為函數(shù)參數(shù)的使用
Slice切片在Go語言中實質是一種結構體類型,本文詳細的介紹了Go語言Slice作為函數(shù)參數(shù)的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
Go中使用gjson來操作JSON數(shù)據(jù)的實現(xiàn)
本文主要介紹了Go中使用gjson來操作JSON數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Golang 基礎之函數(shù)使用(匿名遞歸閉包)實例詳解
這篇文章主要為大家介紹了Golang 基礎之函數(shù)使用(匿名遞歸閉包)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

