go language project customer information relationship system

Keywords: Go

The best way to learn a language is through a practical example. Through this example, not only can we further consolidate the basic skills of golang, but also can let us strengthen our logic ability, call functions step by step, and master the skills of parameter passing and receiving.

Item 2 - customer information relationship system

This project shows the implementation of the object-oriented function of adding, deleting, modifying and querying.

Any project needs to do a good job of requirement analysis before knocking on the code

Project demand analysis

  1. Simulate the realization of customer information management software based on text interface

  2. The software can insert, modify and delete customer objects (realized by slicing), and print customer list

Overall structure of the project

Code area


customer.go

package model

import "fmt"

//Declare a Customer structure to represent the necessary information of a Customer
type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

//Using factory mode, return an instance of 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,
	}
}

//The second method of creating Customer instance, without id
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,
	}
}

//Return user information, formatted string
func (cm Customer) GetInfo() string  {
	info := fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t", cm.Id, cm.Name, cm.Gender, cm.Age, cm.Phone, cm.Email)
	return info
}

customerService.go

package service

import "2020-04-08/customerManage/model"
//The CustomerService completes the operation on the Customer, including adding, deleting, modifying and querying
type CustomerService struct {
	customers []model.Customer
	//Declare a field to indicate how many customers the current slice contains
	//After this field, it can also be used as the id + 1 of the new customer
	customerId int
}

//Write a method that returns * CustomerService
func NewCustomerService() *CustomerService  {
	//In order to see that there are customers in the slice, initialize a customer
	customerService := &CustomerService{}
	customerService.customerId = 1
	customer := model.NewCustomer(1, "Purple flying pig","male",22,"00000000000","zisefeizhu@qq.com")
	customerService.customers = append(customerService.customers, customer)
	return customerService
}

//Return to customer list slice
func (cs *CustomerService) List()  []model.Customer {
	return cs.customers
}

//Add customer to customers slice
func (cs *CustomerService) Add(customer model.Customer) bool {
	//Determine a rule for assigning id, that is, the order of adding
	cs.customerId++
	customer.Id = cs.customerId
	cs.customers = append(cs.customers, customer)
	return true
}

//Find the subscript corresponding to the customer in the slice according to the id. if there is no such customer, return - 1
func (cs *CustomerService) FindById(id int) int {
	index := 1
	//Traverse cs.customers slice
	for i := 0; i < len(cs.customers); i++ {
		if cs.customers[i].Id == id {
			//find
			index = i
		}
	}
	return index
}

//delete user
func (cs *CustomerService) Delete(id int) bool {
	index := cs.FindById(id)
	//If index == -1, there is no such customer
	if index == -1 {
		return false
	}
	//How to delete an element from a slice
	cs.customers = append(cs.customers[:index], cs.customers[index+1:]...)
	return true
}

func (cs *CustomerService) GetInfoById(id int) model.Customer  {
	i := id - 1
	return cs.customers[i]
}

//Modify customer information according to id
func (cs *CustomerService) Update(id int, customer model.Customer) bool {
	for i := 0; i < len(cs.customers); i++ {
		if cs.customers[i].Id == id {
			cs.customers[i].Name = customer.Name
			cs.customers[i].Gender = customer.Gender
			cs.customers[i].Age = customer.Age
			cs.customers[i].Phone = customer.Phone
			cs.customers[i].Email = customer.Email
		}
	}
	return true
}

customerView.go

package main

import (
	"2020-04-08/customerManage/model"
	"2020-04-08/customerManage/service"
	"fmt"
)

type customerView struct {
	//Define required fields
	key string //Accept user input
	loop bool  //Display main menu indicating whether to cycle
	customerService *service.CustomerService
}

//Show all customer information
func (cv *customerView) list()  {
	//First, get the current customer information (in the slice)
	customers := cv.customerService.List()
	//display
	fmt.Println("------------------Customer list------------------")
	fmt.Println("number\t Full name\t Gender\t Age\t Telephone\t mailbox")
	for i := 0; i < len(customers); i++ {
		fmt.Println(customers[i].GetInfo())
	}
	fmt.Println("------------------Customer list------------------")
}

