用Java实现学生管理系统【简化版】基础
?🎉博客首頁:痛而不言笑而不語的淺傷
📢歡迎關注🥳點贊 👍 收藏 ?留言 📝 歡迎討論!
🔮本文由痛而不言笑而不語的淺傷原創,CSDN首發!
🌋系列專欄:《Java每日一練》
🧿首發時間:2022年6月4日
?:熱愛Java學習,期待一起交流!
🙏🏻作者水平有限,如果發現錯誤,求告知,多謝!
🥰有問題可以私信交流!!!
目錄
運行展示
界面初始化
查看所有學生
?添加學生
刪除學生
修改學生
退出系統
?完整源代碼?
?
?* 標準的IO版學生管理系統
?*?
?* * 分析:
?* ?? ??? ?1.定義學生類
?* ?? ??? ?2.學生管理系統主界面的代碼編寫
?* ?? ??? ?3.學生管理系統的查看所有學生的代碼編寫
?* ?? ??? ?4.學生管理系統的添加學生的代碼編寫
?* ?? ??? ?5.學生管理系統的刪除學生的代碼編寫
?* ?? ??? ?6.學生管理系統的修改學生的代碼編寫
????????其實挺簡單的,就最基本的Java基礎語法部分。很適合初學者練習。其實主要從Java面向對象和IO流的一個熟練掌握。最后是轉換成jar文件,通過軟件弄成了.exe的文件。執行exe文件就是如下運行結果。數據的保存是在同目錄下的TXT文件,需要注意的是TXT文件和可執行的exe文件一定要放在同一文件夾內,不然會報錯。
運行展示
界面初始化
public class StudentManagerTest_IO {public static void main(String[] args) throws Exception {File f1 = new File("?G:\\學生管理系統\\Student1.txt");// 創建持久相對路勁String findName = f1.getName();// 學生管理系統主界面的代碼編寫// 死循環執行程序while (true) {System.out.println("----------歡迎使用老馬教育學生管理系統----------");System.out.println("請輸入你要執行的操作:");System.out.println("1 查看所有學生");System.out.println("2 添加學生");System.out.println("3 刪除學生");System.out.println("4 修改學生");System.out.println("5 退出系統");查看所有學生
// 學生管理系統的查看所有學生的代碼編寫public static void findAllStudent(String findName) throws Exception {// 創建集合對象存儲學生數據ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 遍歷集合到輸出控制臺// 首先判斷集合中是否有數據if (array.size() == 0) {System.out.println("不好意思,目前沒有學生信息可供查詢,請重新你的選擇!");} else {System.out.println("所有學生信息如下:");System.out.println("|-------|-------|-------|-------|");System.out.println("|" + "學號" + "\t" + "|" + "姓名" + "\t" + "|" + "年齡" + "\t" + "|" + "地址" + "\t" + "|");System.out.println("|-------|-------|-------|-------|");for (int i = 0; i < array.size(); i++) {Student s = array.get(i);System.out.println("|" + s.getId() + "\t" + "|" + s.getName() + "\t" + "|" + s.getAges() + "\t" + "|"+ s.getAddress() + "\t" + "|");System.out.println("|-------|-------|-------|-------|");}System.out.println();System.out.println();}}?
?添加學生
// 學生管理系統的添加學生的代碼編寫public static void addStudent(String findName) throws Exception {// 創建存儲數據的集合對象ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);String id;// 判斷該學號是否已被占用while (true) {// 定義標記boolean flag = false;// 添加學號System.out.println("請您輸入要添加的學生學號:");id = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getId().equals(id)) {flag = true;break;}}if (flag) {System.out.println("不好意思,你輸入的學號已被占用,請你重新你的選擇:");break;} else {// 添加姓名System.out.println("請您輸入學生姓名:");String name = sc.nextLine();System.out.println("請您輸入學生年齡:");String ages = sc.nextLine();System.out.println("請您輸入學生居住地址:");String address = sc.nextLine();// 創建學生對象Student s = new Student();s.setId(id);s.setName(name);s.setAges(ages);s.setAddress(address);// 把學生對象作為元素添加到集合中array.add(s);// 調用寫數據方法writerData(findName, array);// 添加成功的提示語System.out.println("添加學生信息成功!");break;}}}?
刪除學生
// 學生管理系統的刪除學生的代碼編寫public static void deleteStudent(String findName) throws Exception {// 創建學生數據存儲的空集合ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);// 定義標記int index = -1;// 定義學號變量String id;// 刪除學生信息// 數據要刪除學生信息的學生學號System.out.println("請您輸入要刪除學生信息的學生學號:");id = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);// 判斷是否有要刪除的這個學號if (s.getId().equalsIgnoreCase(id)) {index = i;break;}}if (index == -1) {System.out.println("您想要刪除的學生信息不存在,請重新您的選擇!");} else {array.remove(index);// 調用寫文件方法writerData(findName, array);// 提示語System.out.println("刪除學生信息成功!");}}?
修改學生
// 學生管理系統的修改學生的代碼編寫public static void alterStudent(String findName) throws Exception {// 創建存儲學生數據的空集合ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);// 定義標記int index = -1;// 定義學生學號變量String id;// 輸入學號判斷while (true) {// 輸入學號System.out.println("請您輸入想要修改學生信息的學號:");id = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getId().equals(id)) {index = i;}}if (index == -1) {System.out.println("您想要修改的學生信息不存在,請您重新輸入學號:");} else {// 修改姓名System.out.println("請您輸入學生姓名:");String name = sc.nextLine();// 修改年齡System.out.println("請您輸入學生年齡:");String ages = sc.nextLine();// 修改居住地址System.out.println("請您輸入居住地址:");String address = sc.nextLine();// 創建學生對象Student s = new Student();s.setId(id);s.setName(name);s.setAges(ages);s.setAddress(address);// 添加到集合array.set(index, s);// 調用寫文件方法writerData(findName, array);// 提示語System.out.println("修改學生信息成功!");break;}}} }退出系統
// 退出系統System.out.println("感謝您的使用,辛苦了!");System.exit(0);break;?完整源代碼?
package com.laoma_02;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner;/** 標準的IO版學生管理系統* * * 分析:* 1.定義學生類* 2.學生管理系統主界面的代碼編寫* 3.學生管理系統的查看所有學生的代碼編寫* 4.學生管理系統的添加學生的代碼編寫* 5.學生管理系統的刪除學生的代碼編寫* 6.學生管理系統的修改學生的代碼編寫** */ public class StudentManagerTest_IO {public static void main(String[] args) throws Exception {File f1 = new File("?G:\\學生管理系統\\Student1.txt");// 創建持久相對路勁String findName = f1.getName();// 學生管理系統主界面的代碼編寫// 死循環執行程序while (true) {System.out.println("----------歡迎使用老馬教育學生管理系統----------");System.out.println("請輸入你要執行的操作:");System.out.println("1 查看所有學生");System.out.println("2 添加學生");System.out.println("3 刪除學生");System.out.println("4 修改學生");System.out.println("5 退出系統");// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);// 輸入所匹配的序號并執行操作String option = sc.nextLine();switch (option) {case "1":// 查看所有學生信息findAllStudent(findName);break;case "2":// 添加學生信息addStudent(findName);break;case "3":// 刪除學生信息deleteStudent(findName);break;case "4":// 修改學生信息alterStudent(findName);break;case "5":// 退出系統System.out.println("感謝您的使用,辛苦了!");System.exit(0);break;default:System.out.println("您輸入的選擇不存在,請您重新輸入!");break;}}}// 把文件中的數據讀到集合中public static void readData(String findName, ArrayList<Student> array) throws Exception {// 創建輸入緩沖流對象BufferedReader br = new BufferedReader(new FileReader(findName));// 創建數組讀取文本文件數據并按照執行格式分割,然后把讀取的數據作為元素存儲到集合,最后遍歷集合到輸出控制臺// 定義索引String line;while ((line = br.readLine()) != null) {// 讀取文本文件數據并按照執行格式分割String[] strArray = line.split(",");// 創建學生對象Student s = new Student();s.setId(strArray[0]);s.setName(strArray[1]);s.setAges(strArray[2]);s.setAddress(strArray[3]);// 把讀取的數據作為元素存儲到集合array.add(s);}// 釋放資源你br.close();}// 把集合中的數據寫入文件中public static void writerData(String findName, ArrayList<Student> array) throws Exception {// 創建輸出緩沖流對象BufferedWriter bw = new BufferedWriter(new FileWriter(findName));for (int i = 0; i < array.size(); i++) {Student s = array.get(i);StringBuilder sb = new StringBuilder();sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAges()).append(",").append(s.getAddress());bw.write(sb.toString());bw.newLine();bw.flush();}// 釋放資源bw.close();}// 學生管理系統的查看所有學生的代碼編寫public static void findAllStudent(String findName) throws Exception {// 創建集合對象存儲學生數據ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 遍歷集合到輸出控制臺// 首先判斷集合中是否有數據if (array.size() == 0) {System.out.println("不好意思,目前沒有學生信息可供查詢,請重新你的選擇!");} else {System.out.println("所有學生信息如下:");System.out.println("|-------|-------|-------|-------|");System.out.println("|" + "學號" + "\t" + "|" + "姓名" + "\t" + "|" + "年齡" + "\t" + "|" + "地址" + "\t" + "|");System.out.println("|-------|-------|-------|-------|");for (int i = 0; i < array.size(); i++) {Student s = array.get(i);System.out.println("|" + s.getId() + "\t" + "|" + s.getName() + "\t" + "|" + s.getAges() + "\t" + "|"+ s.getAddress() + "\t" + "|");System.out.println("|-------|-------|-------|-------|");}System.out.println();System.out.println();}}// 學生管理系統的添加學生的代碼編寫public static void addStudent(String findName) throws Exception {// 創建存儲數據的集合對象ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);String id;// 判斷該學號是否已被占用while (true) {// 定義標記boolean flag = false;// 添加學號System.out.println("請您輸入要添加的學生學號:");id = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getId().equals(id)) {flag = true;break;}}if (flag) {System.out.println("不好意思,你輸入的學號已被占用,請你重新你的選擇:");break;} else {// 添加姓名System.out.println("請您輸入學生姓名:");String name = sc.nextLine();System.out.println("請您輸入學生年齡:");String ages = sc.nextLine();System.out.println("請您輸入學生居住地址:");String address = sc.nextLine();// 創建學生對象Student s = new Student();s.setId(id);s.setName(name);s.setAges(ages);s.setAddress(address);// 把學生對象作為元素添加到集合中array.add(s);// 調用寫數據方法writerData(findName, array);// 添加成功的提示語System.out.println("添加學生信息成功!");break;}}}// 學生管理系統的刪除學生的代碼編寫public static void deleteStudent(String findName) throws Exception {// 創建學生數據存儲的空集合ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);// 定義標記int index = -1;// 定義學號變量String id;// 刪除學生信息// 數據要刪除學生信息的學生學號System.out.println("請您輸入要刪除學生信息的學生學號:");id = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);// 判斷是否有要刪除的這個學號if (s.getId().equalsIgnoreCase(id)) {index = i;break;}}if (index == -1) {System.out.println("您想要刪除的學生信息不存在,請重新您的選擇!");} else {array.remove(index);// 調用寫文件方法writerData(findName, array);// 提示語System.out.println("刪除學生信息成功!");}}// 學生管理系統的修改學生的代碼編寫public static void alterStudent(String findName) throws Exception {// 創建存儲學生數據的空集合ArrayList<Student> array = new ArrayList<Student>();// 調用讀數據方法readData(findName, array);// 創建鍵盤錄入對象Scanner sc = new Scanner(System.in);// 定義標記int index = -1;// 定義學生學號變量String id;// 輸入學號判斷while (true) {// 輸入學號System.out.println("請您輸入想要修改學生信息的學號:");id = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getId().equals(id)) {index = i;}}if (index == -1) {System.out.println("您想要修改的學生信息不存在,請您重新輸入學號:");} else {// 修改姓名System.out.println("請您輸入學生姓名:");String name = sc.nextLine();// 修改年齡System.out.println("請您輸入學生年齡:");String ages = sc.nextLine();// 修改居住地址System.out.println("請您輸入居住地址:");String address = sc.nextLine();// 創建學生對象Student s = new Student();s.setId(id);s.setName(name);s.setAges(ages);s.setAddress(address);// 添加到集合array.set(index, s);// 調用寫文件方法writerData(findName, array);// 提示語System.out.println("修改學生信息成功!");break;}}} }最后呢?文章到這里就結束啦,你們學廢了嗎?
?
好啦!今天的練習就到這里。看吧這么努力的你又學到了很多,新的一天加油鴨!!!
你的點贊是對我最大的鼓勵。
你的收藏是對我文章的認可。
你的關注是對我創作的動力。
?
?
總結
以上是生活随笔為你收集整理的用Java实现学生管理系统【简化版】基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入react技术栈(3):React组
- 下一篇: [react] 什么是React的实例?