A simple version of the student management system based on "structure" (Golang)


First of all, before we do the project, we need to analyze the project. Remember not to be greedy for work, which will lead to useless work

analysis

  1. Student class
  2. Manager class
  3. Menu Bar
  4. Implement functions based on management classes
    1. View
    2. Add to
    3. modify
    4. delete
    5. drop out

The structure diagram is as follows:

自顶向下The programming thinking adopted repeats the above analysis, first overall, then details (first overall, then detailed) to achieve the above structure

Define the global student class

Before defining the student class, we need to understand the fields contained in the student class. Here I only defined Id and name. The id is unique and the name can be repeated. The code is as follows

type student struct {
    
    
	id   int64
	name string
}

Define global management (i.e. operation class)

Because the student ID is unique and its comparison relationship, it is very suitable to use the Map Key-valueformat. Here is only the definition and declaration without initialization, the code is as follows

type studentMgr struct {
    
    
	allStudent map[int64]student
}

Borrowing a good data structure will make your programming efficiency and coding thinking more effective

Define the menu bar

Define the menu bar for the user to select the corresponding function. Show according to its function. And according to the relative function, define the response function. as follows

func showMenu() {
    
    
	fmt.Printf("Welcome student Manage System, TimeNow:%v", time.Now())
	fmt.Println(
		` 
			1: 查看学生
			2: 增加学生
			3: 修改学生
			4: 删除学生
			5: 退出~
				`)
	fmt.Print("What are you want do? Please input Serial number:")

}

From the above knowledge, we have five Xu Dingyi function, they are 查看学生, 增加学生, 修改学生, 删除学生, 退出, as follows:

func main() {
    
    
	smr = studentMgr{
    
    
		allStudent: make(map[int64]student),
	}
	for {
    
    
		showMenu()
		var choice int64
		fmt.Scan(&choice)
		fmt.Printf("You select %d\n", choice)
		switch choice {
    
    
		case 1:
			smr.showStudent()
		case 2:
			smr.addStudent()
		case 3:
			smr.editStudent()
		case 4:
			smr.delStudent()
		case 5:
			os.Exit(1)
		default:
			fmt.Println("Invalid input, please select again:")
		}
	}
}

A global management class is instantiated here, and all operations pass through it.

Use the switch statement to carry out multiple conditional branches, which is more conducive for us to write more concise code

Define function function

查看学生,增加学生,修改学生,删除学生

// 查看学生函数
func (s studentMgr) showStudent() {
    
    }
// 增加学生函数
func (s studentMgr) addStudent()	{
    
    }
// 修改学生函数
func (s studentMgr) editStudent()	{
    
    }
// 删除学生函数
func (s studentMgr) delStudent()	{
    
    }

The basics have been done, the basic structure has been completed, applause~

So next, we only need to implement the corresponding functions one by one. Can realize the function.

The first thing we implemented is to view the student function.

Realize the function of viewing students

func (s studentMgr) showStudent() {
    
    
	for _, stu := range s.allStudent {
    
    
		fmt.Printf("ID:%d, Name:%s\n", stu.id, stu.name)
	}
}

We only need to traverse all the keys and values ​​in the Map to get all the students. Nothing to say here

Realize the function of increasing students

func (s studentMgr) addStudent() {
    
    
	var (
		stuId   int64
		stuName string
	)
	// 1. 根据输入内容创建学生
	fmt.Print("Please input you need ID:")
	fmt.Scanln(&stuId)
	fmt.Print("Please input you need name:")
	fmt.Scanln(&stuName)
	newStu := student{
    
    
		id:   stuId,
		name: stuName,
	}
	// 2. 将创建的学生加入stu中
	s.allStudent[newStu.id] = newStu
	fmt.Println("Added successfully")
}

