【韩顺平Golang】模拟实现基于文本界面的《客户信息管理系统》(切片、结构体、MVC)

需求分析

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,
		}
}

//重写String方法
func (this *Customer)String() string {
	var info string = 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 (
	"fmt"
	"CustomerInfoAdminSys/Model"
)

//声明一个CustomerServer结构体,该结构体完成对Customer的CRUD操作
type CustomerService struct{
	customer []Model.Customer	//用于存储多个Customer对象的切片
	customerNum int //用于统计存储Customer对象个数的属性
}

//使用一个工厂函数,用于返回一个CustomerService对象
func NewCustomerService() *CustomerService {
	//初始化一个客户信息
	service := CustomerService{
		customerNum : 1,
	}
	customer := Model.NewCustomer(1,"张三","男",30,"40066668888","zhangsan@qq.com")	//工厂函数创建一个Customer
	service.customer = append(service.customer, customer)	//追加到切片
	return &service
}

//返回客户信息的切片
func (this *CustomerService)GetCustomers() []Model.Customer {
	return this.customer	//所有的customer对象,切片类型
}

//添加客户
func (this *CustomerService)Add(name string, sex string, age int,
	 phone string, email string) bool {
		this.customerNum++ 	//id动态增长
		customer := Model.NewCustomer(this.customerNum, name, sex, age, phone, email)	//工厂函数创建一个Customer
		this.customer = append(this.customer, customer)	//追加到切片
		return true
}

//删除客户
func (this *CustomerService)Delete(id int) bool {
	 index := this.FindIndexById(id)
	 if index != -1 {
		this.customer = append(this.customer[:index],this.customer[index+1:]...)	//去掉下标为index的切片
		return true 	//删除成功
	 }
	 return false		//删除失败
}

//删除客户
func (this *CustomerService)Update(id int) bool {
	//查看该用户是否存在
	if this.FindIndexById(id) == -1 {
		fmt.Println("该用户不存在")
		return false
	}

	customer := this.FindCustomerById(id)	//获取该用户信息
	var name string
	var sex string
	var age int
	var phone string
	var email string
	fmt.Printf("姓名(%v):",customer.Name)
	fmt.Scanln(&name)
	if name != "" {
		customer.Name = name
	}
	fmt.Printf("性别(%v):",customer.Gender)
	fmt.Scanln(&sex)
	if sex != "" {
		customer.Gender = sex
	}
	fmt.Printf("年龄(%v):",customer.Age)
	fmt.Scanln(&age)
	if age != 0 {
		customer.Age = age
	}
	fmt.Printf("电话(%v):",customer.Phone)
	fmt.Scanln(&phone)
	if phone != "" {
		customer.Phone = phone
	}
	fmt.Printf("邮箱(%v):",customer.Email)
	fmt.Scanln(&email)
	if email != "" {
		customer.Email = email
	}
	return true
}


//通过id查找客户在切片当中存储的下标,未找到返回-1
func (this *CustomerService)FindIndexById(id int) int {
	for index, value := range this.customer {
		if value.Id == id {
			return index	//找到
		}
	}
	return -1	//未找到
}

//通过id查找客户信息
func (this *CustomerService)FindCustomerById(id int) *Model.Customer {
	for i := 0; i < len(this.customer); i++ {
		if this.customer[i].Id == id {
			return &this.customer[i]
		}
	}
	return &Model.Customer{} //未找到
}

CustomerView.go

package View

import (
	"fmt"
	"CustomerInfoAdminSys/Service"
)

//声明CostomerView结构体
type ConstomerView struct {
	Key string	//用于接收用户输入的字段
	Loop bool		//表示是否循环的显示主菜单
	CustomerService *Service.CustomerService	//用于存储CustomerService对象的属性
}

//用于床架CustomerView的工厂函数
func NewCustomerView() *ConstomerView {
	//创建CustomerView
	customerView := ConstomerView{
		Key : "",
		Loop: true,
		CustomerService : Service.NewCustomerService(),
	}
	return &customerView
}

//用于显示主菜单的方法
func (this *ConstomerView)MainMenu() {
	for {
		fmt.Println("---------------客户信息管理软件---------------")
		fmt.Println("\t\t1 添 加 客 户")
		fmt.Println("\t\t2 修 改 客 户")
		fmt.Println("\t\t3 删 除 客 户")
		fmt.Println("\t\t4 客 户 列 表")
		fmt.Println("\t\t5 退       出")
		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.exit()
			default:
				fmt.Println("您输入的选择有误,请重新输入!")
		}
		if !this.Loop {
			break
		}
	}
}

//显示客户列表
func (this *ConstomerView)list() {
	//获取所有客户的信息
	customers := this.CustomerService.GetCustomers()
	//显示
	fmt.Println("---------------客户列表---------------")
	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱")
	for _, value := range customers {
		fmt.Println(&value)	//Customer对象重写过String()方法,默认调用String()方法
	}
	fmt.Println("\n-----------客户列表显示完成-----------\n\n")
}

//添加客户
func (this *ConstomerView)add() {
	var name string
	var sex string
	var age int
	var phone string
	var email string
	fmt.Println("---------------添加客户---------------")
	
	fmt.Print("姓名:")
	fmt.Scanln(&name)

	fmt.Print("性别:")
	fmt.Scanln(&sex)

	fmt.Print("年龄:")
	fmt.Scanln(&age)

	fmt.Print("电话:")
	fmt.Scanln(&phone)

	fmt.Print("邮箱:")
	fmt.Scanln(&email)
	if this.CustomerService.Add(name, sex, age, phone, email) {
		fmt.Println("---------------添加完成---------------")
	}else {
		fmt.Println("---------------添加失败---------------")
	}
}

//删除客户
func (this *ConstomerView)delete() {
	fmt.Println("---------------删除客户---------------")
	var id int
	fmt.Print("请选择待删除的客户编号(-1退出):")
	fmt.Scanln(&id)
	if id == -1 {
		fmt.Println("退出了删除客户!~")
		return 
	}
	var key string
	fmt.Print("确认是否删除(Y/N):")
	fmt.Scanln(&key)
	if(key == "Y" || key == "y"){
		if this.CustomerService.Delete(id) {
			fmt.Println("---------------删除完成---------------")
		}else {
			fmt.Println("---------------删除失败,未找到该客户信息---------------")
		}
	}else {
		fmt.Println("---------------取消删除---------------")
	}
}

//修改客户信息
func (this *ConstomerView)update() {
	fmt.Println("---------------修改客户---------------")
	var id int
	fmt.Print("请选择待修改的客户编号(-1退出):")
	fmt.Scanln(&id)
	if id == -1 {
		fmt.Println("退出了修改客户!~")
		return 
	}

	if this.CustomerService.Update(id) {
		fmt.Println("---------------修改完成---------------")
	} else {
		fmt.Println("---------------修改失败---------------")
	}
}

func (this *ConstomerView)exit() {
	var key string
	for {
		fmt.Print("确认是否退出(Y/N):")
		fmt.Scanln(&key)
		if key == "Y" || key == "y"{
			fmt.Println("退出成功!~")
			this.Loop = false
			return
		}else if key == "N" || key == "n" {
			return
		}
	}
}

go.mod

module CustomerInfoAdminSys
go 1.19

Main.go

package main
import(
	"CustomerInfoAdminSys/View"
)
func main() {
	//显示主菜单
	View.NewCustomerView().MainMenu()
}

运行

go run Main.go

测试样例

添加

在这里插入图片描述

删除

在这里插入图片描述

修改

在这里插入图片描述

退出

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孑然R

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值