go语言练习-简易学生管理系统
需求:
写一个简易的学生管理系统,能满足对学生的查询、增加、删除、修改操作
一、函数方式实现
1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "strconv" 8 "strings" 9 ) 10 11 // 存放所有student的映射,key为stu ID,value为每一个student对象 12 var stuList map[int]*students 13 14 // student结构体,也是每一个student对象 15 type students struct { 16 id int 17 name string 18 age int 19 } 20 21 // 构造函数,返回一个stu对象的指针 22 func NewStu(stuID int, stuName string, stuAge int) *students { 23 return &students{ 24 id: stuID, 25 name: stuName, 26 age: stuAge, 27 } 28 } 29 30 // show all students information 31 func ShowStuInfo() { 32 fmt.Println("-------------【查看所有学生信息】-------------") 33 for id, stuObj := range stuList { 34 fmt.Printf("【stuid】: %d 【stuName】: %s 【stuAge】:%d\n", id, stuObj.name, stuObj.age) 35 fmt.Printf("-----------------------------\n\n") 36 } 37 } 38 39 // add student to stuList 40 func AddStu() { 41 fmt.Println("--------------【添加学生】--------------") 42 fmt.Print("【Add Stu】student ID: ") 43 var stuID int 44 var stuName string 45 var stuAge int 46 reader := bufio.NewReader(os.Stdin) 47 strID, err := reader.ReadString('\n') 48 if err != nil { 49 fmt.Printf("read input student id err: %#v\n", err) 50 return 51 } 52 strID = strings.TrimSpace(strID) 53 stuID, err = strconv.Atoi(strID) 54 if err != nil { 55 fmt.Printf("str to int err: %#v\n", err) 56 return 57 } 58 _, ok := stuList[stuID] // 如果学生id存在则退出 59 if ok { 60 fmt.Println("stu already exits.") 61 return 62 } 63 fmt.Print("【Add Stu】student name: ") 64 fmt.Scanln(&stuName) 65 fmt.Print("【Add Stu】student age: ") 66 fmt.Scanln(&stuAge) 67 newStuObj := NewStu(stuID, stuName, stuAge) 68 stuList[stuID] = newStuObj 69 fmt.Printf("***add student %s success.***\n\n", stuName) 70 } 71 72 // edit student 73 func EditStu() { 74 fmt.Println("--------------【编辑学生】--------------") 75 var stuID int 76 var stuName string 77 var stuAge int 78 fmt.Print("【Edit Stu】student id: ") 79 fmt.Scanln(&stuID) 80 _, ok := stuList[stuID] 81 if !ok { 82 fmt.Printf("stu id: %d not exits.\n", stuID) 83 return 84 } 85 fmt.Print("【Edit Stu】new student name: ") 86 fmt.Scanln(&stuName) 87 fmt.Print("【Edit Stu】new student age: ") 88 fmt.Scanln(&stuAge) 89 stuList[stuID].name = stuName 90 stuList[stuID].age = stuAge 91 fmt.Printf("***edit student %s success.***\n\n", stuName) 92 } 93 94 // del student from stuList 95 func DelStu() { 96 fmt.Println("--------------【删除学生】--------------") 97 var stuID int 98 fmt.Print("【Del Stu】student id: ") 99 fmt.Scanln(&stuID) 100 _, ok := stuList[stuID] 101 if !ok { 102 fmt.Println("student not exits.") 103 return 104 } 105 stuName := stuList[stuID].name 106 delete(stuList, stuID) 107 fmt.Printf("***delete student %s success.***\n\n", stuName) 108 } 109 110 func main() { 111 info := `######## 112 Student Manager System: 113 1. show students info 114 2. add student 115 3. edit student 116 4. del student 117 5. exit 118 ######## 119 ` 120 stuList = make(map[int]*students, 100) // 初始化所有student的映射 121 122 for { 123 fmt.Println(info) 124 fmt.Print("please input your choice: ") 125 reader := bufio.NewReader(os.Stdin) // 在标准输入中获取用户输入 126 choice, err := reader.ReadString('\n') 127 if err != nil { 128 fmt.Printf("input err: %#v\n", err) 129 return 130 } 131 choice = strings.TrimSpace(choice) 132 switch choice { 133 case "1": 134 ShowStuInfo() 135 case "2": 136 AddStu() 137 case "3": 138 EditStu() 139 case "4": 140 DelStu() 141 case "5": 142 os.Exit(1) 143 default: 144 ShowStuInfo() 145 } 146 } 147 }
二、使用结构体的方式实现
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 ) 7 8 // 学生对象结构体 9 type Student struct { 10 id int 11 name string 12 age int 13 } 14 15 // 管理学生的结构体 16 type StuManager struct { 17 // 包含所有学生对象的映射 18 stuList map[int]*Student 19 } 20 21 var stuMgr *StuManager 22 23 // 构造函数 24 func (s *StuManager) NewStu(stuID int, stuName string, stuAge int) *Student { 25 return &Student{ 26 id: stuID, 27 name: stuName, 28 age: stuAge, 29 } 30 } 31 32 func (s *StuManager) ShowStuInfo() { 33 fmt.Println("-------------【查看所有学生信息】-------------") 34 for id, stu := range s.stuList { 35 fmt.Printf("【stuid】: %d 【stuName】: %s 【stuAge】:%d\n", id, stu.name, stu.age) 36 fmt.Printf("-----------------------------\n\n") 37 } 38 } 39 40 func (s *StuManager) AddStu() { 41 fmt.Println("--------------【添加学生】--------------") 42 var stuID int 43 var stuName string 44 var stuAge int 45 fmt.Print("【Add Stu】student ID: ") 46 fmt.Scanln(&stuID) 47 _, ok := s.stuList[stuID] 48 if ok { 49 fmt.Printf("student %s already exits.\n", s.stuList[stuID].name) 50 return 51 } 52 fmt.Print("【Add Stu】student name: ") 53 fmt.Scanln(&stuName) 54 fmt.Print("【Add Stu】student age: ") 55 fmt.Scanln(&stuAge) 56 newStuObj := s.NewStu(stuID, stuName, stuAge) 57 s.stuList[stuID] = newStuObj 58 fmt.Printf("***add student %s success.***\n\n", stuName) 59 } 60 61 func (s *StuManager) DelStu() { 62 fmt.Println("--------------【删除学生】--------------") 63 var stuID int 64 fmt.Print("【Del Stu】student id: ") 65 fmt.Scanln(&stuID) 66 _, ok := s.stuList[stuID] 67 if !ok { 68 fmt.Printf("student %d is not exits.\n", stuID) 69 return 70 } 71 stuName := s.stuList[stuID].name 72 delete(s.stuList, stuID) 73 fmt.Printf("***delete student %s success.***\n\n", stuName) 74 } 75 76 func (s *StuManager) EditStu() { 77 fmt.Println("--------------【编辑学生】--------------") 78 var stuID int 79 var stuName string 80 var stuAge int 81 fmt.Print("【Edit Stu】student id: ") 82 fmt.Scanln(&stuID) 83 _, ok := s.stuList[stuID] 84 if !ok { 85 fmt.Printf("student %d is not exits.\n", stuID) 86 return 87 } 88 fmt.Print("【Edit stu】new student name: ") 89 fmt.Scanln(&stuName) 90 fmt.Print("【Edit stu】new student age: ") 91 fmt.Scanln(&stuAge) 92 s.stuList[stuID].name = stuName 93 s.stuList[stuID].age = stuAge 94 fmt.Printf("***edit student %s success.***\n\n", stuName) 95 } 96 97 func main() { 98 info := `######## 99 Student Manager System: 100 1. show students info 101 2. add student 102 3. edit student 103 4. del student 104 5. exit 105 ######## 106 ` 107 stuMgr = &StuManager{ 108 stuList: make(map[int]*Student, 100), 109 } 110 111 for { 112 113 fmt.Println(info) 114 var choice int 115 fmt.Print("Please input your choice: ") 116 fmt.Scanln(&choice) 117 118 switch choice { 119 case 1: 120 stuMgr.ShowStuInfo() 121 case 2: 122 stuMgr.AddStu() 123 case 3: 124 stuMgr.EditStu() 125 case 4: 126 stuMgr.DelStu() 127 case 5: 128 os.Exit(1) 129 default: 130 stuMgr.ShowStuInfo() 131 } 132 } 133 134 }