golang 学生管理

就超棒 · · 670 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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()
    }
}


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:就超棒

查看原文:golang 学生管理

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

670 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传