Golang客户管理系统(含修改)

项目需求说明

模拟实现基于文本界面的《客户信息管理软件》

该软件能够实现对客户对象的插入、修改和删除(用切片实现),并囊都打印客户明细表

目标

  • 模拟实现一个基于文本界面的《客户信息管理软件》
  • 进一步掌握编程技巧和调试技巧,熟悉面向对象编程
  • 主要设计一下知识点:
    • 切片的插入、删除和替换
    • 多对象协同工作

框架图

在这里插入图片描述

源码

在这里插入图片描述

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 (
	"fmt"
	"go_code/customer/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, "kun", "man", 20, "168", "@xinlang.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 {

	//确定一个添加的规则,就是添加的顺序
	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
}

//根据id修改切片中的用户
func (this *CustomerService) UpData(id int) bool {
	index := this.FindByid(id)
	if index == -1 {
		return false
	}
	name := ""
	gender := ""
	age := 0
	phone := ""
	email := ""
	fmt.Printf("姓名(%v):", this.customers[index].Name)
	fmt.Scanln(&name)
	fmt.Printf("性别(%v):", this.customers[index].Gender)
	fmt.Scanln(&gender)
	fmt.Printf("年龄(%v):", this.customers[index].Age)
	fmt.Scanln(&age)
	fmt.Printf("电话(%v):", this.customers[index].Phone)
	fmt.Scanln(&phone)
	fmt.Printf("邮箱(%v):", this.customers[index].Email)
	fmt.Scanln(&email)
	this.customers[index].Name = name
	this.customers[index].Gender = gender
	this.customers[index].Age = age
	this.customers[index].Phone = phone
	this.customers[index].Email = email
	return true
}

customerview.go

package main

import (
	"fmt"
	"go_code/customer/model"
	"go_code/customer/service"
)

type customerView struct {
	//定义必要的字段
	key  string //接受用户的输入
	loop bool   //表示是否循环的显示主菜单
	//增加一个字段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].GetInfo())
	}
	fmt.Printf("--------------客户列表完成---------------\n\n")
}

//得到用户的输入的信息,构建新的客户,并完成添加
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)

	//构建一个新的Customer实例
	//注意id号,没有让用户输入,id是唯一的,需要系统分配
	customer := model.NewCustomer2(name, gender, age, phone, email)
	//调用
	if this.customerService.Add(customer) {
		fmt.Println("--------------添加成功---------------")
	} else {
		fmt.Println("--------------添加失败---------------")
	}

}

//得到用户输入的id,删除该id的对应的用户
func (this *customerView) delete() {
	fmt.Println("--------------删除用户---------------")
	fmt.Println("请选择待删除的客户编号(-1)退出:")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return //放弃删除操作
	}
	fmt.Println("确认是否删除(Y/N):")
	//加入一个循环判断,直到用户输入y,或者n才退出
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		//调用customerService的delete方法
		if this.customerService.Delete(id) {
			fmt.Println("----------------删除完成---------------")
		} else {
			fmt.Println("----------------删除失败,输入的id不存在---------------")
		}
	}

}

//修改客户信息
func (this *customerView) update() {
	fmt.Println("----------修改客户----------")
	fmt.Println("请选择待修改用户的编号(-1退出):")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	fmt.Println("确认是否修改?y/n")
	choose := ""
	fmt.Scanln(&choose)
	if choose == "y" {
		if this.customerService.UpData(id) {
			fmt.Println("----------修改成功----------")
		} else {
			fmt.Println("----------修改失败----------")
		}
	} else {
		fmt.Println("取消修改")
	}
}

//退出该软件
func (this *customerView) exit() {
	fmt.Println("确认是否退出(y/n):")
	for {
		fmt.Scanln(&this.key)
		if this.key == "Y" || this.key == "y" || this.key == "n" || this.key == "N" {
			break
		}
		fmt.Println("你的输入有误,企鹅人是否退出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.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("你退出了客户关系管理系统")
}

func main() {
	//在main函数中,创建一个customerView,并运行显示主菜单
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//这里完成对customerView结构体的customerService字段的初始化
	customerView.customerService = service.NewCustomerService()
	//显示主菜单
	customerView.mainMenu()
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值