實作練習-客戶名單管理系統
尚硅谷Golang課

實作練習-客戶名單管理系統

model

type Customer struct {
	Id     int
	Name   string
	Gender string
	Age    int
	Phone  string
	Email  string
}

//使用工廠模式返回一個實例
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,
	}
}

//返回用戶訊息
func (c Customer) GetInfo() string {
	info := fmt.Sprintf("%v	%v	%v	%v	%v	%v",
		c.Id, c.Name, c.Gender, c.Age, c.Phone, c.Email)
	return info

}

//用戶創建
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 (c *Customer) Reset(name string, gender string, age int, phone string, email string) {
	if name != "" {
		c.Name = name
	}
	if gender != "" {
		c.Gender = gender
	}
	if age != 0 {
		c.Age = age
	}
	if phone != "" {
		c.Phone = phone
	}
	if email != "" {
		c.Email = email
	}

}

view


type customerView struct {
	key             string //接收用戶輸入
	loop            bool   //是否循環回到主頁面
	customerService *service.CustomerService
}

//顯示主菜單
func (cv *customerView) mainMenu() {
	for {
		fmt.Println("\n---客戶訊息管理系統---")
		fmt.Println("   1 添加新客戶")
		fmt.Println("   2 修改客戶")
		fmt.Println("   3 刪除客戶")
		fmt.Println("   4 客戶列表")
		fmt.Println("   5 退出")
		fmt.Print("請選擇(1-5)...")
		fmt.Scanln(&cv.key)
		switch cv.key {
		case "1":
			fmt.Println("你選擇的是...1 添加新客戶")
			cv.add()
		case "2":
			fmt.Println("你選擇的是...2 修改客戶")
		case "3":
			fmt.Println("你選擇的是...3 刪除客戶")
			cv.delete()
		case "4":
			fmt.Println("你選擇的是...4 客戶列表")
			cv.list()
		case "5":
			fmt.Println("你選擇的是...5 退出")
			cv.loop = false
		default:
			fmt.Println("輸入有誤,請重新輸入...")
		}
		if !cv.loop {
			break
		}
	}
	fmt.Println("你已成功退出客戶訊息管理系統!")
}

//得到用戶輸入並添加
func (cv *customerView) add() {
	fmt.Println("---添加新客戶---")
	fmt.Println("輸入姓名...")
	name := ""
	fmt.Scanln(&name)
	fmt.Println("輸入性別...")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Println("輸入年齡...")
	age := 0
	fmt.Scanln(&age)
	fmt.Println("輸入電話...")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Println("輸入信箱...")
	email := ""
	fmt.Scanln(&email)
	//構建一個Customer實例
	//id要系統分配
	customer := model.NewCustomer2(name, gender, age, phone, email)
	//調用
	if cv.customerService.Add(customer) {
		fmt.Println("添加完成!")
	} else {
		fmt.Println("添加失敗!")
	}

}

//修改
// func (cs *customerView) Reset() {
// 	var id int
// 	fmt.Println("请输入需要修改的用户ID(无需请输入-1)")
// 	fmt.Scanln(&id)
// 	if id == -1 {
// 		return
// 	}
// 	resetIndex := cs.customerService.FindById(id)
// 	if resetIndex == -1 {
// 		fmt.Println("改用户不存在")
// 	} else {
// 		_, name, gender, age, phone, email := cs.customerService[resetIndex].GetInfo()
// 		fmt.Printf("姓名(%v):", name)
// 		fmt.Scanln(&name)
// 		fmt.Printf("性别(%v):", gender)
// 		fmt.Scanln(&gender)
// 		fmt.Printf("年龄(%v):", age)
// 		fmt.Scanln(&age)
// 		fmt.Printf("电话(%v):", phone)
// 		fmt.Scanln(&phone)
// 		fmt.Printf("邮箱(%v):", email)
// 		fmt.Scanln(&email)
// 		cs.customerService.List()[resetIndex].Reset(name, gender, age, phone, email)

// 	// }

// }

//刪除用戶
func (cv *customerView) delete() {
	fmt.Println("---刪除客戶---")
	fmt.Println("輸入ID(-1=退出)...")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return //放棄刪除
	}
	fmt.Println("確認是否刪除?(Y/N)")
	var choice string = ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		if cv.customerService.Delete(id) {
			fmt.Println("刪除完成!")
		} else {
			fmt.Println("刪除失敗,該ID不存在")
		}
	}

}

//顯示所有客戶信息
func (cv *customerView) list() {
	customers := cv.customerService.List()
	fmt.Println("---客戶列表---")
	fmt.Println("編號\t姓名\t性別\t年齡\t電話\t信箱")
	for i := range customers {
		fmt.Println(customers[i].GetInfo())

	}

	fmt.Println("---客戶列表末尾---")
}

func main() {
	//創建一個customerView的實例並運行顯示
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//對customerService字段初始化
	customerView.customerService = service.NewCustomerService()

	customerView.mainMenu()
}

controler

//完成對Customer的操作
type CustomerService struct {
	customers []model.Customer
	//聲明一個字段,表示當前切片有幾個客戶
	customerNum int //還能作為新客戶的id+1
}

//寫一個方法返回*CustomerService,就是Customer的切片
func NewCustomerService() *CustomerService {
	customerService := &CustomerService{}
	//初始化一個客戶
	customerService.customerNum = 1
	customer := model.NewCustomer(1, "摺紙", "女", 15, "110", "001@x.mail")
	customerService.customers = append(customerService.customers, customer)
	return customerService

}

//返回客戶切片
func (cs *CustomerService) List() []model.Customer {
	return cs.customers
}

//添加客戶到customer切片
func (cs *CustomerService) Add(customer model.Customer) bool {
	//添加一個分配ID的規則
	cs.customerNum++
	customer.Id = cs.customerNum
	cs.customers = append(cs.customers, customer)
	return true
}

//根據id尋找對應切片的下標
func (cs *CustomerService) FindById(id int) int {
	index := -1 //找不到則返回-1
	for i := 0; i < len(cs.customers); i++ {
		if cs.customers[i].Id == id {
			//找到了
			index = i
		}
	}
	return index
}

//刪除
func (cs *CustomerService) Delete(id int) bool {
	index := cs.FindById(id)
	if index == -1 {
		return false
	}
	//從切片中刪除一個元素
	cs.customers = append(cs.customers[:index], cs.customers[index+1:]...)
	return true
}

//修改

上次修改於 2021-08-01

此篇文章的評論功能已經停用。