Go-客戶信息關(guān)系系統(tǒng)的實(shí)現(xiàn)
項(xiàng)目需求分析
- 模擬實(shí)現(xiàn)基于文本界面的《客戶信息管理軟件》。
- 該軟件能夠?qū)崿F(xiàn)對客戶對象的插入、修改和刪除(用切片實(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(" 請選擇(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)建一個(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("編號\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)建一個(gè) customerView,并運(yùn)行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 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("編號\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 號,沒有讓用戶輸入,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(" 請選擇(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)建一個(gè) customerView,并運(yùn)行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 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("編號\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 號,沒有讓用戶輸入,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("請選擇待刪除客戶編號(-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號不存在-")
}
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ò)誤,請重新輸入!")
}
}
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)
//調(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(" 請選擇(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)建一個(gè) customerView,并運(yùn)行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 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("編號\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 號,沒有讓用戶輸入,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("請選擇待刪除客戶編號(-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號不存在-")
}
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ò)誤,請重新輸入!")
}
}
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)
//調(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(" 請選擇(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)建一個(gè) customerView,并運(yùn)行顯示主菜單..
customerView := customerView{
key: "",
loop: true,
}
//這里完成對 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,說明沒有這個(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查找客戶在切片中對應(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang極簡入門教程(四):編寫第一個(gè)項(xiàng)目
這篇文章主要介紹了Golang極簡入門教程(四):編寫第一個(gè)項(xiàng)目,本文講解了workspace、包路徑、第一個(gè)可執(zhí)行命令等內(nèi)容,需要的朋友可以參考下2014-10-10
Go?tablewriter庫提升命令行輸出專業(yè)度實(shí)例詳解
命令行工具大家都用過,如果是運(yùn)維人員可能會(huì)編寫命令行工具來完成各種任務(wù),命令行輸出的美觀和易讀性往往容易被忽視,很爛的輸出會(huì)讓人感覺不專業(yè),本文將介紹Go語言中牛逼的實(shí)戰(zhàn)工具tablewriter庫,使你在命令行輸出中展現(xiàn)出專業(yè)的一面2023-11-11
Golang發(fā)送Get和Post請求的實(shí)現(xiàn)
做第三方接口有時(shí)需要用Get或者Post請求訪問,本文主要介紹了Golang發(fā)送Get和Post請求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
web項(xiàng)目中g(shù)olang性能監(jiān)控解析
這篇文章主要為大家介紹了web項(xiàng)目中g(shù)olang性能監(jiān)控詳細(xì)的解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
深入解析快速排序算法的原理及其Go語言版實(shí)現(xiàn)
這篇文章主要介紹了快速排序算法的原理及其Go語言版實(shí)現(xiàn),文中對于快速算法的過程和效率有較為詳細(xì)的說明,需要的朋友可以參考下2016-04-04

