Golang-demo

Introduction:

  Realize the addition, deletion, inspection and modification of customer management

model/customer.go

package model
import "fmt"
type Customer struct {
	Id int 
	Name string
	Gender string
	Age int
	Email string
	Phone string
}
func NewCustomer(id int,name string,gander string,age int, email string,phone string) *Customer {
	return &Customer{
		Id: id,
		Name: name,
		Gender: gander,
		Age: age,
		Email: email,
		Phone: phone,
	}
}
func NewCustomerNoId(name string,gander string,age int, email string,phone string) *Customer {
	return &Customer{
		Name: name,
		Gender: gander,
		Age: age,
		Email: email,
		Phone: phone,
	}
}
func (customer *Customer) GetInfo() string {
	info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v",customer.Id,customer.Name,customer.Gender,customer.Age,customer.Email,customer.Phone)
	return info
}

service/customerService.go

package service
import (
	_"fmt"
	"laurencecustomerManage/model"
)

type CustomerService struct {
	customerNum int
	customers []model.Customer
}
func NewCustomerService() *CustomerService {
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(customerService.customerNum,"lauren7ce","男",28,"[email protected]","13538239520")
	customerService.customers = append(customerService.customers,*customer)
	return customerService
}
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
}
func (cs *CustomerService) FindById(id int) int {
	index := -1
	for i, _ := range cs.customers {
		if cs.customers[i].Id == id {
			index = i
			break
		}
	}
	return index
}
func (cs *CustomerService) FindByIdModifile(id int) (key int, customer []model.Customer) {
	index := -1
	for i, _ := range cs.customers {
		if cs.customers[i].Id == id {
			index = i
			break
		}
	}
	if index != -1 {
		customer := cs.customers[index:index+1]
		return index, customer
	} else {
		return 0, nil
	}
}
func (this *CustomerService) Updat(index int,customer *model.Customer) bool {
	this.customers[index] = *customer
	return true
}
func (cs *CustomerService) List() []model.Customer {
	return cs.customers
}
func (cs *CustomerService) Add(customer *model.Customer) bool {
	cs.customerNum += 1 
	customer.Id = cs.customerNum
	cs.customers = append(cs.customers,*customer)
	return true
}

view/customerView.go

package main

import (
	"fmt"
	"laurencecustomerManage/service"
	"laurencecustomerManage/model"
)