//Get the user's input information to form a new customer, and complete the addition
func (cv *customerView) add() {
	fmt.Println("------------------Add customer------------------")
	fmt.Print("Full name:")
	name := ""
	fmt.Scanln(&name)
	fmt.Print("Gender:")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Print("Age:")
	age := 0
	fmt.Scanln(&age)
	fmt.Print("Telephone:")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Print("E-mail:")
	email := ""
	fmt.Scanln(&email)
	//Build a new Customer instance
	//Note that the id number is not entered by the user. The id is unique and needs to be allocated by the system
	customer := model.NewCustomer2(name, gender, age, phone, email)
	//call
	if cv.customerService.Add(customer) {
		fmt.Println("------------------Add finish------------------")
	} else {
		fmt.Println("------------------Add failure------------------")
	}
}

//Get the user's input id and delete the customer corresponding to the id
func (cv *customerView) delete()  {
	fmt.Println("------------------Delete customer------------------")
	fmt.Print("Please select waiting to delete customer number(-1 Sign out): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return   //Discard delete operation
	}
	fmt.Print("Confirm whether to delete(Y/N): ")
	//Here you can add a loop judgment, until the user input y or n, then exit
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		//Call the Delete method of customerService
		if cv.customerService.Delete(id) {
			fmt.Println("------------------Delete completed------------------")
		} else {
			fmt.Println("------------------Delete failed, entered id The number does not exist.")
		}
	}
}

//Get the user's input id and modify the corresponding customer of the id
func (cv *customerView) update() {
	cv.list()
	fmt.Println()
	fmt.Println("------------------Modify customer------------------")
	fmt.Print("Please select customer number to be modified(-1 Sign out): ")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return   //Discard delete operation
	}
	fmt.Print("Confirm whether to modify(Y/N): ")
	//Here you can add a loop judgment, until the user input y or n, then exit
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		//Call the Delete method of customerService
		if cv.customerService.FindById(id) != -1 {
			customer := cv.customerService.GetInfoById(id)
			fmt.Printf("Name (%v: )", customer.Name)
			name := ""
			fmt.Scanln(&name)
			fmt.Printf("Gender (%v): ", customer.Gender)
			gender := ""
			fmt.Scanln(&gender)
			fmt.Printf("Age (%v): ", customer.Age)
			age := 0
			fmt.Scanln(&age)
			fmt.Printf("Telephone (%v): ", customer.Phone)
			phone := ""
			fmt.Scanln(&phone)
			fmt.Printf("Mailbox (%v): ", customer.Email)
			email := ""
			fmt.Scanln(&email)
			customer2 := model.NewCustomer2(name, gender, age, phone, email)
			cv.customerService.Update(id, customer2)
			fmt.Println("------------------Modification completed------------------")
		} else {
			fmt.Println("------------------Modification failed, entered id The number does not exist.")
		}
	}
}

//Sign out
func (cv *customerView) logout()  {
	fmt.Print("Confirm whether to exit(Y/N): ")
	for {
		fmt.Scanln(&cv.key)
		if cv.key == "Y" || cv.key =="y" || cv.key =="N" || cv.key =="n" {
			break
		}
		fmt.Print("Your input is wrong. Confirm whether to exit(Y/N): ")
	}
	if cv.key == "Y" || cv.key == "y" {
		cv.loop = false
	}
}


