简单学生信息管理系统 Golang

utils包

package utils

import (
	"fmt"
	"os"
	"sort"
)

//StuSystem 管理系统类
type StuSystem struct {
	stus []Stu
	//option 确定排序方式的选择
	option int
}

func (stuSystem StuSystem) Len() int {
	return len(stuSystem.stus)
}

func (stuSystem StuSystem) Less(i, j int) bool {
	switch stuSystem.option {
	case 1:
		//按学号由小到大排序
		return stuSystem.stus[i].StuID < stuSystem.stus[j].StuID
	case 2:
		//按姓名由小到大排序
		return stuSystem.stus[i].Name < stuSystem.stus[j].Name
	case 3:
		//按分数由大到小排序
		return stuSystem.stus[i].Score > stuSystem.stus[j].Score
	default:
		fmt.Println("输入有误,排序失败")
		return false
	}
}

func (stuSystem StuSystem) Swap(i, j int) {
	stuSystem.stus[i], stuSystem.stus[j] = stuSystem.stus[j], stuSystem.stus[i]
}

//Stu 学生类
type Stu struct {
	StuID int
	Name  string
	Score float32
	Grade string
	Sex   string
}

//NewStuSystem  自定义构造函数
func NewStuSystem() *StuSystem {
	stuSystem := StuSystem{
		stus: make([]Stu, 0),
	}
	return &stuSystem
}

//PrintStu 打印学生信息
func (stu Stu) PrintStu() {
	fmt.Printf("stuID:%d, name:%s, score:%0.2f, grade:%s, sex:%s\n", stu.StuID, stu.Name, stu.Score, stu.Grade, stu.Sex)
}

//自定义构造函数
func newStu(name, grade, sex string, score float32, id int) *Stu {
	stu := &Stu{
		StuID: id,
		Name:  name,
		Score: score,
		Grade: grade,
		Sex:   sex,
	}
	return stu
}

//判断学生是否存在
func (stuSystem *StuSystem) isExist(id int) (int, bool) {
	for index, val := range stuSystem.stus {
		if val.StuID == id {
			return index, true
		}
	}
	return -1, false
}

func inputStu() *Stu {
	var (
		id    int
		name  string
		score float32
		grade string
		sex   string
	)
	fmt.Println("请输入 学号,姓名,分数,年级,性别")
	fmt.Scanf("%d %s %f %s %s\r\n", &id, &name, &score, &grade, &sex)
	return newStu(name, grade, sex, score, id)

}

//添加学生
func (stuSystem *StuSystem) insertStu() {
	stu := inputStu()
	if _, ok := stuSystem.isExist(stu.StuID); !ok {
		stuSystem.stus = append(stuSystem.stus, *stu)
		fmt.Println("添加成功")
	} else {
		fmt.Println("学号重复,添加失败")
	}
}

//修改学生信息
func (stuSystem *StuSystem) changeStu() {
	stu := inputStu()
	if index, ok := stuSystem.isExist(stu.StuID); ok {
		stuSystem.stus[index] = *stu
		fmt.Println("修改成功")
	} else {
		fmt.Println("学号不存在,不能修改")
	}
}

//删除学生信息
func (stuSystem *StuSystem) deleteStu() {
	fmt.Println("请输入学生ID")
	var id int
	fmt.Scanf("%d\r\n", &id)
	if index, ok := stuSystem.isExist(id); ok {
		stu := stuSystem.stus[index]
		fmt.Printf("stuID:%d, name:%s, score:%0.2f, grade:%s, sex:%s\n", stu.StuID, stu.Name, stu.Score, stu.Grade, stu.Sex)
		fmt.Print("确认删除该生? 确认请输入1\n")
		var option int
		fmt.Scanf("%d\r\n", &option)
		if option == 1 {
			stuSystem.stus = append(stuSystem.stus[0:index], stuSystem.stus[index+1:]...)
			fmt.Println("删除成功!")
		}
	} else {
		fmt.Println("该生不存在或输入有误")
	}
}

//查询学生信息
func (stuSystem *StuSystem) queryStu() {
	fmt.Println("请输入学生ID")
	var id int
	fmt.Scanf("%d\r\n", &id)
	if index, ok := stuSystem.isExist(id); ok {
		stu := stuSystem.stus[index]
		stu.PrintStu()
	} else {
		fmt.Println("该生不存在或输入有误")
	}
}

func (stuSystem StuSystem) sortStu() {
	fmt.Print("请选择排序方式\n1. 按学号排序\n2. 按姓名排序\n3. 按分数排序\n")
	fmt.Scanf("%d\r\b", &stuSystem.option)
	sort.Sort(stuSystem)
	if stuSystem.option > 0 || stuSystem.option < 4 {
		fmt.Println("排序成功")
	}
}

//打印学生信息
func (stuSystem *StuSystem) printStus() {
	for _, stu := range stuSystem.stus {
		stu.PrintStu()
	}
}

//Menu 系统菜单
func Menu() {
	stuSystem := NewStuSystem()
	for {
		fmt.Print("请选择功能\n1. 添加学生信息\n2. 删除学生信息\n3. 修改学生信息\n4. 查询学生信息\n5. 打印全部学生信息\n6. 学生排序\n7. 退出\n")
		var option int
		fmt.Scanf("%d\r\b", &option)
		switch option {
		case 1:
			stuSystem.insertStu()
		case 2:
			stuSystem.deleteStu()
		case 3:
			stuSystem.changeStu()
		case 4:
			stuSystem.queryStu()
		case 5:
			stuSystem.printStus()
		case 6:
			stuSystem.sortStu()
		case 7:
			fmt.Println("谢谢使用")
			os.Exit(0)
		default:
			fmt.Println("输入有误,请重新输入")
			continue
		}
	}
}

main包

package main

import "github.com/hby_gd/student/utils"

func main() {
	utils.Menu()
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值