package main
import (
"fmt"
"os"
)
type Student struct{
id int
name string
age int
classroom int
dorm int
}
var students []*Student
type ManageStudent struct{}
func (m *ManageStudent) AddStudent(s *Student){
students = append(students, s)
fmt.Println("add success")
}
func (m *ManageStudent) DelStudent(id int){
var is_find = false
for index, student := range students{
if student.id == id{
is_find = true
new_students := append(students[:index], students[index+1:]...)
students = new_students
fmt.Println("delete success")
}
}
if !is_find{
fmt.Println("no found")
}
}
func (m *ManageStudent) showMenu(){
fmt.Println("---------------------------------")
fmt.Println("1.add student")
fmt.Println("2.delete student")
fmt.Println("3.modify student")
fmt.Println("4.query student")
fmt.Println("5.show all student")
fmt.Println("6.exit")
fmt.Println("---------------------------------")
var option string
fmt.Printf("input option:")
fmt.Scanf("%s\n", &option)
switch option{
case "1":
fmt.Println("add student")
fmt.Printf("input student id:")
var id int
fmt.Scanf("%d\n", &id)
fmt.Printf("input student name:")
var name string
fmt.Scanf("%s\n", &name)
fmt.Printf("input student age:")
var age int
fmt.Scanf("%d\n", &age)
fmt.Printf("input classroom:")
var classroom int
fmt.Scanf("%d\n", &classroom)
fmt.Printf("input dorm:")
var dorm int
fmt.Scanf("%d\n", &dorm)
var student = new(Student)
student.id = id
student.name = name
student.age = age
student.classroom = classroom
student.dorm = dorm
m.AddStudent(student)
case "2":
fmt.Printf("input student id:")
var id int
fmt.Scanf("%d\n", &id)
m.DelStudent(id)
case "3":
fmt.Printf("input student id:")
var id int
fmt.Scanf("%d\n", &id)
m.modifyStudent(id)
case "4":
fmt.Printf("input student id:")
var id int
fmt.Scanf("%d\n", &id)
m.queryStudent(id)
case "5":
m.showAllStudent()
case "6":
fmt.Println("bye bye...")
os.Exit(0)
}
}
func (m *ManageStudent) showAllStudent(){
for _, student := range students{
fmt.Printf("id=%d \t name=%s \t age=%d \t classroom=%d \t dorm=%d\n",
student.id, student.name, student.age, student.classroom, student.dorm)
}
if len(students) == 0{
fmt.Println("Data is empty")
}
}
func (m *ManageStudent) modifyStudent(id int){
var is_find = false
for _, student := range students{
if student.id == id{
fmt.Printf("input student name:")
var name string
fmt.Scanf("%s\n", &name)
fmt.Printf("input student age:")
var age int
fmt.Scanf("%d\n", &age)
fmt.Printf("input classroom:")
var classroom int
fmt.Scanf("%d\n", &classroom)
fmt.Printf("input dorm:")
var dorm int
fmt.Scanf("%d\n", &dorm)
student.name = name
student.age = age
student.classroom = classroom
student.dorm = dorm
is_find = true
fmt.Println("modify success")
}
}
if !is_find{
fmt.Println("no found")
}
}
func (m *ManageStudent) queryStudent(id int){
var is_find = false
for _, student := range students{
if student.id == id{
fmt.Printf("id=%d \t name=%s \t age=%d \t classroom=%d \t dorm=%d\n",
student.id, student.name, student.age, student.classroom, student.dorm)
is_find = true
}
}
if !is_find{
fmt.Println("no found")
}
}
func main() {
var manage = new(ManageStudent)
for{
manage.showMenu()
}
}
有疑问加站长微信联系(非本文作者)