|
較簡(jiǎn)單的學(xué)生選課系統(tǒng),之前做Java設(shè)計(jì)時(shí)寫的,代碼量大概一千二百行左右,有需要的可以自取
51hei.png (4.37 KB, 下載次數(shù): 63)
下載附件
2021-12-16 01:18 上傳
- package user;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- import course.Course;
- import inputErr.UsrInput;
- public class User {
- public String name; //用戶名
- public String pwd; //密碼
-
- public User() {}
-
- /**
- * 顯示用戶信息
- */
- public void showInfo() {
- System.out.println(name+":"+pwd);
- }
- /**
- * 設(shè)置用戶密碼
- */
- public void changePwd() {
- System.out.print("輸入原密碼:");
-
- String originalPwd = UsrInput.getString();
- if (originalPwd.equals(this.pwd))
- while(true) {
- System.out.print("輸入新密碼:");
- String pwd = UsrInput.getString();
-
- System.out.print("確認(rèn)密碼:");
- String pwd2 = UsrInput.getString();
-
- if (pwd.length()>=6 && pwd.length()<=12 && pwd.equals(pwd2)) {
- this.pwd = pwd;
- System.out.println("密碼修改成功...");
- break;
- } else {
- continue;
- }
- }
- else {
- System.out.println("原密碼不正確...");
- }
-
- }
-
- public User login() {
- // get admin pwd for .txt file
- User.getAdminInfo();
- System.out.print("輸入管理員密碼:");
- String pwd = UsrInput.getString();
- if (pwd.equals(AdminUsers.admin.pwd)) {
- System.out.println(AdminUsers.admin.name + ", 歡迎您!");
- return AdminUsers.admin; //成功
- } else {
- System.out.println("登錄失敗,密碼錯(cuò)誤...");
- return null; //失敗
- }
- }
-
- public static void getAdminInfo() {
- File f = new File("admin pwd.txt");
- try {
- if (!f.exists())
- f.createNewFile();
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f.getAbsoluteFile())));
-
- String info = null;
-
- if ((info=br.readLine())==null) {
- AdminUsers.admin.name = "admin"; //管理員初始名稱
- AdminUsers.admin.pwd = "123456"; //管理員初始密碼123456
- } else {
- String info_[] = info.split(" ");
- AdminUsers.admin.name = info_[0];
- AdminUsers.admin.pwd = info_[1];
- }
- br.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- public static void saveAdminInfo() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter("admin pwd.txt"));
-
- out.write(AdminUsers.admin.name + " " + AdminUsers.admin.pwd); // write password
- out.flush();
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
-
- public void adminMenu() {
- int choice=0, innerChoice=0;
- while (true) {
- System.out.println("1.管理課程\n2.管理師傅\n3.管理學(xué)生\n4.修改管理員密碼\n5.修改管理員名稱\n0.退出");
- choice = UsrInput.getInt();
- switch(choice) {
- case 1:
- do {
- innerChoice=User.adminCourseMenu();
- if (innerChoice!=0)
- User.funcDistribute(innerChoice);
- } while (innerChoice!=0);
- break;
- case 2:
- do {
- innerChoice=User.adminTchMenu();
- if (innerChoice!=0)
- User.funcDistribute(innerChoice);
- } while (innerChoice!=0);
- break;
- case 3:
- do {
- innerChoice=User.adminStuMenu();
- if (innerChoice!=0)
- User.funcDistribute(innerChoice);
- } while (innerChoice!=0);
- break;
- case 4:
- AdminUsers.admin.changePwd();
- break;
- case 5:
- User.changeAdminName();
- break;
- case 0:
- Course.saveTofile(); //保存所有課程信息
- User.saveStuInfo();
- User.saveTchInfo();
- return;
- default: break;
- }
- }
- }
-
- public static void changeAdminName() {
- System.out.print("輸入新的管理員名稱:");
- String newAdminName = UsrInput.getString();
-
- AdminUsers.admin.name = newAdminName;
- System.out.println("名稱修改成功...");
- }
-
- public static void funcDistribute(int funcId) {
- switch (funcId) {
- case 1: Course.addCourses(); break;
- case 2: Course.deleteCourses(); break;
- case 3: Course.setCourseTeacher(); break;
- case 4: Course.showAllCourses(); break;
- case 5: Course.sortCourses(); break;
- case 6: User.addTeachers(); break;
- case 7: User.deleteTeachers(); break;
- case 8: Course.searchCourseByTeacher(); break;
- case 9: AdminUsers.showAllTcher(); break;
- case 10: User.resetPwd(1); break;
- case 11: User.addStudents(); break;
- case 12: User.deleteStudents(); break;
- case 13: AdminUsers.showAllStudent(); break;
- case 14: User.resetPwd(0); break;
- default:
- }
- }
-
- public static int adminCourseMenu() {
- System.out.println("選擇功能:\n1.添加課程\n2.刪除課程\n"
- + "3.設(shè)置課程師傅\n4.顯示課程列表\n5.按選課人數(shù)排序\n"
- + "0.退出");
- while (true) {
- int choice = UsrInput.getInt();
- if (choice==0) return 0;
- else if (choice>=1 && choice<=5)
- return choice;
- }
- }
- public static int adminTchMenu() {
- System.out.println("選擇功能:\n1.添加師傅\n2.刪除師傅\n"
- + "3.查看師傅所授課程\n4.顯示師傅列表\n"
- + "5.師傅密碼恢復(fù)\n0.退出");
- while (true) {
- int choice = UsrInput.getInt();
- if (choice==0) return 0;
- else if (choice>=1 && choice<=5)
- return choice+5;
- }
- }
- public static int adminStuMenu() {
- System.out.println("選擇功能:\n1.添加學(xué)生\n2.刪除學(xué)生\n"
- + "3.查看學(xué)生列表\n"
- + "4.學(xué)生密碼恢復(fù)\n0.退出");
- while (true) {
- int choice = UsrInput.getInt();
- if (choice==0) return 0;
- else if (choice>=1 && choice<=4)
- return choice+10;
- }
- }
- public static void addTeacher() {
- System.out.print("姓名:");
- String name = UsrInput.getString();
- System.out.print("工號(hào):");
- int workId = UsrInput.getInt();
- System.out.print("職位:");
- String occupation = UsrInput.getString();
-
- AdminUsers.t.add(new Teacher(name, "123456", workId, occupation));
- }
-
- public static void addTeachers() {
- while(true) {
- User.addTeacher();
-
- System.out.print("師傅信息添加成功,是否繼續(xù)添加?(y/n)");
- if (UsrInput.getString().equals("y"))
- continue;
- else
- break;
- }
- }
- public static boolean deleteTeacher() {
- System.out.print("輸入要?jiǎng)h除的師傅信息工號(hào):");
- int objTeacher = UsrInput.getInt();
- Teacher tmp = null;
- for (int i=0; i<AdminUsers.t.size(); ++i)
- if ((tmp=AdminUsers.t.get(i)).getWorkId()==objTeacher) {
- AdminUsers.t.remove(i); //刪除師傅信息
- //首先級(jí)聯(lián)置空其教授的課程
- for (Course c : Course.courseSet)
- if (c.teacher.equals(tmp.name))
- c.teacher = "---";
- return true;
- }
- return false;
- }
- public static void deleteTeachers() {
- if (AdminUsers.t==null || AdminUsers.t.size()==0) {
- System.out.println("暫無(wú)師傅信息...");
- return; //空
- }
- while(true) {
- if (User.deleteTeacher())
- System.out.print("師傅信息刪除成功,是否繼續(xù)刪除?(y/n)");
- else
- System.out.print("刪除失敗,無(wú)匹配師傅信息...\n輸入y繼續(xù)刪除,輸入n退出:");
- if (UsrInput.getString().equals("y")) {
- continue;
- } else
- break;
- }
- }
-
- public static void addStudent() {
- System.out.print("姓名:");
- String name = UsrInput.getString();
- System.out.print("學(xué)號(hào):");
- int stuId = UsrInput.getInt();
- System.out.print("班級(jí):");
- String class_ = UsrInput.getString();
-
- AdminUsers.s.add(new Student(name, "123456", stuId, class_));
- }
-
- public static void addStudents() {
- while(true) {
- User.addStudent();
-
- System.out.print("學(xué)生信息添加成功,是否繼續(xù)添加?(y/n)");
- if (UsrInput.getString().equals("y"))
- continue;
- else
- break;
- }
- }
-
- public static boolean deleteStudent() {
- System.out.print("輸入要?jiǎng)h除的學(xué)生信息學(xué)號(hào):");
- int objStu = UsrInput.getInt();
- Student tmp = null;
- for (int i=0; i<AdminUsers.s.size(); ++i)
- if (objStu==(tmp=AdminUsers.s.get(i)).getStuId()) {
- AdminUsers.s.remove(i);
-
- //該學(xué)生選修的課程人數(shù)減一
- for (Integer cid : Student.allStuCourses.get(tmp.getStuId()))
- for (Course c : Course.courseSet)
- if (c.id==cid.intValue())
- --c.stuNum;
- return true;
- }
- return false;
- }
- public static void deleteStudents() {
- if (AdminUsers.s==null || AdminUsers.s.size()==0) {
- System.out.println("暫無(wú)任何學(xué)生信息...");
- return; //空
- }
- Scanner cin = new Scanner(System.in);
-
- while(true) {
- if (User.deleteStudent())
- System.out.print("學(xué)生信息刪除成功,是否繼續(xù)?(y/n)");
- else
- System.out.print("刪除失敗,無(wú)匹配的學(xué)生信息...\n輸入y繼續(xù)刪除,輸入n退出:");
- if (cin.next().equals("y")) {
- continue;
- } else
- break;
- }
- }
-
- public static void saveStuInfo() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter("student info.txt"));
-
- for (Student s : AdminUsers.s)
- out.write(s.toString()+"\r\n");
-
- out.flush();
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public static void readStuInfo() {
- File f = new File("student info.txt");
- try {
- if (!f.exists()) f.createNewFile();
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f.getAbsoluteFile())));
-
- String info = null;
-
- while ((info = br.readLine()) != null) {
- String[] info_ = info.split(" ");
-
- int stuId = Integer.parseInt(info_[0]);
- String name = info_[1];
- String class_ = info_[2];
- String pwd = info_[3];
-
- AdminUsers.s.add(new Student(name, pwd, stuId, class_));
- }
- br.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void saveTchInfo() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter("teacher info.txt"));
-
- for (Teacher t : AdminUsers.t)
- out.write(t.toString()+"\r\n");
-
- out.flush();
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public static void readTchInfo() {
- File f = new File("teacher info.txt");
- try {
- if (!f.exists()) f.createNewFile();
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f.getAbsoluteFile())));
-
- String info = null;
-
- while ((info = br.readLine()) != null) {
- String[] info_ = info.split(" ");
-
- int workId = Integer.parseInt(info_[0]);
- String name = info_[1];
- String occupation = info_[2];
- String pwd = info_[3];
-
- AdminUsers.t.add(new Teacher(name, pwd, workId, occupation));
- }
- br.close();
- } catch (FileNotFoundException e) {
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
代碼:
選課系統(tǒng).rar
(33.13 KB, 下載次數(shù): 6)
2021-12-14 16:41 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|