go语言学习【十】

客户管理系统
1.主菜单和退出
2.添加客户
3.删除客户
4.修改客户信息

通过使用不同的包,以及对包的引用,实现客户管理系统。

customerView.go

package main

import (
	"fmt"
	"go_code/customerManage/service"
	"go_code/customerManage/model"
)
type customerView struct {
	key string //接收用户输入
	loop bool //表示是否循环显示主菜单
	//增加一个字段用来调用customerService
	customerService *service.CustomerService

}

//编写方法,调用List方法显示列表
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("--------------------客户列表完成----------------------"+"\n")
}

//退出软件
func(this *customerView) exit() {
	fmt.Print("确认是否退出(Y/N):")
	for{
	   fmt.Scanln(&this.key)
	   if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n" {
		   break
	   }
	   fmt.Print("你的输入有误,确认是否退出(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(this *customerView) add() {
	fmt.Println("---------------------添加客户----------------------")
	fmt.Print("姓名:")
	name := ""
	fmt.Scanln(&name)
	fmt.Print("性别:")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Print("年龄:")
	age := 0
	fmt.Scanln(&age)
	fmt.Print("电话:")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Print("电子邮件:")
	email := ""
	fmt.Scanln(&email)

	//构建一个新的Customer实例
	//注意:Id号是系统分配的
	customer := model.NewCustomer2(name,gender,age,phone,email)
	//调用
	if this.customerService.Add(customer) {
		fmt.Println("---------------------添加完成----------------------"+"\n")
	} else {
		fmt.Println("---------------------添加失败----------------------"+"\n")
	}
}

//得到用户的输入id,删除该id对应的客户
func(this *customerView) delete() {
	fmt.Println("---------------------删除客户----------------------")
	fmt.Print("请选择待删除客户编号(-1退出):")
	id := -1
	fmt.Scanln(&id)
	if id==-1 {
		return //放弃删除
	}
	choice := ""
	fmt.Print("确认是否删除(Y/N):")
	for {
	    fmt.Scanln(&choice)
		if choice == "y" || choice =="Y" {
		//调用customerService的Delete方法
		if this.customerService.Delete(id) {
			fmt.Println("---------------------删除完成----------------------"+"\n")
	} else {
		fmt.Println("----------------删除失败,该id号不存在------------")
	}
	break
	} else if choice == "n" || choice =="N" {
		break
	} else {
		fmt.Print("你输入的字母不正确,请重新输入:")
	}	
  }
}

func main() {
 //在主函数中创建一个customerView实例,运行显示主菜单
 customerView := customerView{
	 key :"",
	 loop :true,
 }
 customerView.customerService = service.NewCustomerService()
 customerView.mainMenu()

} 

customerService.go

package service
import(
	"go_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,"Jack","男",18,"15641469784","2654623@qq.com")
	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,
		}
	}

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
}

运行结果:

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):1
---------------------添加客户----------------------
姓名:小猪
性别:2
年龄:2222
电话:
电子邮件:
---------------------添加完成----------------------

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):5
确认是否退出(Y/N):y
你退出了客户信息管理软件......

E:\goproject\src\go_code\customerManage\view>go run customerView.go
-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):1
---------------------添加客户----------------------
姓名:小猪
性别:男
年龄:2
电话:48621546301
电子邮件:451845@qq.com
---------------------添加完成----------------------

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):1
---------------------添加客户----------------------
姓名:花花
性别:女
年龄:16
电话:598149623
电子邮件:xshsi@sohu
---------------------添加完成----------------------

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):4
---------------------客户列表----------------------
编号    姓名    性别    年龄    电话            邮箱
1       Jack    男      18      15641469784     2654623@qq.com
2       小猪    男      2       48621546301     451845@qq.com
3       花花    女      16      598149623       xshsi@sohu
--------------------客户列表完成----------------------

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):3
---------------------删除客户----------------------
请选择待删除客户编号(-1退出):2
确认是否删除(Y/N):n
-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):3
---------------------删除客户----------------------
请选择待删除客户编号(-1退出):2
确认是否删除(Y/N):m
你输入的字母不正确,请重新输入:y
---------------------删除完成----------------------

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):4
---------------------客户列表----------------------
编号    姓名    性别    年龄    电话            邮箱
1       Jack    男      18      15641469784     2654623@qq.com
3       花花    女      16      598149623       xshsi@sohu
--------------------客户列表完成----------------------

-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):5
确认是否退出(Y/N):m
你的输入有误,确认是否退出(Y/N):n
-----------------客户信息管理软件--------------
                   1.添加客户
                   2.修改客户
                   3.删除客户
                   4.客户列表
                   5.退   出
请选择(1-5):5
确认是否退出(Y/N):y
你退出了客户信息管理软件......

这次项目完成需要熟练掌握切片,结构体字段与工厂模式的具体使用场景,同时要求对方法以及指针的用法要多加练习。
客户管理系统的实现需要对项目做分层框架,对于各个包里面的方法以及各个字段的使用注意事项要有明确的认知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值