golang-学生管理系统
思路:
1.需要一个变量储存所有学生信息-map类型
2.具有唯一的ID-[key]
3.需要获取当中每一个学生的信息-map[key]student
4.学生信息繁多,使用struct
package main
import (
"fmt"
"os"
)
//Mgr 储存学生数据
var Mgr studentMgr
//定义个储存学生信息的结构体
type studentMgr struct {
stu map[uint]*student
}
//定义一个学生信息的结构体
type student struct {
id uint
name string
age uint
score int
}
//查看所有学生信息的功能
func (s studentMgr) showAllStudent() {
fmt.Printf("%s\t%s\t%s\t%s\n", "ID", "Name", "Age", "Score")
for i, v := range s.stu {
fmt.Printf("%d\t%s\t%d\t%d\n", i, v.name, v.age, v.score)
}
fmt.Println("----This is all students----")
}
//查看增加学生信息的功能
func (s studentMgr) addStudent() {
fmt.Printf("Please imput add student ID:")
var (
stuID uint
stuName string
stuAge uint
stuScore int
)
fmt.Scanln(&stuID)
_, ok := s.stu[stuID]
if ok {
fmt.Println("此学号存在")
return
}
fmt.Printf("Please imput add student name:")
fmt.Scanln(&stuName)
fmt.Printf("Please imput add student age:")
fmt.Scanln(&stuAge)
fmt.Printf("Please imput add student score:")
fmt.Scanln(&stuScore)
s.stu[stuID] = &student{
id: stuID,
name: stuName,
age: stuAge,
score: stuScore,
}
}
//查看修改学生信息的功能
func (s studentMgr) alterStudent() {
fmt.Printf("Please imput alter student ID:")
var (
stuID uint
stuName string
stuAge uint
stuScore int
num int
)
fmt.Scanln(&stuID)
_, ok := s.stu[stuID]
if !ok {
fmt.Println("没有此人")
return
}
fmt.Printf("Please input options to modify:\n1.name;2.age,3.score,4.all\nPlease input num:")
fmt.Scanln(&num)
switch num {
case 1:
fmt.Printf("Please imput alter student name:")
fmt.Scanln(&stuName)
s.stu[stuID].name = stuName
case 2:
fmt.Printf("Please imput alter student age:")
fmt.Scanln(&stuAge)
s.stu[stuID].age = stuAge
case 3:
fmt.Printf("Please imput alter student score:")
fmt.Scanln(&stuScore)
s.stu[stuID].score = stuScore
case 4:
fmt.Printf("Please imput alter student name:")
fmt.Scanln(&stuName)
fmt.Printf("Please imput alter student age:")
fmt.Scanln(&stuAge)
fmt.Printf("Please imput alter student score:")
fmt.Scanln(&stuScore)
s.stu[stuID] = &student{
name: stuName,
age: stuAge,
score: stuScore,
}
}
fmt.Println("----修改成功----")
}
//查看删除学生信息的功能
func (s studentMgr) delStudent() {
fmt.Printf("Please imput delete student ID:")
var stuID uint
fmt.Scanln(&stuID)
delete(s.stu, stuID)
fmt.Println("----删除成功----")
}
func main() {
Mgr = studentMgr{
stu: make(map[uint]*student, 100),
}
fmt.Println("----Welcome!----")
for {
fmt.Println(`
1.show
2.add
3.alter
4.delete
5.exit
`)
fmt.Printf("Please chose num:")
var stuID uint
fmt.Scanln(&stuID)
switch stuID {
case 1:
Mgr.showAllStudent()
case 2:
Mgr.addStudent()
case 3:
Mgr.alterStudent()
case 4:
Mgr.delStudent()
case 5:
os.Exit(1)
default:
fmt.Println("输入错误,请重新输入")
}
}
}