个人github(包括golang学习笔记、源码):https://github.com/fangguizhen/Notes/blob/master/Golang%E7%9F%A5%E8%AF%86%E7%82%B9.md
文章说明:该系统能实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表。后面系统加入客户查找、系统简单登录与退出功能。
一、项目需求分析
该系统能实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表。
二、项目页面设计
1、主菜单页面
2、客户列表页面
三、代码
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
}
customerService.go
package service
import (
"customerManager2/model"
"fmt"
)
//定义CustomerService,完成对customer的增删改
type CustomerService struct {
customers []model.Customer
//声明一个字段,表示当前切片含有多少个客户
//该字段还可以作为新客户的id号
customerNum int
}
//编写一个方法,可以返回*CustomerService
func Newcustomerservice() *CustomerService {
//初始化客户
customerService := &CustomerService{}
customerService.customerNum = 1
customer := model.NewCustomer(1, "张三", "男", 20, "112", "[email protected]")
//把customer加入到切片中
customerService.customers = append(customerService.customers, customer)
return customerService
}
//返回客户切片
func (this *CustomerService) List() []model.Customer {
return this.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
//遍历cv.customers 切片
for i := 0; i < len(this.customers); i++ {
if this.customers[i].Id == id {
index = i
}
}
return index
}
//修改指定客户信息
func (this *CustomerService) Update(customer model.Customer) bool {
index := this.FindById(customer.Id)
if index == -1 {
return false
}
this.customers = append(append(this.customers[:index], customer), this.customers[index+1:]...)
return true
}
//查找指定客户信息
func (this *CustomerService) Search(id int) bool {
index := this.FindById(id)
if index == -1 {
return false
}
customers := this.List()
fmt.Println(customers[index].GetInfo())
return true
}
customerView.go
package main
import (
"customerManager2/model"
"customerManager2/service"
"fmt"
)
type customerView struct {
//接收用户输入
key string
//控制是否循环
loop bool
//增加一个customerService字段
customerService *service.CustomerService
}
//编写简单登录
func (this *customerView) Login(account string, pwd string) {
for {
fmt.Println("请输入你的系统账号..")
fmt.Scanln(&account)
fmt.Println("请输入你的系统密码..")
fmt.Scanln(&pwd)
//逻辑判断
if account != "中国东莞" || pwd != "88888888" {
fmt.Println("你输入的账号或者密码不正确,请重新输入..")
//递归调用
this.Login("", "")
} else {
fmt.Println("恭喜你!正在进入系统...")
//如果成功进入,调用主菜单
this.mainView()
}
}
}
//显示所有客户的信息
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].GetInfo())
}
fmt.Printf("----------显示成功----------\n")
}
//查找指定ID客户信息
func (this *customerView) search() {
fmt.Println("----------查找客户----------")
fmt.Print("请选择查找客户的编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return
}
if this.customerService.Search(id) {
fmt.Println("----------查找完成----------")
} else {
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("----------添加成功----------")
} else {
fmt.Println("----------添加失败----------")
}
}
//得到用户的输入,修改对应Id的客户
func (this *customerView) update() {
fmt.Println("----------修改客户----------")
fmt.Print("请选择修改客户的编号(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
return
}
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 := model.NewCustomer(id, name, gender, age, phone, email)
if this.customerService.Update(customer) {
fmt.Println("----------修改成功----------")
} else {
fmt.Println("----------修改失败----------")
}
}
//得到用户输入的ID,删除对应客户
func (this *customerView) delete() {
fmt.Println("----------删除客户----------")
fmt.Print("请输入你要删除客户的ID(-1退出):")
id := -1
fmt.Scanln(&id)
if id == -1 {
//放弃删除操作
return
}
choice := ""
fmt.Print("确认是否删除(Y/N):")
for {
fmt.Scanln(&choice)
if choice == "Y" || choice == "y" || choice == "N" || choice == "n" {
break
}
fmt.Print("你的输入有误,确认是否删除(Y/N):")
}
if choice == "Y" || choice == "y" {
//调用
if this.customerService.Delete(id) {
fmt.Println("----------删除成功----------")
} else {
fmt.Println("-----删除失败,ID不存在------")
}
}
}
//退出提示
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) mainView() {
for {
fmt.Println("------客户信息管理系统------")
fmt.Println(" 1 添加客户")
fmt.Println(" 2 修改客户")
fmt.Println(" 3 删除客户")
fmt.Println(" 4 客户列表")
fmt.Println(" 5 查找客户")
fmt.Println(" 6 退出软件")
fmt.Print("请选择(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.search()
case "6":
this.loop = false
default:
fmt.Println("请从(1-5)选择正确的选项..")
}
if !this.loop {
break
}
}
fmt.Println("你已经退出客户信息管理系统账号..")
}
func main() {
fmt.Println("客户信息管理系统开发..")
//创建一个customerView实例
customerView := customerView{
key: "",
loop: true,
}
//完成对customerView结构体customerService字段的初始化
customerView.customerService = service.Newcustomerservice()
//调用登录页面
//customerView.Login()
customerView.Login("", "")
}
系统测试页面如下:
参考:尚硅谷韩顺平Go语言核心编程
欢迎大家指正补充,感谢阅读。