237_尚硅谷_客户管理系统-主菜单和退出

1.项目功能实现-----显示主菜单和完成退出软件功能说概括1.项目功能实现-----显示主菜单和完成退出软件功能说概括

2.customer.go: customer定义客户字段结构体,并使用工厂模式返回customer实例2.customer定义客户字段结构体,并使用工厂模式返回customer实例

package model

// todo 声明一个Customer结构体, 表示一个客户信息
type Customer struct {
	Id     int
	Name   string
	Gender string
	Age    int
	Phone  string
	Email  string
}

// todo 使用一个工厂模式,返回一个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,
	}
}

3.customerService.go: 该CustomerService, 完成对Customer的操作, 包括数据的增删改查3.该CustomerService, 完成对Customer的操作, 包括数据的增删改查

package service

import "customerMange/model"

// todo 该CustomerService, 完成对Customer的操作, 包括
// todo 增删改查
type CustomerService struct {
	customers   []model.Customer // * 定义切片,customers
	customerNum int              // * 声明一个字段, 表示当前切片含有多少个客户, 该字段后续还可以作为新客户的id+1
}

4.customerView.go: 显示主菜单的显示逻辑

4.显示主菜单的显示逻辑

package main

import (
	"fmt"
)

type customerView struct {
	// 定义必要的字段
	key  string // * 接收用户输入
	loop bool   // * 表示是否循环显示主菜单

}

// todo 显示主菜单
func (this *customerView) mainMenu() {
	for {
		fmt.Println("---------------客户信息管理软件---------------")
		fmt.Println("               1 添加客户                    ")
		fmt.Println("               2 修改客户                    ")
		fmt.Println("               3 删除客户                    ")
		fmt.Println("               4 客户列表                    ")
		fmt.Println("               5 退出                        ")
		fmt.Printf("               请选择(1-5): ")
		// todo 执行终端输入
		fmt.Scanln(&this.key)
		switch this.key {
		case "1":
			fmt.Println("1 添加客户")
		case "2":
			fmt.Println("2 修改客户")
		case "3":
			fmt.Println("3 删除客户")
		case "4":
			fmt.Println("4 客户列表")
		case "5":
			fmt.Println("5 退出")
			this.loop = false
		default:
			fmt.Println("输入有误, 请重新输入!")
		}

		// todo this.loop默认数true, 如果为取反为false, 退出程序,终止循环
		if !this.loop {
			break
		}
	}
	fmt.Println("`客户关系管理系统`已退出")
}

func main() {
	// 在主函数中,创建一个customerView, 并运行显示主菜单......
	customerView := customerView{
		key:  "",
		loop: true,
	}
	key := customerView.key
	loop := customerView.loop
	fmt.Printf("Terminal input key: %v, loop: %v", key, loop)
	// 调用显示主菜单
	customerView.mainMenu()
}

5.显示菜单和退出程序的运行结果image

posted on 2026-02-06 11:19  与太阳肩并肩  阅读(1)  评论(0)    收藏  举报

导航