客户管理系统--golang

src下分了三个文件夹分别是model、view、service
model里面的代码

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

}

//构建不带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
}

service里的代码

package service

import (
	"../model"
)

//customerService完成对customer的操作
type CustomerService struct {
	customers []model.Customer
	//声明字段,表示当前切片含有多少个客户,该字段可以做客户编号
	customerNum int
}

//编写一个方法 ,可以返回 *CustomerService
func NewCustomerService() *CustomerService {
	//为了看到客户在切片中,初始化一个客户
	customerService := &CustomerService{}
	customerService.customerNum = 1
	//customer := model.NewCustomer(1, "张三", "男", 20, "112", "12@qq.com")
	//customerService.customers = append(customerService.customers, customer)
	return customerService
}

//添加客户到customer切片
//一定要用指针的方式
func (this *CustomerService) Add(customer model.Customer) bool {
	//确定分配一个id的规则,就是添加的顺序
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers, customer)
	return true
}

//修改用户信息
func (this *CustomerService) Edit(customer model.Customer) bool {
	//先判断要从修改的id是否在客户列表中
	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) List() []model.Customer {
	return this.customers
}

//根据id删除客户(从切片删除)
func (this *CustomerService) Delete(id int) bool {
	index := this.FindById(id)
	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.customer切片
	for i := 0; i < len(this.customers); i++ {
		if this.customers[i].Id == id {
			index = i
		}
	}
	return index
}

func (this *CustomerService) FindCustomerById(id int) model.Customer {
	index := -1
	//遍历this.customer切片
	for i := 0; i < len(this.customers); i++ {
		if this.customers[i].Id == id {
			index = i
		}
	}
	return this.customers[index]
}

view

package main

import (
	"fmt"

	"../model"
	"../service"
)

type customerView struct {
	key  string //接受用户输入
	loop bool   //表示是否循环显示主菜单
	//增加一个customerService字段
	customerService *service.CustomerService
}

func (this *customerView) showMenu() {
	for {
		fmt.Println("-----------------------------客户信息管理软件--------------------\n")
		fmt.Println("			1添	加 客	户			")
		fmt.Println("			2修	改	客	户			")
		fmt.Println("			3删	除	客	户			")
		fmt.Println("			4客	户	列	表			")
		fmt.Println("			5退	出			")
		fmt.Println("请选择1-5")
		fmt.Scan(&this.key)
		switch this.key {
		case "1":
			this.add()
		case "2":
			this.edit()
		case "3":
			this.detete()
		case "4":
			this.list()
		case "5":
			this.exit()
		default:
			fmt.Println("输入有误,请重新输入")
		}
		if !this.loop {
			break
		}
	}
	fmt.Println("你退出了软件的使用。。。。")
}

func (this *customerView) exit() {
	fmt.Println("确认是否退出(y/n)")
	for {
		fmt.Scan(&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) list() {
	//首先获取当前所有的客户信息(在切片中)
	customers := this.customerService.List()
	fmt.Println("-----------------------------客户列表展示--------------------\n")
	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱\n")
	for i := 0; i < len(customers); i++ {
		fmt.Println(customers[i].GetInfo())
	}
	fmt.Println("-----------------------------客户列表完成-------------------\n")
}

//得到用户的输入,信息构建新的客户,并完成添加
func (this *customerView) add() {
	fmt.Println("-----------------------------添加客户--------------------\n")
	fmt.Printf("姓名:")
	name := ""
	fmt.Scan(&name)
	fmt.Printf("性别")
	gender := ""
	fmt.Scan(&gender)

	fmt.Printf("年龄")
	age := 0
	fmt.Scan(&age)

	fmt.Printf("电话")
	phone := ""
	fmt.Scan(&phone)

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

}

//得到用户的输入,删除该id
func (this *customerView) detete() {
	fmt.Println("---------------------删除客户-----------------")
	fmt.Println("请选择待删除客户编号(-1退出)")
	id := -1
	fmt.Scan(&id)
	if id == -1 {
		return //放弃删除操作
	}
	fmt.Println("确认是否删除(y/n)")
	choice := ""
	fmt.Scan(&choice)
	if choice == "y" || choice == "Y" {
		//调用serviced的delete方法
		if this.customerService.Delete(id) {
			fmt.Println("-----------------------删除完成---------------------")
		} else {
			fmt.Println("-----------------------删除失败,输入的id号不存在---------------------")
		}
	}

}

func (this *customerView) edit() {
	fmt.Println("----------------------------修改客户信息--------------------\n")
	fmt.Println("请输入要修改的客户id,(-1)退出")

	id := -1
	fmt.Scan(&id)
	if id == -1 {
		return //放弃删除操作
	}

	fmt.Printf("姓名:")
	name := ""
	fmt.Scan(&name)

	// this.customerService.customers[id].name = name
	fmt.Printf("性别")
	gender := ""
	fmt.Scan(&gender)

	fmt.Printf("年龄")
	age := 0
	fmt.Scan(&age)

	fmt.Printf("电话")
	phone := ""
	fmt.Scan(&phone)

	fmt.Printf("邮箱")
	email := ""
	fmt.Scan(&email)

	//构建新的customer实例
	//注意,id号么有让用户输入,id是唯一的,需要系统分配
	customer := model.NewCustomer(id, name, gender, age, phone, email)
	if this.customerService.Edit(customer) {
		fmt.Println("----------------------修改完成-----------------")
	} else {
		fmt.Println("----------------------修改失败-----------------")
	}

}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值