客户关系管理系统版本一(函数式)
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// 实现一个客户信息管理系统
// 实现客户信息的增删改查以及保存
// customers []
// 写一个程序最核心的问题:数据模型创建
// Customer
type Customer struct {
ID int
Name string
Age int
Email string
Phone string
}
var Customers []Customer
func customerExistByID() (index int) {
var id int
fmt.Println("请输入客户ID:")
fmt.Scan(&id)
index = -1
for i, c := range Customers {
if c.ID == id {
index = i
}
}
return
}
func addCustomer() {
// 添加客户
var name string
var email string
var phone string
var age int
fmt.Println("请输入客户姓名:")
fmt.Scan(&name)
fmt.Println("请输入客户年龄:")
fmt.Scan(&age)
fmt.Println("请输入客户邮箱:")
fmt.Scan(&email)
fmt.Println("请输入客户手机号:")
fmt.Scan(&phone)
// 实例化Customer对象
newCustomer := Customer{len(Customers) + 1, name, age, email, phone}
//添加到customers
Customers = append(Customers, newCustomer)
fmt.Println("添加成功!")
fmt.Println("Customers:", Customers)
}
func listCustomer() {
// 查询所有客户
for i, customerStruct := range Customers {
fmt.Printf("%d. ID:%d 客户姓名:%s 年龄:%d,邮箱:%s,电话:%s\n", i+1, customerStruct.ID, customerStruct.Name, customerStruct.Age, customerStruct.Email, customerStruct.Phone)
}
}
func GetOneCustomer() {
index := customerExistByID()
if index == -1 {
fmt.Println("没有符合条件的查询ID!")
} else {
fmt.Printf("ID:%d 客户姓名:%s 年龄:%d,邮箱:%s,电话:%s\n", Customers[index].ID, Customers[index].Name, Customers[index].Age, Customers[index].Email, Customers[index].Phone)
}
}
func updateCustomer() {
index := customerExistByID()
if index == -1 {
fmt.Println("没有符合条件的修改ID!")
} else {
fmt.Printf("ID:%d 客户姓名:%s 年龄:%d,邮箱:%s,电话:%s\n", Customers[index].ID, Customers[index].Name, Customers[index].Age, Customers[index].Email, Customers[index].Phone)
//修改
fmt.Println(`
修改属性:
1. 姓名
2. 年龄
3. email
4. phone`)
var attr int
fmt.Print("请输入修改属性序号:")
fmt.Scan(&attr)
switch attr {
case 1:
// 修改姓名
var name string
fmt.Print("请输入姓名:")
fmt.Scan(&name)
Customers[index].Name = name
case 2:
// 修改年龄
case 3:
// 修改email
case 4:
// 修改phone
}
}
}
func deleteCustomer() {
index := customerExistByID()
if index == -1 {
fmt.Println("没有符合条件的修改ID!")
return
}
// 查到ID
delID := Customers[index].ID
Customers = append(Customers[:index], Customers[index+1:]...)
fmt.Printf("ID%d客户删除成功", delID)
}
func save() {
// 基于json文件字符串的序列化,写入磁盘
jsonBytes, _ := json.Marshal(Customers)
ioutil.WriteFile("customers.json", jsonBytes, 0666)
}
func initData() {
jsonBytes, _ := ioutil.ReadFile("customers.json")
var data []Customer
json.Unmarshal(jsonBytes, &data)
Customers = data
}
func main() {
// 数据初始化
// 文件读操作
initData()
for true {
fmt.Println(`
1. 添加客户
2. 查询所有客户
3. 查询客户
4. 修改客户信息
5. 删除客户
6. 保存
7. 退出程序`)
var choice int
fmt.Print("请输入您的选择:")
fmt.Scan(&choice)
switch choice {
case 1:
addCustomer()
case 2:
listCustomer()
case 3:
// 查询客户
GetOneCustomer()
case 4:
// 修改客户信息
updateCustomer()
case 5:
// 删除客户
deleteCustomer()
case 6:
// 保存
save()
case 7:
// 退出程序
default:
fmt.Println("无效输入")
}
}
}
客户关系管理系统二(面向对象)
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// 实现一个客户信息管理系统
// 实现客户信息的增删改查以及保存
// customers []
// 写一个程序最核心的问题:数据模型创建
// Customer
type Customer struct {
ID int
Name string
Age int
Email string
Phone string
}
type CustomerService struct {
Customers []Customer
}
func (cs *CustomerService) customerExistByID() (index int) {
var id int
fmt.Println("请输入客户ID:")
fmt.Scan(&id)
index = -1
for i, c := range cs.Customers {
if c.ID == id {
index = i
}
}
return
}
func (cs *CustomerService) addCustomer() {
// 添加客户
var name string
var email string
var phone string
var age int
fmt.Println("请输入客户姓名:")
fmt.Scan(&name)
fmt.Println("请输入客户年龄:")
fmt.Scan(&age)
fmt.Println("请输入客户邮箱:")
fmt.Scan(&email)
fmt.Println("请输入客户手机号:")
fmt.Scan(&phone)
// 实例化Customer对象
newCustomer := Customer{len(cs.Customers) + 1, name, age, email, phone}
//添加到customers
cs.Customers = append(cs.Customers, newCustomer)
fmt.Println("添加成功!")
fmt.Println("cs Customers111:", cs.Customers)
}
func (cs *CustomerService) listCustomer() {
fmt.Println("cs Customers222:", cs.Customers)
// 查询所有客户
for i, customerStruct := range cs.Customers {
fmt.Printf("%d. ID:%d 客户姓名:%s 年龄:%d,邮箱:%s,电话:%s\n", i+1, customerStruct.ID, customerStruct.Name, customerStruct.Age, customerStruct.Email, customerStruct.Phone)
}
}
func (cs *CustomerService) GetOneCustomer() {
index := cs.customerExistByID()
if index == -1 {
fmt.Println("没有符合条件的查询ID!")
} else {
fmt.Printf("ID:%d 客户姓名:%s 年龄:%d,邮箱:%s,电话:%s\n", cs.Customers[index].ID, cs.Customers[index].Name, cs.Customers[index].Age, cs.Customers[index].Email, cs.Customers[index].Phone)
}
}
func (cs *CustomerService) updateCustomer() {
index := cs.customerExistByID()
if index == -1 {
fmt.Println("没有符合条件的修改ID!")
} else {
fmt.Printf("ID:%d 客户姓名:%s 年龄:%d,邮箱:%s,电话:%s\n", cs.Customers[index].ID, cs.Customers[index].Name, cs.Customers[index].Age, cs.Customers[index].Email, cs.Customers[index].Phone)
//修改
fmt.Println(`
修改属性:
1. 姓名
2. 年龄
3. email
4. phone`)
var attr int
fmt.Print("请输入修改属性序号:")
fmt.Scan(&attr)
switch attr {
case 1:
// 修改姓名
var name string
fmt.Print("请输入姓名:")
fmt.Scan(&name)
cs.Customers[index].Name = name
case 2:
// 修改年龄
case 3:
// 修改email
case 4:
// 修改phone
}
}
}
func (cs *CustomerService) deleteCustomer() {
index := cs.customerExistByID()
if index == -1 {
fmt.Println("没有符合条件的修改ID!")
return
}
// 查到ID
delID := cs.Customers[index].ID
cs.Customers = append(cs.Customers[:index], cs.Customers[index+1:]...)
fmt.Printf("ID%d客户删除成功", delID)
}
func (cs *CustomerService) save() {
// 基于json文件字符串的序列化,写入磁盘
jsonBytes, _ := json.Marshal(cs.Customers)
ioutil.WriteFile("customers.json", jsonBytes, 0666)
}
func (cs *CustomerService) initData() {
jsonBytes, err := ioutil.ReadFile("customers.json")
if err != nil {
fmt.Println("err", err)
} else {
var data []Customer
json.Unmarshal(jsonBytes, &data)
fmt.Println("data", data)
cs.Customers = data
}
}
func main() {
// CustomerService实例化
cs := &CustomerService{}
// 文件读操作
cs.initData()
for true {
fmt.Println(`
1. 添加客户
2. 查询所有客户
3. 查询客户
4. 修改客户信息
5. 删除客户
6. 保存
7. 退出程序`)
var choice int
fmt.Print("请输入您的选择:")
fmt.Scan(&choice)
switch choice {
case 1:
cs.addCustomer()
case 2:
cs.listCustomer()
case 3:
// 查询客户
cs.GetOneCustomer()
case 4:
// 修改客户信息
cs.updateCustomer()
case 5:
// 删除客户
cs.deleteCustomer()
case 6:
// 保存
cs.save()
case 7:
// 退出程序
default:
fmt.Println("无效输入")
}
}
}