//Show main menu
func (cv *customerView) mainMenu()  {
	for {
		fmt.Println("------------------Customer information management software------------------")
		fmt.Println("                  1 Add customer")
		fmt.Println("                  2 Modify customer")
		fmt.Println("                  3 Delete customer")
		fmt.Println("                  4 Customer list")
		fmt.Println("                  5 retreat    Out")
		//fmt.Println("please select (1-5):") line feed
		fmt.Print("Please choose(1 - 5): ")
		fmt.Scanln(&cv.key)
		switch cv.key {
		case "1":
			//fmt.Println("add customer")
			cv.add()
		case "2":
			//fmt.Println("modify customer")
			cv.update()
		case "3":
			cv.delete()
		case "4":
			//fmt.Println("customer list")
			cv.list()
		case "5":
			//cv.loop = false
			cv.logout()
		default:
			fmt.Print("Your input is wrong, please input again...")
		}
		if !cv.loop {
			break
		}
	}
	fmt.Println("You have exited the customer relationship system...")
}

func main()  {
	//In the main function, create a customerView and run the display main menu
	customerView := customerView{
		key:  "",
		loop: true,
	}
	//Finish initializing the customerService field of the customerView structure
	customerView.customerService = service.NewCustomerService()
	//Show main menu
	customerView.mainMenu()
}

Verification

------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 4
 ------------------Customer list------------------
No. Name gender age phone email
 1. Purple flying pig, male 22 billion, zisefeizhu@qq.com	
------------------Customer list------------------
------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 1
 ------------------Add customer------------------
Name: jingxing
 Gender: Female
 Age: 21
 Tel: 00000000000
 Email: jingxing@qq.com
 ------------------Add complete------------------
------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 2
 ------------------Customer list------------------
No. Name gender age phone email
 1. Purple flying pig, male 22 billion, zisefeizhu@qq.com	
2 Jingxing female 21 00000000000 jingxing@qq.com	
------------------Customer list------------------

------------------Modify customer------------------
Please select to wait for modification of customer number (- 1 exit): 2
 Confirm to modify (Y/N): n
 ------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 2
 ------------------Customer list------------------
No. Name gender age phone email
 1. Purple flying pig, male 22 billion, zisefeizhu@qq.com	
2 Jingxing female 21 00000000000 jingxing@qq.com	
------------------Customer list------------------

------------------Modify customer------------------
Please select to wait for modification of customer number (- 1 exit): 2
 Confirm to modify (Y/N): y
 Name (jingxing:) jingxing
 Gender (female): Female
 Age (21): 22
 Telephone (00000000000): 00000000000
 Email (jingxing@qq.com): jingxing@qq.com
 ------------------Modification complete------------------
------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 4
 ------------------Customer list------------------
No. Name gender age phone email
 1. Purple flying pig, male 22 billion, zisefeizhu@qq.com	
2 Jingxing female 22 00000000000 jingxing@qq.com	
------------------Customer list------------------
------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 3
 ------------------Delete customer------------------
Please select to wait for deletion of customer number (- 1 exit): - 1
 ------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 3
 ------------------Delete customer------------------
Please select to wait for deletion of customer number (- 1 exit): 2
 Confirm delete (Y/N): n
 ------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 3
 ------------------Delete customer------------------
Please select to wait for deletion of customer number (- 1 exit): 2
 Confirm delete (Y/N): y
 ------------------Delete complete------------------
------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 4
 ------------------Customer list------------------
No. Name gender age phone email
 1. Purple flying pig, male 22 billion, zisefeizhu@qq.com	
------------------Customer list------------------
------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 5
 Confirm exit (Y/N): n
 ------------------Customer information management software------------------
                  1 add customer
                  2 modify customer
                  3 delete customer
                  4 customer list
                  5 exit
 Please select (1 - 5): 5
 Confirm exit (Y/N): y
 You have exited the customer relationship system

summary

The most important significance of this project is: when I need it * when I don't need it*

When you need to modify the variable content of the structure, the structure variable parameter passed in by the method needs to use a pointer, that is, the address of the structure. When you need to modify the variables of the schema in the map, you also need to use the address of the schema as the value of the map. If you just read the structure variable, you can pass the reference directly without using the pointer. *The type variable here stores the address, which needs to be clear. You need to use & type to get the address.

Posted by JamesU2002 on Thu, 09 Apr 2020 05:48:36 -0700