Student information management system demo

main.go:

package main 

import ( 
	"fmt" 
	"os" 
) 

// Student information management system 
// 
	1.Add 
student information 
// 2.Edit student information // 3.Show all student information 

func showMenu () { fmt.Println ("Welcome Come to the student information management system ") 
	fmt.Println (" 1. Add student ") 
	fmt.Println (" 2. Edit student information ") 
	fmt.Println (" 3. Display all student information ") 
	fmt.Println (" 4 . Exit the system ") 
} 

// Get the student information entered by the user 
func getInput () * student { 
	var ( 
		id int 
		name string 
		class string 
	) 
	fmt.Println (" Please enter the student information as required ") 
	fmt.Print (" Please enter Student ID: ") 
	fmt.Scanf ("% d \ n ", & id) 
	fmt.Print (" Please enter the student's name: ") 
	fmt.Scanf ("% s \ n", & name) 
	fmt.Print ("Please enter the class of the student:")Scanf("%s\n",&name)
	fmt.Scanf("%s\n",&class)
	stu: = newStudent (id, name, class) 
	return stu 
} 
func main () { 
	sm: = newStudentMgr () 
	for { 
		// 1. print system menu 
		showMenu () 
		// 2. wait for the user to select the option to be executed 
		var input int 
		fmt.Scanf ("% d \ n", & input) // If you want to change the value of the input variable, you must pass a pointer in. 
		fmt.Println ("User input is:", input) 
		// 3. Execute the user selection Action 
		switch input { 
		case 1: 
			// Add student 
			stu: = getInput () 
			sm.addStudent (stu) 
		case 2: 
		// Edit student 
			stu: = getInput () 
			sm.modifyStudent (stu) 
		case 3: 
		// Show all students 

		sm.showStudent () 
		case 4: 
			// Exit
			os.Exit (0) 

		} 
	} 

}

  

student.go:

package main 

import "fmt" 

type student struct { 
	id int 
	name string 
	class string 
} 

func newStudent (id int, name string, class string) * student { 
	return & student { 
		id: id, 
		name: name, 
		class: class, 
	} 
} 

/ / Type of student management 
type studentMgr struct { 
	allStudents [] * student 
} 

func newStudentMgr () * studentMgr { 
	return & studentMgr {allStudents: make ([] * student, 0,100)} 
} 

// 1. Add the recipient of the student use value type s studentMgr, add student failed, need to modify the value in the receiver remember to use the pointer receiver 
func (s * studentMgr) addStudent (newStu * student) { 
	s.allStudents = append (s.allStudents, newStu) 
}
// 2. Edit student 
func (s * studentMgr) modifyStudent (newStu * student) { 
	for i, v: = range s.allStudents { 
		if v.id == newStu.id {// student to be modified 
			s.allStudents [ i] = newStu 
			return 
		} 
	} 
	fmt.Printf ("The entered student information is wrong, there is no student number in the system:% d student \ n", newStu.id) 
} 

// 3. Show student 
func (s * studentMgr ) showStudent () { 
	for _, v: = range s.allStudents { 
		fmt.Printf ("Student ID:% d Name:% s Class:% s \ n", v.id, v.name, v.class) 
	} 
}

  

Welcome to the student information management system
 1. Add student
 2. Edit student information
 3. Display all student information
 4. Exit the system
 1 The 
user enters: 1 
Please enter the student information as required 
Please enter the student's student number: 001 
Please enter the student Name: han 
, please enter the class of the student: 01 
Welcome to the student information management system 
1. Add student
 2. Edit student information
 3. Display all student information
 4. Exit the system
 3 The 
user entered is: 3 
Student ID: 1 Name: han class: 01 
Welcome to the student information management system 
1. Add students
 2. Edit student information
 3. Display all student information
 4. Exit the system

 

Guess you like

Origin www.cnblogs.com/twoheads/p/12693541.html
Recommended