golang项目02_客户信息管理系统

golang项目02_客户信息管理系统

主菜单界面:

--------------------客户信息管理系统--------------------

​ 1 添 加 用 户

​ 2 修 改 用 户

​ 3 删 除 用 户

​ 4 客 户 用 户

​ 5 退 出

​ 请选择(1~5):

程序框架图:

设计文档:


类图

用例图、流程图、数据表(给或者不给)

功能模块:

用户管理模块、订单管理模块、购物车模块、支付模块、安全模块


用户管理框架如:

程序框图:分析该模块有多少个文件(类),和各个类之间的调用关系,程序元需要按照架构师的要求,进行分层;

先进性程序框架图,然后在进行代码实现。

思路分析:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zikprBp8-1649059818192)(C:\Users\黑色\AppData\Roaming\Typora\typora-user-images\image-20220322204946203.png)]

所以说这一个项目总共需要使用三个文件实现:

model:
package model

import "fmt"

type Customer struct{
	Id int
	name string
	gender string
	age int
	phonenumber string
	emil string
}//一个客户的模板
//工厂模式,相当于一个构造方法

func NewCustomer(id int,name string,
	gender string,age int,phonenumber string,
	emil string) Customer{
	customer:=Customer{
		Id : id,
		name : name,
		gender : gender,
		age : age ,
		phonenumber: phonenumber,
		emil: emil,
	}
	return customer
}//一个构造函数
func NewCustomer2(name string,
	gender string,age int,phonenumber string,
	emil string) Customer{
	customer:=Customer{
		name : name,
		gender : gender,
		age : age ,
		phonenumber: phonenumber,
		emil: emil,
	}
	return customer
}//一个构造函数

func (this Customer)Tostring() string{
	return fmt.Sprintf("%v      %v      %v       %v      %v      %v\t",
		this.Id,this.name,this.gender,this.age,this.phonenumber,this.emil)
}


func (this Customer)GetName()string{
	return this.name
}
func (this Customer)GetAge()int{
	return this.age
}
func (this Customer)GetPhone()string{
	return this.phonenumber
}
func (this Customer)GetEmile()string{
	return this.emil
}

func (this Customer)GetGander()string{
	return this.gender
}
service:
package service

import (
	"fmt"
	"go_code/customerManager/model"
)

//该结构体完成对customer的操作
//增加、删除、改、查

type CustomerService struct{
	customers []model.Customer
	//声明一个字段,表示当前切片含有多少个客户
	//该字段后面,还可以作为新客户id+1
	customerNum int
}

func NewcustomerService() CustomerService{
	CustomerService := CustomerService{}
	CustomerService.customerNum=1
	customer := model.NewCustomer(1,"jack","男",18,
		"1879653","qqzhangsan.com")
	CustomerService.customers= append(CustomerService.customers, customer)
	return CustomerService
}//一个构造方法,

func (this *CustomerService) List() []model.Customer{
	return this.customers
}//4客户列表的功能

func (this *CustomerService) Add(customer model.Customer) bool{
	//这里的this是一个指针,引用假如不是引用则永远可能保存多个用户
	this.customerNum++
	customer.Id = this.customerNum
	this.customers=append(this.customers,customer)//添加
	return true
}//1客户添加

//删除功能
func (this *CustomerService) Delete(id int) bool{
	index := this.FindByid(id)//进行查找
	if index == -1{
		return false//没有找到返回是一个false
	}
	this.customers = append(this.customers[:index], this.customers[index+1:]...)
	//这一个语句就是删除找到的id
	return true//到了并且删除了就返回的true
}
//修改功能

func (this *CustomerService) Update(index int) {
	var updateName string;
	var updateGander string
	var updateAge int
	var updatePhone string
	var updateEmil string
	fmt.Print("姓名:"+"("+this.customers[index].GetName()+")")
	fmt.Scanln(&updateName)
	fmt.Print("性别:"+"(",this.customers[index].GetGander(),")")
	fmt.Scanln(&updateGander)
	fmt.Print("年龄:"+"(",this.customers[index].GetAge(),")")
	fmt.Scanln(&updateAge)
	fmt.Print("电话号码:"+"(",this.customers[index].GetPhone(),")")
	fmt.Scanln(&updatePhone)
	fmt.Print("邮箱:"+"(",this.customers[index].GetEmile(),")")
	fmt.Scanln(&updateEmil)
	customer := model.NewCustomer(index+1,
		updateName,updateGander,updateAge,updatePhone,
		updateEmil)
	this.customers[index] = customer
	fmt.Println("-----------修改成功------------")
}



