golang-学生管理系统

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("输入错误,请重新输入")
		}
	}

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值