xxx学校/学院/大学信息管理系统
生活随笔
收集整理的這篇文章主要介紹了
xxx学校/学院/大学信息管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????????注意:本程序完全用java語言實現,下面的每個代碼段,都需要單獨創建一個類,其中老師部分和學生部分大致相同,還有非常大的優化空間,如果覺得存儲數據的空間太小,可以自行更改,或者將數組改為集合來進行存儲(可參考我的另一篇文章),本程序較為簡單,但對于新手來說,非常適合參考與編寫。
運行時的控制臺(小部分功能):
1、學生添加
2、 老師添加
?程序源碼如下:
1、程序入口
public class InfoManagerEntry {public static void main(String[] args) {Scanner in = new Scanner(System.in);//主菜單搭建while (true) {System.out.println("---------歡迎來到湖南城市學院信息管理系統---------");System.out.println("請輸入您的選擇:1、學生管理 2、教師管理 3、退出");String choice = in.next();switch (choice) {case "1"://System.out.println("歡迎來到學生管理系統");//開啟學生管理系統StudentController studentController = new StudentController();studentController.start();break;case "2": // System.out.println("歡迎來到教師管理系統");TeacherController teacherController = new TeacherController();teacherController.start();break;case "3":System.out.println("感謝您使用湖南城市學院信息管理系統,再見!");//退出正在運行的JVM虛擬機System.exit(0);break;default:System.out.println("您的輸入有誤,請重新輸入");break;}}} }2、學生管理部分
一、與用戶溝通部分
public class StudentController {//使用static關鍵字讓in成為類對象private Scanner in = new Scanner(System.in);private StudentService studentService = new StudentService();//開啟學生管理系統public void start() {lo:while (true) {System.out.println("---------歡迎來到湖南城市學院學生管理系統---------");System.out.println("請輸入您的選擇:1、添加學生 2、刪除學生 3、修改學生 4、查看學生 5、退出");String choice = in.next();switch (choice) {case "1": // System.out.println("添加學生");addStudent();break;case "2": // System.out.println("刪除學生");deleteStudentBySid();break;case "3": // System.out.println("修改學生");updateStudent();break;case "4": // System.out.println("查看學生");findAllStudent();break;case "5":System.out.println("感謝您使用湖南城市學院學生管理系統,再見!");break lo;default:System.out.println("您的輸入有誤,請重新輸入");break;}}}//添加學生public void addStudent() {//接受學生信息String sid;while (true) {System.out.println("請輸入學生學號:");sid = in.next();//判斷學號是否存在boolean flag = studentService.isExists(sid);//如果為true,重新輸入if (flag) {System.out.println("學號已經被占用,請重新輸入");} else {break;}}Student stu = getStudent(sid);//接受布爾值boolean result = studentService.addStudent(stu);//根據boolean值,在控制臺打印成功\失敗if (result) {System.out.println("添加成功");} else {System.out.println("添加失敗");}}//查看學生public void findAllStudent() {//接收數組Student[] arr = studentService.findAllStudent();if (arr != null) {//輸出表頭System.out.println("學號\t\t姓名\t年齡\t生日");//遍歷數組for (int i = 0; i < arr.length; i++) {Student stu = arr[i];//判斷是否為空,不然會報錯if (stu != null) {System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());}}} else {System.out.println("查無信息,請添加后重試");}}//刪除學生public void deleteStudentBySid() {//判斷Id是否存在String deleteid = CheckId();//傳送IdstudentService.deleteStudentBySid(deleteid);System.out.println("刪除成功");}//修改學生public void updateStudent() {//判斷即將修改的學生Id是否存在String updateid = CheckId();//接收新的對象Student newstu = getStudent(updateid);//傳遞新的對象studentService.updateStudent(newstu);//提示信息System.out.println("修改成功");}//封裝學生對象public Student getStudent(String sid) {System.out.println("請輸入姓名:");//接收新的名字String name = in.next();System.out.println("請輸入年齡:");//新的年齡String age = in.next();System.out.println("請輸入生日:");//新的生日String birthday = in.next();//封裝為一個新的對象Student stu = new Student(sid, name, age, birthday);return stu;}//錄入查找學生學號public String CheckId() {String Id;while (true) {System.out.println("請輸入學生學號:");//接收學號Id = in.next();boolean flag = studentService.isExists(Id);//如果存在跳出循環if (flag) {break;}//不存在則提示用戶else {System.out.println("學號不存在,請重新輸入");}}return Id;} }二、學生信息部分
public class Student {private String sid;private String name;private String age;private String birthday;public Student() {}public Student(String sid, String name, String age, String birthday) {this.sid = sid;this.name = name;this.age = age;this.birthday = birthday;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;} }三、學生信息存儲部分
public class StudentDao {//創建學生數組private static Student[] arr = new Student[5];//添加學生public boolean addStudent(Student stu) {int Index = -1;//遍歷數組,找空位for (int i = 0; i < arr.length; i++) {Student student = arr[i];if (student == null) {//如果找到空位,就讓Index記錄該索引值Index = i;break;}}if (Index == -1) {//數組內無空位了return false;} else {//存入學生對象arr[Index] = stu;return true;}}//查找學生public Student[] findAllStudent() {return arr;}//刪除學生public void deleteStudentBySid(String sid) {//找到位置int Index = getIndex(sid);//刪除arr[Index] = null;}//根據學號找位置public int getIndex(String sid) {//假設數組沒有這個學號int Index = -1;//遍歷數組for (int i = 0; i < arr.length; i++) {Student stu = arr[i];if (stu != null && stu.getSid().equals(sid)) {Index = i;break;}}return Index;}//修改學生public void updateStudent(Student stu) {//獲取被修改學生學號String sid = stu.getSid();//找到該學生的索引位置int Index = getIndex(sid);//添加新的學生對象進去arr[Index] = stu;} }四、代碼邏輯部分
public class StudentService {StudentDao studentDao = new StudentDao();// 學號是否存在public boolean isExists(String sid) {//創建變量flag,假設學號不在數組中boolean flag = false;//獲得學生數組Student[] arr = studentDao.findAllStudent();//遍歷數組,判斷數組中是否有相同學號for (int i = 0; i < arr.length; i++) {Student student = arr[i];if (student != null && student.getSid().equals(sid)) {flag = true;break;}}return flag;}//添加學生public boolean addStudent(Student stu) {//傳遞學生對象boolean result = studentDao.addStudent(stu);//返回布爾值給上一層return result;}// 查找學生public Student[] findAllStudent() {//接收數組Student[] arr = studentDao.findAllStudent();//判斷返回值時有用,假定數組為空boolean flag = false;//遍歷數組for (int i = 0; i < arr.length; i++) {Student stu = arr[i];//如果數組內不為空if (stu != null) {flag = true;break;}}if (flag) {//為真返回數組return arr;} else {return null;}}//刪除學生public void deleteStudentBySid(String deleteid) {//傳學號studentDao.deleteStudentBySid(deleteid);}// 修改學生public void updateStudent(Student stu) {//傳遞對象studentDao.updateStudent(stu);} }2、老師管理部分
一、與用戶溝通部分
public class TeacherController {Scanner in = new Scanner(System.in);TeacherService teacherService = new TeacherService();//開啟老師管理系統public void start() {lo:while (true) {System.out.println("---------歡迎來到湖南城市學院老師管理系統---------");System.out.println("請輸入您的選擇:1、添加老師 2、刪除老師 3、修改老師 4、查看老師 5、退出");//接收選擇String choice = in.next();//選擇路線switch (choice) {case "1": // System.out.println("添加老師");addTeacher();break;case "2": // System.out.println("刪除老師");deleteTeacher();break;case "3": // System.out.println("修改老師");updateTeacher();break;case "4": // System.out.println("查看老師");findAllTeacher();break;case "5":System.out.println("感謝您使用湖南城市學院老師管理系統,再見!");break lo;default:System.out.println("您輸入有誤,請重新輸入!");break;}}}//修改老師public void updateTeacher() {//獲得IdString updateId = CheckId();//獲得對象Teacher teacher = getTeacher(updateId);teacherService.upadateTeacher(teacher);System.out.println("修改成功");}//刪除老師public void deleteTeacher() {String deleteId;while (true) {System.out.println("請輸入工號:");deleteId = in.next();//接收布爾值,判斷工號是否存在數組中boolean flag = teacherService.isExist(deleteId);//如果存在,跳出刪除if (flag) {break;} else {System.out.println("查無此人,請重新輸入!");}}teacherService.deleteTeacher(deleteId);System.out.println("刪除成功");}//查找老師public void findAllTeacher() {//獲取數組地址Teacher[] arr = teacherService.findAllTeacher();//如果沒有做添加老師的步驟,那么數組的地址是不存在的if (arr != null) {System.out.println("工號\t\t姓名\t年齡\t生日");for (int i = 0; i < arr.length; i++) {Teacher teacher = arr[i];//要先判斷是否為空,不然會報錯if (teacher != null) {System.out.println(teacher.getTid() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());}}} else {System.out.println("查無信息,請添加后重試!");}}// 添加老師public void addTeacher() {String addId;while (true) {System.out.println("請輸入工號:");//接收Id,判斷Id是否存在addId = in.next();//接收返回值boolean flag = teacherService.isExist(addId);//返回值為true,工號不能兩個老師用if (flag) {System.out.println("工號被占用,請重新輸入");}//返回值為false則跳出循環,繼續完善其他信息else {break;}}//獲得對象Teacher teacher = getTeacher(addId);//將對象傳出boolean flag = teacherService.addTeacher(teacher);if (flag) {System.out.println("添加成功");} else {System.out.println("添加失敗");}}//查詢,判斷工號是否在數組內public String CheckId() {String Id;while (true) {System.out.println("請輸入工號:");Id = in.next();//接受布爾值,判斷工號是否在數組內boolean flag = teacherService.isExist(Id);//如果工號在數組內,跳出循環,修改其他信息if (flag) {break;} else {System.out.println("查無此人,請重新輸入!");}}return Id;}//信息錄入與封裝public Teacher getTeacher(String Id) {//完善其他信息System.out.println("請輸入姓名:");String name = in.next();System.out.println("請輸入年齡:");String age = in.next();System.out.println("請輸入生日:");String birthday = in.next();//封裝為對象Teacher teacher = new Teacher(Id, name, age, birthday);return teacher;} }二、老師信息
public class Teacher {private String tid;private String name;private String age;private String birthday;public Teacher() {}public Teacher(String tid, String name, String age, String birthday) {this.tid = tid;this.name = name;this.age = age;this.birthday = birthday;}public String getTid() {return tid;}public void setTid(String tid) {this.tid = tid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;} }三、老師信息存儲?
public class TeacherDao {//加static是為了保證此類的任何對象都在使用這個數組static Teacher[] arr = new Teacher[5];//傳遞數組,查看老師public Teacher[] findAllTeacher() {return arr;}//添加學生public boolean addTeacher(Teacher teacher) {//假設數組已經存滿int Index = -1;//遍歷數組,找空位,并且記錄空位的位置for (int i = 0; i < arr.length; i++) {Teacher t = arr[i];if (t == null) {Index = i;break;}}//數組如果滿了,就傳falseif (Index == -1) {return false;} else {//將傳過來的對象添加到空位中去arr[Index] = teacher;return true;}}//刪除學生public void deleteTeacher(String deleteId) {//根據傳來的學號找到相應的索引位置int Index = getIndex(deleteId);//將其刪除arr[Index] = null;}//根據工號找索引位置public int getIndex(String Id) {//先給索引賦初值,表示不在數組中int Index = -1;//遍歷數組,找到工號所對應的索引位置for (int i = 0; i < arr.length; i++) {Teacher teacher = arr[i];if (teacher != null && teacher.getTid().equals(Id)) {Index = i;}}return Index;}//修改老師public void updateTeacher(Teacher teacher) {//將要修改的IdString Id = teacher.getTid();//根據Id找到位置int Index = getIndex(Id);//將新的對象填入數組中arr[Index] = teacher;} }四、代碼邏輯部分
public class TeacherService {TeacherDao teacherDao = new TeacherDao();//判斷老師是否存在,根據工號查找老師public boolean isExist(String addId) {//先假設找不到老師boolean flag = false;//創建對象接收數組Teacher[] arr = teacherDao.findAllTeacher();//遍歷數組,找相同的學號for (int i = 0; i < arr.length; i++) {Teacher teacher = arr[i];if (teacher != null && teacher.getTid().equals(addId)) {flag = true;break;}}//找到了,返回值為trueif (flag) {return flag;}//找不到,返回值為falseelse {return flag;}}//添加老師public boolean addTeacher(Teacher teacher) {//接受布爾值,并且返回boolean flag = teacherDao.addTeacher(teacher);if (flag) {return flag;} else {return flag;}}//查找老師public Teacher[] findAllTeacher() {Teacher[] arr = teacherDao.findAllTeacher();//假設數組為空boolean flag = false;//遍歷數組,看數組是否為空for (int i = 0; i < arr.length; i++) {Teacher teacher = arr[i];if (teacher != null) {flag = true;break;}}//假如數組不為空,flag為trueif (flag) {return arr;} else {return null;}}//刪除老師public void deleteTeacher(String deleteId) {teacherDao.deleteTeacher(deleteId);}// 修改老師public void upadateTeacher(Teacher teacher) {teacherDao.updateTeacher(teacher);} }如有疑問,歡迎交流!?
總結
以上是生活随笔為你收集整理的xxx学校/学院/大学信息管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webrtc 的回声抵消(aec、aec
- 下一篇: 共享文件的原理