结构体-练习题

学生管理系统

package main

import (
	"fmt"
	"os"
	"strconv"
)

/*
使用面向对象的思维方式实现一个简单的学生信息管理系统。
每个学生有姓名、性别、年龄、班级信息,
用户可以在控制台添加学生、修改学生信息、删除学生、打印所有的学生列表。
*/
// 1. 打印出来全部的学生
// 2. 添加学生信息
// 3. 删除学生信息
// 4. 修改学生信息

//定义了一个学生信息map
var allStudent map[int]*student

//班级有自己的信息
type class struct {
	classname string //年级
	classid   string //班级
}

//学生有自己的信息
type student struct {
	id   int
	age  int
	name string
	class
}

//平台上面有学生信息
type sms struct {
	name    string
	student map[int]*student
}

//造一个学生
func newStudent(id, age int, classid, name, classname string) *student {
	return &student{
		id:   id,
		age:  age,
		name: name,
		class: class{
			classname: classname,
			classid:   classid,
		},
	}
}

//造一个平台,平台有名字,有学生列表
func newSms(allStudent map[int]*student, name string) *sms {
	return &sms{
		name:    name,
		student: allStudent,
	}
}

//平台上面有操作方式--查看
func (s *sms) showstudent() {
	for k, v := range allStudent {
		fmt.Printf("学号:%d\t学生年龄:%d\t学生名称:%s\t学生年级:%s\t学生班级名称:%s\n", k, v.age, v.name, v.classname, v.classid)
	}
}

//平台上面有操作方式--添加
func (s *sms) addstudent() {
	var (
		id        int
		age       int
		name      string
		classname string
		classid   string
		tag       = true
		o         string
	)
	for tag {
		fmt.Println("输入需要添加的学生的姓名:")
		fmt.Scanln(&name)
		fmt.Println("输入需要添加的学生的学号:")
		fmt.Scanln(&id)
		fmt.Println("输入需要添加的学生的年龄:")
		fmt.Scanln(&age)
		fmt.Println("输入需要添加的学生的年级:")
		fmt.Scanln(&classname)
		fmt.Println("输入需要添加的学生的班级:")
		fmt.Scanln(&classid)

		_, ok := allStudent[id]
		if ok {
			for key := range allStudent {
				if key != id {
					fmt.Println("添加学生成功!!")
					allStudent[id] = newStudent(id, age, classid, name, classname)
					break
				}
				fmt.Println("--------输入的学号已经存在!!!!")
			}
		} else {
			fmt.Println("添加学生成功!")
			allStudent[id] = newStudent(id, age, classid, name, classname)
		}
		fmt.Println("是否退出(y/n):")
		fmt.Scanln(&o)
		if o == "y" {
			tag = false
		}
	}
}

//平台上面有操作方式--修改
func (s *sms) upstudent() {
	var (
		upname string
		upid   int
		choice int
		pri    = func() (*student, string) { //定义匿名函数
			fmt.Print("输入修改的后的值:")
			fmt.Scanln(&upname)
			for k, st := range allStudent {
				if upid == k {
					return st, upname
				}
			}
			return nil, ""
		}
	)

	fmt.Println("输入要修改的学号:")
	fmt.Scanln(&upid)
	fmt.Print(`
 进入平台学生信息修改界面:
	1. 修改姓名
	2. 修改年龄
	3. 修改年级
	4. 修改班级
 `)
	fmt.Print("输入修改的选项:")
	fmt.Scanln(&choice)
	switch choice {
	case 1:
		st, upname := pri()
		if st != nil {
			st.name = upname
			fmt.Printf("-------->修改完成!%s\n", upname)
		} else {
			fmt.Printf("-------->修改出现问题!%s\n", upname)
		}
	case 2:
		st, upname := pri()
		if st != nil {
			cc, _ := strconv.Atoi(upname)
			st.age = int(cc)
			fmt.Printf("-------->修改完成!%s\n", upname)
		} else {
			fmt.Printf("-------->修改出现问题!%s\n", upname)
		}
	case 3:
		st, upname := pri()
		if st != nil {
			st.classname = upname
			fmt.Printf("-------->修改完成!%s\n", upname)
		} else {
			fmt.Printf("-------->修改出现问题!%s\n", upname)
		}
	case 4:
		st, upname := pri()
		if st != nil {
			st.classid = upname
			fmt.Printf("-------->修改完成!%s\n", upname)
		} else {
			fmt.Printf("-------->修改出现问题!%s\n", upname)
		}
	default:
		fmt.Println("滚~!!!")
	}
}

//平台上面有操作方式--删除
func (s *sms) delstudent() {
	var (
		id   int
		name string
	)
	fmt.Print("请输入需要删除的学号:")
	fmt.Scanln(&id)
	name = allStudent[id].name
	delete(allStudent, id)
	fmt.Printf("已经删除的学生%s\n", name)
}

//入口函数
func main() {
	allStudent = make(map[int]*student, 100)
	fmt.Println(allStudent)

	schoolms := newSms(allStudent, "大人学校")
	for {
		fmt.Printf("欢迎来带%s平台!!!\n", schoolms.name)
		fmt.Print(`
		1. 查看学生信息
		2. 新增学生信息
		3. 修改学生信息
		4. 删除学生信息
		5. 退出
 `)
		// 2. 选择
		var choice int
		fmt.Print("请输入你需要操作的选项:")
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			schoolms.showstudent()
		case 2:
			schoolms.addstudent()
		case 3:
			schoolms.upstudent()
		case 4:
			schoolms.delstudent()
		case 5:
			os.Exit(1)
		default:
			fmt.Println("滚~")
		}
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值