已开启
学生信息管理系统 #129
学生信息管理系统 #129
已开启
魏蕾创建于 6月20日
共 7 个文件变更+742-0
@@ -0,0 +1,94 @@
1+package ex5
2+ 
3+import std.convert.*
4+ 
5+/**
6+ * 课程性质 枚举类型
7+ */
8+public enum Electivity <: ToString {
9+ | Compulsory // 必修课
10+ | Elective // 选修课
11+ 
12+ // ToString 接口定义的转换字符串的函数
13+ public func toString() {
14+ match (this) {
15+ case Compulsory => "必修课"
16+ case Elective => "选修课"
17+ }
18+ }
19+}
20+ 
21+/**
22+ * 课程 类
23+ */
24+public class Course <: ToString {
25+ /**
26+ * 主构造函数
27+ *
28+ * @param id 课程编号
29+ * @param name 课程名称
30+ * @param credit 课程学分
31+ * @param electivity 课程性质
32+ */
33+ public Course (
34+ private let id: UInt32,
35+ private var name: String,
36+ private var credit: Float16,
37+ private var electivity!: Electivity = Electivity.Compulsory
38+ ) {}
39+
40+ // 成员属性propId
41+ prop propId: UInt32 {
42+ // 成员属性propId的getter
43+ get() {
44+ id
45+ }
46+ }
47+ 
48+ // 成员属性propName
49+ mut prop propName: String {
50+ // 成员属性propName的getter
51+ get() {
52+ name
53+ }
54+ 
55+ // 成员属性propName的setter
56+ set(name) {
57+ this.name = name
58+ }
59+ }
60+ 
61+ // 成员属性propElectivity
62+ mut prop propElectivity: Electivity {
63+ // 成员属性propElectivity的getter
64+ get() {
65+ electivity
66+ }
67+ 
68+ // 成员属性propElectivity的setter
69+ set(electivity) {
70+ this.electivity = electivity
71+ }
72+ }
73+ 
74+ // 成员属性propCredit
75+ mut prop propCredit: Float16 {
76+ // 成员属性propCredit的getter
77+ get() {
78+ credit
79+ }
80+ 
81+ // 成员属性propCredit的setter
82+ set(credit) {
83+ this.credit = credit
84+ }
85+ }
86+ 
87+ /*
88+ * 输出课程信息(ToString 接口定义的转换字符串的函数)
89+ */
90+ public func toString() {
91+ "课程: ${id} ${name} 性质: ${electivity} 学分: ${credit.format(".1")}"
92+ }
93+}
94+ 
@@ -0,0 +1,149 @@
1+package ex5
2+ 
3+import std.collection.*
4+import std.env.*
5+ 
6+/**
7+ * 课程 管理类
8+ */
9+public class CourseManager {
10+ // 单例模式
11+ // 无法从外部访问构造函数
12+ private CourseManager(
13+ var courses!: ArrayList<Course> = ArrayList<Course>() //课程列表
14+ ) {}
15+ 
16+ // 单例对象
17+ private static let instance: CourseManager = CourseManager()
18+ // 通过属性获取单例对象
19+ static prop propInstance: CourseManager {
20+ get() {
21+ instance
22+ }
23+ }
24+ 
25+ /**
26+ * 打印课程信息
27+ */
28+ func printCourses() {
29+ for (course in courses) {
30+ println(course.toString())
31+ }
32+ println("课程总数: ${courses.size}!")
33+ }
34+ 
35+ /**
36+ * 录入课程信息
37+ */
38+ func inputCourse() {
39+ println("开始录入课程信息:")
40+ try {
41+ // 获取用户输入的基本信息
42+ print("请输入课程编号:")
43+ let id = inputInfo<UInt32>()
44+ print("请输入课程名称:")
45+ let name = getStdIn().readln() ?? ""
46+ print("请输入课程性质(必修【1】,选修【2】):")
47+ let electivity = if (inputInfo<Int64>(1, 2) == 1) { Compulsory } else { Elective }
48+ print("请输入课程学分:")
49+ let credit = inputInfo<Float16>(0.0, 10.0)
50+ // 创建课程类对象
51+ let course = Course(id, name, credit, electivity: electivity)
52+ // 将course添加到courses中
53+ courses.add(course)
54+ println("录入完毕!")
55+ } catch (e : Exception) {
56+ println(e.message)
57+ }
58+ }
59+ 
60+ /**
61+ * 查询课程信息
62+ */
63+ func queryCourse() {
64+ print("请输入课程号:")
65+ let courseId = inputInfo<UInt32>()
66+ 
67+ // 查找课程
68+ for (course in courses) {
69+ // 查找到课程
70+ if (courseId == course.propId) {
71+ println("查询成功,信息如下:")
72+ println(course.toString())
73+ return
74+ }
75+ }
76+ 
77+ println("未找到课程!")
78+ }
79+ 
80+ /**
81+ * 删除课程信息
82+ */
83+ func deleteCourse() {
84+ 
85+ print("请输入课程号:")
86+ let courseId = inputInfo<UInt32>()
87+ 
88+ // 查找课程
89+ for (i in 0..courses.size) {
90+ // 查找到课程
91+ if (courseId == courses[i].propId) {
92+ courses.remove(at: i)
93+ println("编号${courseId}课程删除成功!")
94+ return
95+ }
96+ }
97+ println("未找到课程!")
98+ }
99+ 
100+ /**
101+ * 修改课程信息
102+ */
103+ func modifyCourse() {
104+ print("请输入课程号:")
105+ let courseId = inputInfo<UInt32>()
106+ 
107+ // 查找课程
108+ for (course in courses) {
109+ // 查找到课程
110+ if (courseId == course.propId) {
111+ println("查询成功,信息如下:")
112+ println(course.toString())
113+ 
114+ // 修改信息
115+ print("是否修改课程名称?(是【1】 否【2】):")
116+ if (inputInfo<Int64>(1, 2) == 1) {
117+ print("请输入课程名称:")
118+ course.propName = getStdIn().readln() ?? ""
119+ }
120+ print("是否修改课程性质?(是【1】 否【2】):")
121+ if (inputInfo<Int64>(1, 2) == 1) {
122+ print("请输入课程性质(必修【1】,选修【2】):")
123+ course.propElectivity = if (inputInfo<Int64>(1, 2) == 1) { Compulsory } else { Elective }
124+ }
125+ print("是否修改课程学分?(是【1】 否【2】):")
126+ if (inputInfo<Int64>(1, 2) == 1) {
127+ print("请输入课程学分:")
128+ course.propCredit = inputInfo<Float16>(0.0, 10.0)
129+ }
130+ return
131+ }
132+ }
133+ println("未找到课程!")
134+ }
135+ 
136+ /*
137+ * 通过id查询课程
138+ */
139+ func courseById(courseId: UInt32): ?Course {
140+ for (course in courses) {
141+ if (course.propId == courseId) {
142+ return Some(course)
143+ }
144+ }
145+
146+ return None
147+ }
148+}
149+ 
@@ -0,0 +1,31 @@
1+package ex5
2+ 
3+import std.collection.*
4+ 
5+func initData() {
6+ // 创建一个临时的 ArrayList 容器,用于存放 Student(学生)对象
7+ let studentData = ArrayList<Student>()
8+ // 向临时列表中添加初始学生数据
9+ studentData.add(Student(2501, "panda", 20, Boy, "Class3")) // 添加学号2501的学生
10+ studentData.add(Student(2502, "zoom", 19, Boy, "Class3")) // 添加学号2502的学生
11+ studentData.add(Student(2303, "zip", 19, Girl, "Class4")) // 添加学号2303的学生
12+ /**
13+ * 注入初始化数据到全局管理器
14+ * 通过 StudentManager 的单例属性 (propInstance) 访问唯一的管理器实例,
15+ * 并将准备好的学生列表 (studentData) 赋值给其内部变量 students。
16+ * 这样做确保了整个程序各模块共享同一份学生数据源,体现了单例模式的数据一致性。
17+ */
18+ StudentManager.propInstance.students = studentData
19+ 
20+ // 创建一个临时的 ArrayList 容器,用于存放 Course(课程)对象
21+ let courseData = ArrayList<Course>()
22+ // 向临时列表中添加初始课程数据
23+ courseData.add(Course(101, "Math", 3.0, electivity: Compulsory)) // 添加必修课:数学
24+ courseData.add(Course(102, "Chinese", 3.0, electivity: Compulsory)) // 添加必修课:语文
25+ courseData.add(Course(103, "Art", 1.0, electivity: Elective)) // 添加选修课:美术
26+ courseData.add(Course(104, "Music", 2.0, electivity: Elective)) // 添加选修课:音乐
27+ // 将准备好的课程列表赋值给 CourseManager 单例中的 courses 属性
28+ CourseManager.propInstance.courses = courseData
29+ 
30+ 
31+}
@@ -0,0 +1,116 @@
1+package ex5
2+ 
3+import std.convert.*
4+import std.env.*
5+ 
6+/**
7+ * 主函数
8+ */
9+main() {
10+ // 初始化数据
11+ initData()
12+ // 死循环:获取并执行用户输入的命令。
13+ while(true) {
14+ println("请输入命令:")
15+ println("清屏【0】 学生管理模块【1】 课程管理模块【2】 成绩打印块【3】 退出【4】")
16+ // 获取输入信息
17+ var input = getStdIn().readln() ?? ""
18+ // 获取命令代码
19+ if( let Some(value) <- Int32.tryParse(input)) {
20+ match (value) {
21+ // 【0】主动清屏
22+ case 0 => clear()
23+ // 【1】学生管理模块
24+ case 1 => manageStudents()
25+ // 【2】课程管理模块
26+ case 2 => manageCourses()
27+ // 【3】成绩打印模块
28+ //case 3 => manageScores()
29+ // 【4】退出
30+ case 4 => break
31+ // 命令输入错误
32+ case _ => println("命令输入错误,请重新输入!")
33+ }
34+ } else {
35+ println("命令输入错误,请重新输入!")
36+ }
37+ // pause()
38+ }
39+ return 0
40+}
41+ 
42+ 
43+/**
44+ * 学生管理模块
45+ */
46+func manageStudents() {
47+ // 学生管理器
48+ let mgr = StudentManager.propInstance
49+ 
50+ while(true) {
51+ println("请输入命令:")
52+ println("清屏【0】 打印所有学生信息【1】 添加学生信息【2】 查询学生信息【3】 删除学生信息【4】 修改学生信息【5】 退出【6】")
53+ // 获取输入信息
54+ var input = inputInfo<Int32>()
55+ // 获取命令代码
56+ clear()
57+ match (input) {
58+ // 【0】主动清屏
59+ case 0 => clear()
60+ // 【1】打印所有学生信息
61+ case 1 => mgr.printStudents()
62+ // 【2】添加学生信息
63+ case 2 => mgr.inputStudent()
64+ // 【3】查询学生信息
65+ case 3 => mgr.queryStudent()
66+ // 【4】删除学生信息
67+ case 4 => mgr.deleteStudent()
68+ // 【5】修改学生信息
69+ case 5 => mgr.modifyStudent ()
70+ // 退出学生管理
71+ case 6 => break
72+ // 命令输入错误
73+ case _ => println("无效命令!")
74+ }
75+ // pause()
76+ }
77+}
78+ 
79+ 
80+ 
81+/**
82+ * 课程管理模块
83+ */
84+func manageCourses() {
85+ // 课程管理器
86+ let mgr = CourseManager.propInstance
87+ while(true) {
88+ println("请输入命令:")
89+ println("清屏【0】 打印所有课程【1】 录入课程【2】 查询课程【3】 删除课程【4】 修改课程【5】 退出【6】")
90+ // 获取输入信息
91+ var input = inputInfo<Int32>()
92+ // 获取命令代码
93+ clear()
94+ match (input) {
95+ // 【0】主动清屏
96+ case 0 => clear()
97+ // 【1】打印所有课程
98+ case 1 => mgr.printCourses()
99+ // 【2】录入课程
100+ case 2 => mgr.inputCourse()
101+ // 【3】查询课程
102+ case 3 => mgr.queryCourse()
103+ // 【4】删除课程
104+ case 4 => mgr.deleteCourse()
105+ // 【5】修改课程
106+ case 5 => mgr.modifyCourse()
107+ // 【6】退出
108+ case 6 => break
109+ // 命令输入错误
110+ case _ => println("命令输入错误,请重新输入!")
111+ }
112+ pause()
113+ }
114+}
115+ 
116+ 
@@ -0,0 +1,115 @@
1+package ex5
2+ 
3+/*
4+ * 性别 枚举类型
5+ */
6+public enum Sex <: ToString {
7+ | Girl | Boy
8+ 
9+ // ToString 接口定义的转换字符串的函数
10+ public func toString() {
11+ match (this) {
12+ case Girl => "女"
13+ case Boy => "男"
14+ }
15+ }
16+}
17+ 
18+/**
19+ * 学生 类
20+ */
21+public class Student <: ToString {
22+ 
23+ /**
24+ * 静态变量 学校名称
25+ */
26+ public static let schoolName = "合肥师范学院"
27+ 
28+ // 成员变量
29+ private var id : UInt32 // 学号
30+ private var name : String // 姓名
31+ private var age : UInt8 // 年龄
32+ private var sex : Sex // 性别
33+ private var classBelonged : String // 班级
34+ 
35+ // 成员属性propId
36+ prop propId: UInt32 {
37+ // 成员属性propId的getter
38+ get() {
39+ id
40+ }
41+ }
42+ // 成员属性propName
43+ mut prop propName: String {
44+ // 成员属性propName的getter
45+ get() {
46+ name
47+ }
48+ // 成员属性propName的setter
49+ set(name) {
50+ this.name = name
51+ }
52+ }
53+ // 成员属性propAge
54+ mut prop propAge: UInt8 {
55+ // 成员属性propAge的getter
56+ get() {
57+ age
58+ }
59+ // 成员属性propAge的setter
60+ set(age) {
61+ this.age = age
62+ }
63+ }
64+ 
65+ // 成员属性propSex
66+ mut prop propSex: Sex {
67+ // 成员属性propSex的getter
68+ get() {
69+ sex
70+ }
71+ // 成员属性propSex的setter
72+ set(sex) {
73+ this.sex = sex
74+ }
75+ }
76+ 
77+ // 成员属性propClassBelonged
78+ mut prop propClassBelonged: String {
79+ // 成员属性propClassBelonged的getter
80+ get() {
81+ classBelonged
82+ }
83+ // 成员属性propClassBelonged的setter
84+ set(classBelonged) {
85+ this.classBelonged = classBelonged
86+ }
87+ }
88+ 
89+ /**
90+ * 普通构造函数
91+ *
92+ * @param id 学号
93+ * @param name 姓名
94+ * @param age 年龄
95+ * @param sex 性别
96+ * @param classBelonged 班级
97+ *
98+ */
99+ public init (id : UInt32, name : String, age : UInt8, sex : Sex, classBelonged : String) {
100+ // 初始化成员变量
101+ this.id = id
102+ this.name = name
103+ this.age = age
104+ this.sex = sex
105+ this.classBelonged = classBelonged
106+ }
107+ 
108+ /**
109+ * 输出学生信息(ToString 接口定义的转换字符串的函数)
110+ */
111+ public func toString() {
112+ return "学号: ${id} 姓名: ${name} 年龄: ${age} 性别: ${sex} 学校班级: ${schoolName} ${classBelonged}"
113+ }
114+}
115+ 
@@ -0,0 +1,163 @@
1+package ex5
2+ 
3+import std.env.*
4+import std.collection.*
5+ 
6+/**
7+ * 学生 管理类
8+ */
9+public class StudentManager {
10+ // 单例模式
11+ // 无法从外部访问构造函数
12+ private StudentManager(
13+ var students!: ArrayList<Student> = ArrayList<Student>() // 学生列表
14+ ) {}
15+ 
16+ // 单例对象
17+ private static let instance : StudentManager = StudentManager()
18+ // 通过属性获取单例对象
19+ static prop propInstance: StudentManager {
20+ get() {
21+ instance
22+ }
23+ }
24+ 
25+ /**
26+ * 输出学生信息
27+ */
28+ func printStudents() {
29+ for (stu in students) {
30+ println(stu.toString())
31+ }
32+ println("学生总数: ${students.size}")
33+ }
34+ 
35+ 
36+ /**
37+ * 录入学生信息
38+ */
39+ func inputStudent() {
40+ println("开始录入学生信息:")
41+ try {
42+ // 获取用户输入的基本信息
43+ print("请输入学号:")
44+ let id = inputInfo<UInt32>()
45+ print("请输入姓名:")
46+ let name = getStdIn().readln() ?? ""
47+ print("请输入年龄:")
48+ let age = inputInfo<UInt8>(0, 255)
49+ print("请输入性别(0代表女;1代表男):")
50+ let sex = if (inputInfo<Int64>(0, 1) == 0) { Girl } else { Boy }
51+ print("请输入班级:")
52+ let classBelonged = getStdIn().readln() ?? ""
53+ // 创建学生类对象
54+ let student = Student(id, name, age, sex, classBelonged)
55+ // 将student添加到students中
56+ students.add(student)
57+ println("成功录入学生信息!")
58+ // 输出学生信息
59+ printStudents()
60+ } catch (e : Exception) {
61+ println(e.message)
62+ }
63+ }
64+ 
65+ /**
66+ * 查询学生信息
67+ */
68+ func queryStudent() {
69+ 
70+ print("请输入学号:")
71+ let studentId = inputInfo<UInt32>()
72+ // 查找学生
73+ for (stu in students) {
74+ // 找到学生
75+ if (studentId == stu.propId) {
76+ println("查询成功,信息如下:")
77+ println(stu.toString())
78+ return
79+ }
80+ }
81+ println("未找到学生!")
82+ }
83+ 
84+ /**
85+ * 删除学生信息
86+ */
87+ func deleteStudent() {
88+ 
89+ print("请输入学号:")
90+ var studentId = inputInfo<UInt32>()
91+ 
92+ // 查找学生
93+ for (i in 0..students.size) {
94+ // 找到学生
95+ if (studentId == students[i].propId) {
96+ students.remove(at: i)
97+ println("删除成功!")
98+ return
99+ }
100+ }
101+ println("未找到学生!")
102+ }
103+ 
104+ 
105+ 
106+ /**
107+ * 修改学生信息
108+ */
109+ func modifyStudent() {
110+ 
111+ print("请输入学号:")
112+ var studentId = inputInfo<UInt32>()
113+ 
114+ // 查找学生
115+ for (stu in students) {
116+ // 找到学生
117+ if (studentId == stu.propId) {
118+ println("查询成功,信息如下:")
119+ println(stu.toString())
120+ 
121+ // 修改信息
122+ print("是否修改姓名?(【1】是 2【否】):")
123+ if (inputInfo<Int64>(1, 2) == 1) {
124+ print("请输入姓名:")
125+ stu.propName = getStdIn().readln() ?? ""
126+ }
127+ print("是否修改年龄?(【1】是 2【否】):")
128+ if (inputInfo<Int64>(1, 2) == 1) {
129+ print("请输入年龄:")
130+ stu.propAge = inputInfo<UInt8>(0, 255)
131+ }
132+ print("是否修改性别?(【1】是 2【否】):")
133+ if (inputInfo<Int64>(1, 2) == 1) {
134+ print("请输入性别(0代表女;1代表男):")
135+ stu.propSex = if (inputInfo<Int64>(0, 1) == 0) { Girl } else { Boy }
136+ }
137+ print("是否修改班级?(【1】是 2【否】):")
138+ if (inputInfo<Int64>(1, 2) == 1) {
139+ print("请输入班级:")
140+ stu.propClassBelonged = getStdIn().readln() ?? ""
141+ }
142+ println("修改成功!")
143+ return
144+ }
145+ }
146+ 
147+ println("未找到学生!")
148+
149+ }
150+ 
151+ /*
152+ * 通过id查询学生
153+ */
154+ func studentById(studentId: UInt32): ?Student {
155+ for (student in students) {
156+ if (student.propId == studentId) {
157+ return Some(student)
158+ }
159+ }
160+ 
161+ return None
162+ }
163+}
@@ -0,0 +1,74 @@
1+package ex5
2+ 
3+import std.convert.*
4+import std.env.*
5+ 
6+/**
7+ * 将输入信息转换为指定类型的值;如输入信息有误,则重新输入
8+ *
9+ * @return 指定T类型的值
10+ */
11+func inputInfo<T>() : T where T <: Parsable<T> {
12+ var retryNum = 3
13+ while (retryNum != 0) {
14+ var input = getStdIn().readln() ?? ""
15+ var value = T.tryParse(input)
16+ if (value.isSome()) {
17+ return value.getOrThrow()
18+ } else {
19+ print("输入错误,请重新输入(还有${retryNum}次机会):")
20+ }
21+ retryNum --
22+ }
23+ throw Exception("输入错误超过3次,取消录入!")
24+}
25+ 
26+ 
27+/**
28+ * 将输入信息转换为指定类型的值;如输入信息有误,则重新输入
29+ *
30+ * @param min 数值最小值
31+ * @param max 数值最大值
32+ * @return 指定T类型的值
33+ */
34+func inputInfo<T>(min : T, max : T) : T where T <: Parsable<T> & Comparable<T> {
35+ var retryNum = 3
36+ while (retryNum != 0) {
37+ var input = getStdIn().readln() ?? ""
38+ var value = T.tryParse(input)
39+ if (value.isSome()) {
40+ // 检查是否在范围之内
41+ if (value.getOrThrow() < min || value.getOrThrow() > max) {
42+ print("数值范围超限,请重新输入:")
43+ retryNum --
44+ continue
45+ }
46+ return value.getOrThrow()
47+ } else {
48+ print("输入错误,请重新输入(还有${retryNum}次机会):")
49+ retryNum --
50+ }
51+ }
52+ throw Exception("输入错误超过3次,取消录入!")
53+}
54+ 
55+/**
56+* 清除控制台屏幕并复位光标
57+*/
58+func clear() {
59+ // print("\u{1b}[H\u{1b}[J")
60+ print("\u{1b}[2J\u{1b}[H")
61+ // for (_ in 0..50) {
62+ // println("")
63+ // }
64+}
65+ 
66+/*
67+* 暂停函数
68+*/
69+func pause() {
70+ println("点击回车键继续...")
71+ getStdIn().readln()
72+ clear()
73+}
74+