import scala.io.StdIn.readLine import scala.util.control.Breaks._

import java.io.{BufferedReader, File, FileWriter, FileReader, IOException} import scala.collection.mutable.ListBuffer

object StudentManagementSystem { val filename: String = "students.txt"

def main(args: Array[String]): Unit = { breakable { while (true) { menu() val choice: String = readLine("请选择功能:") choice match { case "1" => insert() case "2" => delete() case "3" => modify() case "4" => search() case "5" => show() case "6" => val answer: String = readLine("您确定退出系统吗?y/n") if (answer.equalsIgnoreCase("y")) { println("欢迎您的使用!!") break() } case _ => println("输入有误,请重新输入") } readLine() } } }

def menu(): Unit = { println("==================学生信息管理系统==============") println("===============功能菜单==================") println("\t\t\t1. 添加学生信息") println("\t\t\t2. 删除学生信息") println("\t\t\t3. 修改学生信息") println("\t\t\t4. 查找学生信息") println("\t\t\t5. 显示学生信息") println("\t\t\t6. 退出学生信息系统") println("======================================") }

def insert(): Unit = { val student_list: ListBuffer[Map[String, Any]] = ListBufferMap[String, Any] breakable { while (true) { val id: String = readLine("请添加新同学的ID:") if (id.isEmpty) { break() } val name: String = readLine("请添加新同学的姓名:") if (name.isEmpty) { break() } try { val phoneNumber: String = readLine("请添加新同学的手机号:") if (phoneNumber.length != 11) { println("手机号位数不正确,请重新输入") continue() } val qq号: String = readLine("请添加新同学的QQ号:") if (qq号.length < 5 || qq号.length > 10) { println("QQ号位数不正确,请重新输入") continue() } val 微信号: String = readLine("请添加新同学的微信号:") val student: Map[String, Any] = Map("id" -> id, "name" -> name, "PhoneNumber" -> phoneNumber.toInt, "QQ号" -> qq号.toInt, "微信号" -> 微信号) student_list.append(student) val answer: String = readLine("是否继续添加y/n?") if (answer.equalsIgnoreCase("n")) { break() } } catch { case _: NumberFormatException => println("输入无效,请重新输入") } } } save(student_list.toList) println("学生信息保存成功") }

def save(lst: List[Map[String, Any]]): Unit = { var stu_txt: FileWriter = null try { stu_txt = new FileWriter(filename, true) } catch { case e: IOException => stu_txt = new FileWriter(filename) } for (item <- lst) { stu_txt.write(item.toString + '\n') } stu_txt.close() }

def search(): Unit = { val student_query: ListBuffer[Map[String, Any]] = ListBufferMap[String, Any] breakable { while (true) { var id: String = "" var name: String = "" if (new File(filename).exists()) { val mode: String = readLine("按ID查找请按1,按姓名查找请按2:") if (mode == "1") { id = readLine("请输入学生ID:") } else if (mode == "2") { name = readLine("请输入学生姓名:") } else { println("输入有误,请重新输入") search() } val file: BufferedReader = new BufferedReader(new FileReader(filename)) var student: String = file.readLine() while (student != null) { val d: Map[String, Any] = eval(student) if (!id.isEmpty) { if (d("id") == id) { student_query.append(d) } } else if (!name.isEmpty) { if (d("name") == name) { student_query.append(d) } } student = file.readLine() } file.close() show_query(student_query.toList) student_query.clear() val answer: String = readLine("是否继续查询?y/n") if (answer.equalsIgnoreCase("n")) { break() } } else { println("无学生信息") return } } } }

def show_query(lst: List[Map[String, Any]]): Unit = { if (lst.isEmpty) { println("无相关信息") return } val format_title: String = "%-6s\t%-12s\t%-8s\t%-10s\t%-10s\t%-8s" println(format_title.format("ID", "姓名", "手机号", "QQ号", "微信号", "")) val format_data: String = "%-6s\t%-12s\t%-8s\t%-10s\t%-10s\t%-8s" for (item <- lst) { println(format_data.format(item("id").toString, item("name").toString, item("PhoneNumber").toString, item("QQ号").toString, item("微信号").toString, "")) } }

def delete(): Unit = { breakable { while (true) { val student_id: String = readLine("请输入删除学生的ID:") if (!student_id.isEmpty) { if (new File(filename).exists()) { val file: BufferedReader = new BufferedReader(new FileReader(filename)) val student_old: ListBuffer[String] = ListBufferString var line: String = file.readLine() while (line != null) { student_old.append(line) line = file.readLine() } file.close() var flag: Boolean = false if (student_old.nonEmpty) { val wfile: FileWriter = new FileWriter(filename) for (item <- student_old) { val d: Map[String, Any] = eval(item) if (d("id") != student_id) { wfile.write(d.toString + '\n') } else { flag = true } } wfile.close() if (flag) { println(s"id为$student_id的学生信息已被删除") } else { println(s"没有找到学生ID为$student_id的学生") } } else { println("无学生信息") break() } show() val answer: String = readLine("是否继续删除?y/n") if (answer.equalsIgnoreCase("n")) { break() } } } } } }

def modify(): Unit = { show() if (new File(filename).exists()) { val file: BufferedReader = new BufferedReader(new FileReader(filename)) val student_old: ListBuffer[String] = ListBufferString var line: String = file.readLine() while (line != null) { student_old.append(line) line = file.readLine() } file.close() val student_id: String = readLine("请输入修改学生的ID:") val wfile: FileWriter = new FileWriter(filename) for (item <- student_old) { val d: Map[String, Any] = eval(item) if (d("id") == student_id) { println("已经找到学生信息,请修改相关信息") breakable { while (true) { try { d("name") = readLine("请输入姓名") d("PhoneNumber") = readLine("请修改手机号信息").toInt if (d("PhoneNumber").toString.length != 11) { println("手机号位数不正确,请重新输入") continue() } d("QQ号") = readLine("请修改QQ号信息").toInt if (d("QQ号").toString.length < 5 || d("QQ号").toString.length > 10) { println("QQ号位数不正确,请重新输入") continue() } d("微信号") = readLine("请修改微信号信息") } catch { case _: NumberFormatException => println("输入有误,请重新输入") } break() } } wfile.write(d.toString + '\n') println("修改成功") } else { wfile.write(d.toString + '\n') } } wfile.close() show() } }

def show(): Unit = { val student_query: ListBuffer[Map[String, Any]] = ListBufferMap[String, Any] if (new File(filename).exists()) { val file: BufferedReader = new BufferedReader(new FileReader(filename)) var line: String = file.readLine() while (line != null) { val d: Map[String, Any] = eval(line) student_query.append(d) line = file.readLine() } file.close() show_query(student_query.toList) student_query.clear() } else { println("无学生信息") return } }

def eval(str: String): Map[String, Any] = { str.stripPrefix("Map(").stripSuffix(")").split(',').map { s => val pair: Array[String] = s.split(" -> ") pair(0).stripPrefix(" ").stripSuffix(" ") -> pair(1).stripSuffix(" ").stripSuffix(" ").stripPrefix(" ") }.toMap }

import ospathfilename = studentstxtdef main while True menu choice = input请选择功能 if choice == 6 answer = input您确定退出系统吗?yn if answer == y or answer == Y

原文地址: https://www.cveoy.top/t/topic/hPGC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录