Here, we need to take two steps

  1. Get user keyboard input

    var (
    		stuId   int64
    		stuName string
    	)
    	// 1. 根据输入内容创建学生
    	fmt.Print("Please input you need ID:")
    	fmt.Scanln(&stuId)
    	fmt.Print("Please input you need name:")
    	fmt.Scanln(&stuName)
    
  2. Add the entered student information to the management function

    newStu := student{
          
          
    		id:   stuId,
    		name: stuName,
    	}
    	// 2. 将创建的学生加入stu中
    	s.allStudent[newStu.id] = newStu
    	fmt.Println("Added successfully")
    }
    

    Prompt success if added successfully

By doing this, we can carry out a small test. You Mu is a little excited and a little square. Anyway, I have, the example is as follows

Leave a small bug and wait for you to solve it. It prompts, if the key already exists, then the addition operation will be performed? If you have no idea for a while, you can continue to look down. Although I did not tell you directly here, but the corresponding solution is carried out below

Realize the function of modifying students

func (s studentMgr) editStudent() {
    
    
	// 获取用户输入
	var StuId int64
	fmt.Print("Please input want change student IdCode:")
	fmt.Scanln(&StuId)
  // 检查该学号学生是否存在,没有则提示不存在
	value, ok := s.allStudent[StuId]
	if !ok {
    
    
		fmt.Println("Not found")
		return
	}
	fmt.Printf("You want change student message:"+
		" Id: %d, Name:%s\n", value.id, value.name)
	// 获取修改
	var newName string
	fmt.Print("Please change to new message:")
	fmt.Scanln(&newName)
	value.name = newName
	// 更新学生的姓名
	s.allStudent[StuId] = value
}
  1. First we get user input

    // 获取用户输入
    	var StuId int64
    	fmt.Print("Please input want change student IdCode:")
    	fmt.Scanln(&StuId)
    
  2. Take the student ID entered by the user and go to the Map to find the corresponding student ID

    value, ok := s.allStudent[StuId]
    	if !ok {
          
          
    		fmt.Println("Not found")
    		return
    	}
    	fmt.Printf("You want change student message:"+
    		" Id: %d, Name:%s\n", value.id, value.name)
    

    If you want to make changes, you need to exist. If the student does not exist, it will prompt that the student is not found, and return directly. The proof cannot be modified. If it exists, it must be a unique Id, because we are using Map format, and the key is unique.

  3. If it exists, we need to get the value modified by the user and overwrite the original Name. Can be modified

    var newName string
    	fmt.Print("Please change to new message:")
    	fmt.Scanln(&newName)
    	value.name = newName
    	// 更新学生的姓名
    	s.allStudent[StuId] = value
    

Test time, examples are as follows:

First, I add a student inside, Id:1,Name:Payne.

Realize the function of deleting students

func (s studentMgr) delStudent() {
    
    
	var studentID int64
	// 获取用户需删除的id
	fmt.Print("Please input want delete studentId:")
	fmt.Scanln(&studentID)
	// 去map里面查找,若有则删除。没有则退出重新选择
	value, ok := s.allStudent[studentID]
	if !ok {
    
    
		fmt.Println("Not found")
		return
	}
	fmt.Printf("You want delete student message:"+
		" Id: %d, Name:%s\n", value.id, value.name)
	delete(s.allStudent, studentID)
	fmt.Print("Deleted Successfully\n")
}
  1. First, we need to delete, then it must exist before we can delete it. Is this all right? I think it's ok.

    	var studentID int64
    	// 获取用户需删除的id
    	fmt.Print("Please input want delete studentId:")
    	fmt.Scanln(&studentID)
    
  2. Not found prompt not found

    value, ok := s.allStudent[studentID]
    	if !ok {
          
          
    		fmt.Println("Not found")
    		return
    	}
    	fmt.Printf("You want delete student message:"+
    		" Id: %d, Name:%s\n", value.id, value.name)
    
  3. Exist to modify

    	fmt.Printf("You want delete student message:"+
    		" Id: %d, Name:%s\n", value.id, value.name)
    	delete(s.allStudent, studentID)
    	fmt.Print("Deleted Successfully\n")
    

Guess you like

Origin blog.csdn.net/wzp7081/article/details/110012108