type customerView struct {
	option string
	loop bool
	customerService *service.CustomerService
}
func (cv *customerView) list() {
	customer := cv.customerService.List()
	fmt.Println("--------------------------------------")
	fmt.Println("编号\t姓名\t性别\t年龄\t邮箱\t电话\n ")
	for i := 0; i < len(customer); i++ {
		fmt.Println(customer[i].GetInfo())
	}
	fmt.Println("--------------------------------------")
}
func(cv *customerView) add() {
	fmt.Println ("------------- Add customer -----------------") 
	name := ""
	fmt.Print ("Name:") 
	fmt. Scanln (& name) 
	for { 
		if name! = "" { 
			Break 
		} 
		fmt.Print ("Name:") 
		fmt.Scanln (& name) 
	} 
	
	gender: = "" 
	fmt.Print ("Gender:") 
	fmt.Scanln (& gender ) 
	for { 
		if gender! = "" { 
			break 
		} 
		fmt.Print ("gender:") 
		fmt.Scanln (& gender) 
	} 
	age: = 0 
	fmt.Print ("age:") 
	fmt.Scanln (& age) 
	for { 
		if age! = 0 { 
			break 
		} 
		fmt.Print ("Age:")
		fmt.Scanln(&age)
	}
	email := ""
	fmt.Print ("Mailbox:") 
	fmt.Scanln (& email) 
	for { 
		if email != "" {
			break
		} 
		fmt.Print ("Mailbox:") 
		fmt.Scanln (& email) 
	} 
	phone: = "" 
	fmt.Print ("Phone:") 
	fmt. Scanln (& phone) 
	for { 
		if phone! = "" { 
			Break 
		} 
		fmt.Print ("Phone:") 
		fmt.Scanln (& phone) 
	} 
	// Build Customer 
	customer: = model.NewCustomerNoId (name, gender, age, email, phone) 
	cv.customerService.Add (customer) 
	fmt.Println ("----------------------------------- --- ") 
} 
func (cv * customerView) delete () { 
	fmt.Print (" Please enter the ID to delete: ") 
	id:= -1 
	fmt.Scanln (& id) 
	fmt.Println ("Confirm deletion? Y / n:")
	choice: = ""
func (cv *customerView) modifile() {
	for {
		fmt.Scanln(&choice)
		if choice == "y" || choice == "n" { 
			break 
		} 
		fmt.Print ("The input is incorrect, please re-enter:") 
	} 
	if choice == "y" && cv.customerService.Delete (id) { 
		fmt.Println ("Delete successfully") 
	} else { 
		fmt.Println ("Delete failed") 
	} 
} 
func (cv * customerView) exit () { 
	fmt.Print ("Are you sure you want to exit? y / n:" ) 
	choice: = "" 
	for { 
		fmt.Scanln (& choice) 
		if choice == "y" || choice == "n" { 
			break 
		} 
		fmt.Print ("The input is incorrect, please re-enter:") 
	} 
	if choice == "y" { 
		cv.loop = false 
	} 
} 
	fmt.Print ("Please enter the user ID to edit:") 
	id: = -1 
	fmt.Scanln (& id) 
	indx, customer: = cv.customerService.FindByIdModifile (id) 
	fmt.Println (customer [0]) 
	if customer! = nil { 
		fmt.Println ("- ------ Edit and update customer information --------- ") 
		fmt.Printf (" Name (% v): ", customer [0] .Name) 
		name: =" " 
		fmt.Scanln ( & name) 
		fmt.Printf ("Gender (% v):", customer [0] .Gender) 
		gender: = "" 
		fmt.Scanln (& gender) 
		fmt.Printf ("Age (% v):", customer [0] .Age) 
		age: = 0 
		fmt.Scanln (& age) 
		fmt.Printf ("Phone (% v):", customer [0] .Phone) 
		phone: = "" 
		fmt.Scanln (& phone) 
		fmt.Printf ("Mailbox % v): ", customer [0] .Email) 
		email:= ""
		fmt.Scanln(&email)
		upCustomer: = model.NewCustomer (id, name, gender, age, phone, email) 
		if cv.customerService.Updat (indx, upCustomer) { 
			fmt.Println ("Update Successful") 
		} else { 
			fmt.Println ("Update Successful ") 
		} 
	} 
} 
func (cv * customerView) mainMenu () { 
	for { 
		fmt.Println (" ---------- Family revenue and expenditure accounting details ------------ ") 
		fmt.Println (" 1 add customer ") 
		fmt.Println (" 2 modify customer ") 
		fmt.Println (" 3 delete customer ") 
		fmt.Println (" 4 customer list ") 
		fmt.Println (" 5 exit " ) 
		fmt.Println ("Please select (1-5)") 
		fmt.Println ("------------------------------ -------- ") 
		fmt.Print ("Please select the option you want to operate:")
		fmt.Scanln (& cv.option) 
		switch cv.option { 
		case "1": 
			cv.add()
		case "2":
			cv.modifile () 
		case "3": 
			cv.delete () 
		case "4": 
			cv.list () 
		case "5": 
			cv. exit () 
		default: 
			fmt.Print ("The input is incorrect, please re-enter:") 
		} 
		if! cv.loop { 
			fmt.Println ("Welcome to visit next time, Goodbay !!!!!!!!!!!! ") 
			break 
		} 
	} 
} 
func main () { 
	var cv customerView = customerView { 
		option:" ", 
		loop: true, 
	} 
	cv.customerService = service.NewCustomerService () 
	cv.mainMenu () 
}

 

EVERYTHING

1. Account password login verification

2. Account relationship

 

Guess you like

Origin www.cnblogs.com/Essaycode/p/12677754.html