从您的描述来看,您正在设计一个基于控制台输入输出的“客户信息系统”。这个系统的核心功能包括 **添加客户**、**修改客户**、**删除客户** 和 **显示客户列表**。下面是一个简要的设计说明以及一些关键点:
---
### 设计思路
#### 结构体定义
```go
type Customer struct {
Id int // 客户唯一标识符
Name string // 姓名
Gender string // 性别
Age int // 年龄
Phone string // 电话号码
Email string // 邮箱地址
}
```
每个客户的详细信息通过上述 `Customer` 结构体存储。
#### 功能模块划分
1. **主菜单**
提供用户选择操作的功能入口。
2. **添加客户**
用户可以输入新客户的姓名、性别、年龄、电话和邮箱等信息,并将其保存到系统的数据集合中(如切片数组)。
3. **修改客户**
根据提供的客户编号找到对应记录并允许更新其字段值。
4. **删除客户**
按照指定编号查找目标客户条目,并询问确认后移除该记录。
5. **展示所有客户**
将已录入的所有客户按表格形式打印出来,便于查看当前的数据状态。
---
### 示例代码片段(Go语言)
以下是部分实现参考代码:
```go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
// 定义结构体
type Customer struct {
Id int
Name string
Gender string
Age int
Phone string
Email string
}
var customers []Customer
var idCounter = 1
func showMenu() {
fmt.Println("---------------客户信息管理软件----------------")
fmt.Println("1 添加客户")
fmt.Println("2 修改客户")
fmt.Println("3 删除客户")
fmt.Println("4 查看客户列表")
fmt.Println("5 退出程序")
fmt.Print("请选择(1-5): ")
}
func addCustomer(scanner *bufio.Scanner) {
var name, gender, phone, email string
var age int
fmt.Println("\n> 添加客户")
fmt.Print("请输入姓名: ")
scanner.Scan()
name = scanner.Text()
fmt.Print("请输入性别: ")
scanner.Scan()
gender = scanner.Text()
fmt.Print("请输入年龄: ")
scanner.Scan()
age, _ = strconv.Atoi(scanner.Text())
fmt.Print("请输入电话: ")
scanner.Scan()
phone = scanner.Text()
fmt.Print("请输入邮箱: ")
scanner.Scan()
email = scanner.Text()
newCustomer := Customer{
Id: idCounter,
Name: name,
Gender: gender,
Age: age,
Phone: phone,
Email: email,
}
customers = append(customers, newCustomer)
idCounter++
fmt.Println("客户添加成功!")
}
func listCustomers() {
if len(customers) == 0 {
fmt.Println("暂无客户数据!")
return
}
fmt.Printf("%-5s %-10s %-8s %-6s %-15s %-20s\n", "编号", "姓名", "性别", "年龄", "电话", "邮箱")
for _, customer := range customers {
fmt.Printf("%-5d %-10s %-8s %-6d %-15s %-20s\n",
customer.Id, customer.Name, customer.Gender, customer.Age, customer.Phone, customer.Email)
}
}
func modifyCustomer(scanner *bufio.Scanner) {
fmt.Println("\n> 修改客户")
fmt.Print("请输入需要修改的客户编号(-1返回上一级): ")
scanner.Scan()
inputId, err := strconv.Atoi(scanner.Text())
if inputId == -1 || err != nil {
return
}
index := findIndexById(inputId)
if index == -1 {
fmt.Println("未找到匹配的客户编号!")
} else {
oldCustomer := &customers[index]
fmt.Printf("原信息 -> %v\n", oldCustomer)
fmt.Printf("> 是否修改【%s】的信息(y/n)? ", oldCustomer.Name)
scanner.Scan()
confirm := scanner.Text()
if confirm == "y" {
fmt.Print("新的名字[留空则保持不变]: ")
scanner.Scan()
textName := scanner.Text()
if textName != "" {
oldCustomer.Name = textName
}
fmt.Print("新的性别[留空则保持不变]: ")
scanner.Scan()
textGender := scanner.Text()
if textGender != "" {
oldCustomer.Gender = textGender
}
fmt.Print("新的年龄[留空则保持不变]: ")
scanner.Scan()
textAge := scanner.Text()
if textAge != "" {
numAge, _ := strconv.Atoi(textAge)
oldCustomer.Age = numAge
}
fmt.Print("新的电话[留空则保持不变]: ")
scanner.Scan()
textPhone := scanner.Text()
if textPhone != "" {
oldCustomer.Phone = textPhone
}
fmt.Print("新的邮件地址[留空则保持不变]: ")
scanner.Scan()
textEmail := scanner.Text()
if textEmail != "" {
oldCustomer.Email = textEmail
}
fmt.Println("客户信息修改成功!")
}
}
}
func deleteCustomer(scanner *bufio.Scanner) {
fmt.Println("\n> 删除客户")
fmt.Print("请输入需要删除的客户编号(-1返回上一级): ")
scanner.Scan()
inputId, err := strconv.Atoi(scanner.Text())
if inputId == -1 || err != nil {
return
}
index := findIndexById(inputId)
if index == -1 {
fmt.Println("未找到匹配的客户编号!")
} else {
fmt.Printf("确定要删除客户:%+v 吗?(y/n)", customers[index])
scanner.Scan()
confirm := scanner.Text()
if confirm == "y" {
customers = append(customers[:index], customers[index+1:]...)
fmt.Println("删除成功!")
} else {
fmt.Println("取消删除操作...")
}
}
}
func findIndexById(id int) int {
for i, c := range customers {
if c.Id == id {
return i
}
}
return -1
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for {
showMenu()
scanner.Scan()
option, _ := strconv.Atoi(scanner.Text())
switch option {
case 1:
addCustomer(scanner)
case 2:
modifyCustomer(scanner)
case 3:
deleteCustomer(scanner)
case 4:
listCustomers()
case 5:
fmt.Println("感谢使用本系统,再见!")
os.Exit(0)
default:
fmt.Println("无效选项,请重新选择!")
}
}
}
```
---
### 输出示例
运行结果类似于以下内容:
```
---------------客户信息管理软件----------------
1 添加客户
2 修改客户
3 删除客户
4 查看客户列表
5 退出程序
请选择(1-5): 1
...
```
---