//根据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来进行查找客户//找到了返回的是客户的id号
}
view:
package main

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

type customerView struct{
	key string
	flag bool
	customerService service.CustomerService//新增加一个字段
}




//4
func (this customerView) list(){
	customers := this.customerService.List()//调用主菜单里面的service的切片
	fmt.Println("--------------------客 户 列 表--------------------")
	fmt.Println("编号      姓名      性别       年龄      电话      邮箱")
	for i:=0;i<len(this.customerService.List());i++{
		fmt.Println(customers[i].Tostring())
	}
	fmt.Println("--------------------客户列表完成--------------------")
}
//1
func (this *customerView) add() {
	fmt.Println("--------------------添 加 用 户--------------------")
	fmt.Println("姓名 :")
	var name string
	fmt.Scanf("%s", &name)
	fmt.Println("性别 :")
	var gander string
	fmt.Scanf("%s", &gander)
	fmt.Println("年龄 :")
	var age int
	fmt.Scanf("%d", &age)
	fmt.Println("电话 :")
	var phone string
	fmt.Scanf("%s", &phone)
	fmt.Println("邮箱 :")
	var emile string
	fmt.Scanf("%s", &emile)
	customer := model.NewCustomer2(name, gander, age, phone, emile)
	if this.customerService.Add(customer) {
		fmt.Println("-----------添加成功---------")
	} else {
		fmt.Println("添加失败")
	}

}
//3
func (this *customerView)delete(){
	fmt.Println("--------------------删 除 用 户--------------------")
	fmt.Println("请输入要删除的用户编号(-1退出):")
	choice := 0
	fmt.Scanln(&choice)
	if choice==-1{
		return
	}
	if this.customerService.Delete(choice){
		fmt.Println("---------删除成功-------")
	}else{
		fmt.Println("-------删除失败,客户的编号不存在----------")
	}

}

func (this *customerView)mainMenu(){
	this.flag = true;
	this.key = " "
	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)//接收一个key
		//功能区
		switch this.key{
		case "1":
			this.add()

		case "2":
			this.Update()
		case "3":
			this.delete()
		case "4":
			this.list()
		case "5":
			this.flag =this.exit()
		default:
			fmt.Println("输入有误,请重新输入~···")
		}
		if !this.flag{
			break;
		}
	}
		fmt.Println("您已经退出了客户管理系统····")
}

func (this customerView)exit() bool{
		flag := false

	for{
		fmt.Println("是否退出(y/n):")
		key := " "
		fmt.Scanln(&key)
		if key=="Y"||key=="y"{
			flag =false
			break
		}

		if key=="N"||key=="n"{
			flag=true
			break
		}
	}
	return flag
}

func (this *customerView)Update(){
	index := 0
	fmt.Print("--------------------修 改 用 户--------------------\n")
	fmt.Print("请输入要修改客户的id(-1退出):")
	id := 0
	fmt.Scanln(&id)
	if id == -1{
		return
	}
	for{
		choice := id
		index = this.customerService.FindByid(choice)
		if id==-1{
			fmt.Print("没有该id号的用户,修改失败,请重新输入:")
			fmt.Scanln(&choice)
		}else{
			break
		}
	}
	this.customerService.Update(index)
}

func main(){
		var viewMenu customerView
		viewMenu.customerService = service.NewcustomerService()
		viewMenu.mainMenu()
}

his.customerService.FindByid(choice)
if id==-1{
fmt.Print(“没有该id号的用户,修改失败,请重新输入:”)
fmt.Scanln(&choice)
}else{
break
}
}
this.customerService.Update(index)
}

func main(){
var viewMenu customerView
viewMenu.customerService = service.NewcustomerService()
viewMenu.mainMenu()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值