Go-客戶信息關(guān)系系統(tǒng)的實(shí)現(xiàn)
項(xiàng)目需求分析
- 模擬實(shí)現(xiàn)基于文本界面的《客戶信息管理軟件》。
- 該軟件能夠?qū)崿F(xiàn)對(duì)客戶對(duì)象的插入、修改和刪除(用切片實(shí)現(xiàn)),并能夠打印客戶明細(xì)表
項(xiàng)目的界面設(shè)計(jì)
主菜單界面
添加客戶界面
刪除客戶界面
客戶列表界面
客戶關(guān)系管理系統(tǒng)的程序框架圖
項(xiàng)目功能實(shí)現(xiàn)-顯示主菜單和完成退出軟件功能
代碼實(shí)現(xiàn) customerManage/model/customer.go
package model //聲明一個(gè) Customer 結(jié)構(gòu)體,表示一個(gè)客戶信息 type Customer struct{ Id int Name string Gender string Age int Phone string Email string } //使用工廠模式,返回一個(gè) Customer 的實(shí)例 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 //聲明一個(gè)字段,表示當(dāng)前切片含有多少個(gè)客戶 //該字段后面,還可以作為新客戶的 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(" 請(qǐng)選擇(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("你的輸入有誤,請(qǐng)重新輸入...") } if !this.loop{ break } fmt.Println("你退出了客戶管理系統(tǒng)...") } } func main(){ //在 main 函數(shù)中,創(chuàng)建一個(gè) customerView,并運(yùn)行顯示主菜單.. customerView:=customerView{ key:"", loop:true, } //顯示主菜單 customerView.mainMenu() }
項(xiàng)目功能實(shí)現(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)的顯示主菜單 //增加一個(gè)字段 customerService customerService *service.CustomerService } //顯示所有客戶信息 func (this *customerView) list() { customers := this.customerService.List() //顯示 fmt.Println("--------客戶列表--------------") fmt.Println("編號(hào)\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(" 請(qǐng)選擇(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("你的輸入有誤,請(qǐng)重新輸入...") } if !this.loop { break } fmt.Println("你退出了客戶管理系統(tǒng)...") } } func main() { //在 main 函數(shù)中,創(chuàng)建一個(gè) customerView,并運(yùn)行顯示主菜單.. customerView := customerView{ key: "", loop: true, } //這里完成對(duì) customerView 結(jié)構(gòu)體的 customerService 字段的初始化 customerView.customerService = service.NewCustomerService() //顯示主菜單 customerView.mainMenu() }
項(xiàng)目功能實(shí)現(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)的顯示主菜單 //增加一個(gè)字段 customerService customerService *service.CustomerService } //顯示所有客戶信息 func (this *customerView) list() { customers := this.customerService.List() //顯示 fmt.Println("--------客戶列表--------------") fmt.Println("編號(hào)\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("--------客戶列表完成-----------") } //得到用戶的輸入,信息構(gòu)建新的客戶,并完成添加 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) //構(gòu)建一個(gè)新的 Customer 實(shí)例 //注意: id 號(hào),沒有讓用戶輸入,id 是唯一的,需要系統(tǒng)分配 custmoer:=model.NewCustomer2(name,gender,age,phone,email) //調(diào)用 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(" 請(qǐng)選擇(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("你的輸入有誤,請(qǐng)重新輸入...") } if !this.loop { break } fmt.Println("你退出了客戶管理系統(tǒng)...") } } func main() { //在 main 函數(shù)中,創(chuàng)建一個(gè) customerView,并運(yùn)行顯示主菜單.. customerView := customerView{ key: "", loop: true, } //這里完成對(duì) customerView 結(jié)構(gòu)體的 customerService 字段的初始化 customerView.customerService = service.NewCustomerService() //顯示主菜單 customerView.mainMenu() }
項(xiàng)目功能實(shí)現(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)的顯示主菜單 //增加一個(gè)字段 customerService customerService *service.CustomerService } //顯示所有客戶信息 func (this *customerView) list() { customers := this.customerService.List() //顯示 fmt.Println("--------客戶列表--------------") fmt.Println("編號(hào)\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("--------客戶列表完成-----------") } //得到用戶的輸入,信息構(gòu)建新的客戶,并完成添加 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) //構(gòu)建一個(gè)新的 Customer 實(shí)例 //注意: id 號(hào),沒有讓用戶輸入,id 是唯一的,需要系統(tǒng)分配 custmoer := model.NewCustomer2(name, gender, age, phone, email) //調(diào)用 if this.customerService.Add(custmoer) { fmt.Println("--------添加完成------------") } else { fmt.Println("--------添加失敗------------") } } func (this *customerView) delete() { flag := false fmt.Println("--------刪除客戶------------") for { fmt.Println("請(qǐng)選擇待刪除客戶編號(hào)(-1)退出:") id := -1 fmt.Scanln(&id) if id == -1 { return //放棄刪除操作 } fmt.Println("確認(rèn)是否刪除(Y/N)") //這里同學(xué)們可以加入一個(gè)循環(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號(hào)不存在-") } break } } if flag { break } } } func (this *customerView) exit() { fmt.Println("確認(rèn)是否退出(Y/N)") for { fmt.Scanln(&this.key) if this.key == "y" || this.key == "n" { break } else { fmt.Println("輸入格式錯(cuò)誤,請(qǐng)重新輸入!") } } 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("你輸入有誤,請(qǐng)重新輸入 y/n") } customer := model.NewCustomer2(name, gender, age, phone, email) //調(diào)用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(" 請(qǐng)選擇(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("你的輸入有誤,請(qǐng)重新輸入...") } if !this.loop { break } fmt.Println("你退出了客戶管理系統(tǒng)...") } } func main() { //在 main 函數(shù)中,創(chuàng)建一個(gè) customerView,并運(yùn)行顯示主菜單.. customerView := customerView{ key: "", loop: true, } //這里完成對(duì) customerView 結(jié)構(gòu)體的 customerService 字段的初始化 customerView.customerService = service.NewCustomerService() //顯示主菜單 customerView.mainMenu() }
項(xiàng)目功能實(shí)現(xiàn)-完善退出確認(rèn)功能(課后作業(yè))
客戶關(guān)系管理系統(tǒng)-課后練習(xí)
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)的顯示主菜單 //增加一個(gè)字段 customerService customerService *service.CustomerService } //顯示所有客戶信息 func (this *customerView) list() { customers := this.customerService.List() //顯示 fmt.Println("--------客戶列表--------------") fmt.Println("編號(hào)\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("--------客戶列表完成-----------") } //得到用戶的輸入,信息構(gòu)建新的客戶,并完成添加 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) //構(gòu)建一個(gè)新的 Customer 實(shí)例 //注意: id 號(hào),沒有讓用戶輸入,id 是唯一的,需要系統(tǒng)分配 custmoer := model.NewCustomer2(name, gender, age, phone, email) //調(diào)用 if this.customerService.Add(custmoer) { fmt.Println("--------添加完成------------") } else { fmt.Println("--------添加失敗------------") } } func (this *customerView) delete() { flag := false fmt.Println("--------刪除客戶------------") for { fmt.Println("請(qǐng)選擇待刪除客戶編號(hào)(-1)退出:") id := -1 fmt.Scanln(&id) if id == -1 { return //放棄刪除操作 } fmt.Println("確認(rèn)是否刪除(Y/N)") //這里同學(xué)們可以加入一個(gè)循環(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號(hào)不存在-") } break } } if flag { break } } } func (this *customerView) exit() { fmt.Println("確認(rèn)是否退出(Y/N)") for { fmt.Scanln(&this.key) if this.key == "y" || this.key == "n" { break } else { fmt.Println("輸入格式錯(cuò)誤,請(qǐng)重新輸入!") } } 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("你輸入有誤,請(qǐng)重新輸入 y/n") } customer := model.NewCustomer2(name, gender, age, phone, email) //調(diào)用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(" 請(qǐng)選擇(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("你的輸入有誤,請(qǐng)重新輸入...") } if !this.loop { break } fmt.Println("你退出了客戶管理系統(tǒng)...") } } func main() { //在 main 函數(shù)中,創(chuàng)建一個(gè) customerView,并運(yùn)行顯示主菜單.. customerView := customerView{ key: "", loop: true, } //這里完成對(duì) customerView 結(jié)構(gòu)體的 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 //聲明一個(gè)字段,表示當(dāng)前切片含有多少個(gè)客戶 //該字段后面,還可以作為新客戶的 id+1 customerNum int } //編寫一個(gè)方法,可以返回*CustmoerService func NewCustomerService() *CustomerService { //為了能夠看到有客戶在切片中,我們初始化一個(gè)切片 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,說(shuō)明沒有這個(gè)客戶 if index ==-1{ return false } //如何從切片中刪除一個(gè)元素 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查找客戶在切片中對(duì)應(yīng)的下表,如果沒有該客戶,返回-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 }
到此這篇關(guān)于Go-客戶信息關(guān)系系統(tǒng)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Go-客戶信息關(guān)系系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Goland激活碼破解永久版及安裝詳細(xì)教程(親測(cè)可以)
這篇文章主要介紹了Goland激活碼破解永久版及安裝詳細(xì)教程(親測(cè)可以),本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10詳解Go語(yǔ)言Slice作為函數(shù)參數(shù)的使用
Slice切片在Go語(yǔ)言中實(shí)質(zhì)是一種結(jié)構(gòu)體類型,本文詳細(xì)的介紹了Go語(yǔ)言Slice作為函數(shù)參數(shù)的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Go語(yǔ)言O(shè)RM框架構(gòu)造查詢條件示例詳解
這篇文章主要為大家介紹了Go語(yǔ)言O(shè)RM框架構(gòu)造查詢條件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12golang切片擴(kuò)容規(guī)則實(shí)現(xiàn)
這篇文章主要介紹了golang切片擴(kuò)容規(guī)則實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03GO語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)的方法
這篇文章主要介紹了GO語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)的方法,實(shí)例分析了Go語(yǔ)言實(shí)現(xiàn)TCP服務(wù)的技巧,需要的朋友可以參考下2015-03-03Go中使用gjson來(lái)操作JSON數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了Go中使用gjson來(lái)操作JSON數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Golang 基礎(chǔ)之函數(shù)使用(匿名遞歸閉包)實(shí)例詳解
這篇文章主要為大家介紹了Golang 基礎(chǔ)之函數(shù)使用(匿名遞歸閉包)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10