學生管理系統(java)

1.前期準備

(1)新建java項目

(2)新建java軟件包以及三個文件Student.java,Student.txt,StuSystem.java

Student.java
package student_management_system;public class Student {private String id;private String name;private int age;private String address;public Student(){}public Student(String id,String name,int age,String address){this.id=id;this.name=name;this.age=age;this.address=address;}public String getId() {return id;}public void setId(String id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
StuSystem.java
package student_management_system;import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;public class StuSystem {public static void saveStudentsToFile(ArrayList<Student> list,String file) { //保存文件
//        每次調用 saveStudentsToFile 方法時,都會覆蓋 Student.txt 文件中的現有內容。
//        如果你希望在文件末尾追加內容,可以在 FileWriter 構造函數中傳遞一個額外的 true 參數,以啟用追加模式。
//        new FileWriter("Student.txt", true)try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {writer.write(String.format("%-10s\t%-15s\t%-5s\t%-30s\n", "id", "姓名", "年齡", "家庭住址"));for (Student stu : list) {writer.write(String.format("%-10s\t%-15s\t%-5s\t%-30s\n", stu.getId(), stu.getName(), stu.getAge(), stu.getAddress()));}} catch (IOException e) {e.printStackTrace();}}public static ArrayList<Student> loadStudentsFromFile(String file) {ArrayList<Student> list = new ArrayList<>();try (BufferedReader reader = new BufferedReader(new FileReader(file))) {String line;// 跳過標題行reader.readLine();while ((line = reader.readLine()) != null) {// 使用正則表達式 \s+ 來匹配一個或多個空格作為分隔符String[] data = line.split("\\s+");if (data.length == 4) {Student student = new Student(data[0], data[1], Integer.parseInt(data[2]), data[3]);list.add(student);}}} catch (IOException e) {e.printStackTrace();}return list;}public static void main(String[] args) {// 打印當前的工作目錄System.out.println("Current working directory: " + System.getProperty("user.dir"));ArrayList<Student> list=new ArrayList<>();String file="./src/student_management_system/Student.txt";list=loadStudentsFromFile(file);boolean flag=true;while (flag) {System.out.println("------------歡迎來到學生管理系統------------");System.out.println("1:添加學生");System.out.println("2:刪除學生");System.out.println("3:修改學生");System.out.println("4:查詢某個學生");System.out.println("5:查詢全部學生");System.out.println("6:退出");System.out.println("請輸入您的選擇:");Scanner sc=new Scanner(System.in);String choose=sc.next();switch(choose){case "1" -> {System.out.println("添加學生");addStudent(list);break;}case "2" -> {System.out.println("刪除學生");deleteStudent(list);break;}case "3" -> {System.out.println("修改學生");updateStudent(list);break;}case "4" -> {System.out.println("查詢某個學生");findOneStudent(list);break;}case "5" -> {System.out.println("查詢全部學生");query_allStudent(list);break;}case "6" -> {System.out.println("退出");saveStudentsToFile(list,file); // 保存到文件flag=false;break;}default -> System.out.println("沒有這個選項");}}}public static void addStudent(ArrayList<Student> list){Scanner sc=new Scanner(System.in);String id= null;while (true) {System.out.println("請輸入學生的id");id = sc.next();int a=getIndex(list,id);if(a==-1) break;else System.out.println("這個學生的id已存在,請重新輸入!");}System.out.println("請輸入學生的name");String name=sc.next();System.out.println("請輸入學生的age");int age=sc.nextInt();System.out.println("請輸入學生的address");String address=sc.next();Student student=new Student(id,name,age,address);list.add(student);System.out.printf("學號為%s的學生已成功添加!\n",id);}public static void deleteStudent(ArrayList<Student> list){Scanner sc=new Scanner(System.in);while (true) {System.out.println("請輸入學生的id");String id=sc.next();int a=getIndex(list,id);if (a==-1) System.out.println("這個學生id不存在,請重新輸入!");else{list.remove(a);System.out.printf("學號為%s的學生已成功刪除!\n",id);break;}}}public static void updateStudent(ArrayList<Student> list){Scanner sc=new Scanner(System.in);while (true) {System.out.println("請輸入學生的id");String id=sc.next();int a=getIndex(list,id);if (a==-1) System.out.println("這個學生id不存在,請重新輸入!");else{System.out.println("請輸入學生的name");String name=sc.next();System.out.println("請輸入學生的age");int age=sc.nextInt();System.out.println("請輸入學生的address");String address=sc.next();Student student=new Student(id,name,age,address);list.set(a,student);System.out.printf("學號為%s的學生已成功修改!\n",id);break;}}}public static void findOneStudent(ArrayList<Student> list){Scanner sc = new Scanner(System.in);while (true) {System.out.println("請輸入學生的id");String id = sc.next();int a = getIndex(list, id);if (a == -1) {System.out.println("這個學生id不存在,請重新輸入!");} else {Student stu = list.get(a);
// %-5s 表示輸出一個字符串,并且這個字符串應該左對齊,至少占用5個字符的寬度。如果字符串長度小于5,那么右邊會用空格填充;System.out.printf("學號為%s的學生的信息已成功查詢到!\n", id);System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", "id", "姓名", "年齡", "家庭住址");System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", stu.getId(), stu.getName(), stu.getAge(), stu.getAddress());break;}}}public static void query_allStudent(ArrayList<Student> list){if(list.isEmpty()){System.out.println("當前無學生信息,請添加后再查詢!");return;}System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", "id", "姓名", "年齡", "家庭住址");for(int i = 0; i < list.size(); i++){Student stu = list.get(i);System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", stu.getId(), stu.getName(), stu.getAge(), stu.getAddress());}}public static int getIndex(ArrayList<Student> list,String id){for(int i=0;i<list.size();i++){Student student=list.get(i);String sid=student.getId();if(sid.equals(id)){return i;}}return -1;   //如果查找的學生id不存在時返回-1}}

2.StuSystem.java的函數

(1)主函數main()

public static void main(String[] args) {// 打印當前的工作目錄System.out.println("Current working directory: " + System.getProperty("user.dir"));ArrayList<Student> list=new ArrayList<>();String file="./src/student_management_system/Student.txt";list=loadStudentsFromFile(file);boolean flag=true;while (flag) {System.out.println("------------歡迎來到學生管理系統------------");System.out.println("1:添加學生");System.out.println("2:刪除學生");System.out.println("3:修改學生");System.out.println("4:查詢某個學生");System.out.println("5:查詢全部學生");System.out.println("6:退出");System.out.println("請輸入您的選擇:");Scanner sc=new Scanner(System.in);String choose=sc.next();switch(choose){case "1" -> {System.out.println("添加學生");addStudent(list);break;}case "2" -> {System.out.println("刪除學生");deleteStudent(list);break;}case "3" -> {System.out.println("修改學生");updateStudent(list);break;}case "4" -> {System.out.println("查詢某個學生");findOneStudent(list);break;}case "5" -> {System.out.println("查詢全部學生");query_allStudent(list);break;}case "6" -> {System.out.println("退出");saveStudentsToFile(list,file); // 保存到文件flag=false;break;}default -> System.out.println("沒有這個選項");}}}

(2)添加學生

public static void addStudent(ArrayList<Student> list){Scanner sc=new Scanner(System.in);String id= null;while (true) {System.out.println("請輸入學生的id");id = sc.next();int a=getIndex(list,id);if(a==-1) break;else System.out.println("這個學生的id已存在,請重新輸入!");}System.out.println("請輸入學生的name");String name=sc.next();System.out.println("請輸入學生的age");int age=sc.nextInt();System.out.println("請輸入學生的address");String address=sc.next();Student student=new Student(id,name,age,address);list.add(student);System.out.printf("學號為%s的學生已成功添加!\n",id);}

(3)刪除學生

public static void deleteStudent(ArrayList<Student> list){Scanner sc=new Scanner(System.in);while (true) {System.out.println("請輸入學生的id");String id=sc.next();int a=getIndex(list,id);if (a==-1) System.out.println("這個學生id不存在,請重新輸入!");else{list.remove(a);System.out.printf("學號為%s的學生已成功刪除!\n",id);break;}}}

(4)修改學生

public static void updateStudent(ArrayList<Student> list){Scanner sc=new Scanner(System.in);while (true) {System.out.println("請輸入學生的id");String id=sc.next();int a=getIndex(list,id);if (a==-1) System.out.println("這個學生id不存在,請重新輸入!");else{System.out.println("請輸入學生的name");String name=sc.next();System.out.println("請輸入學生的age");int age=sc.nextInt();System.out.println("請輸入學生的address");String address=sc.next();Student student=new Student(id,name,age,address);list.set(a,student);System.out.printf("學號為%s的學生已成功修改!\n",id);break;}}}

(5)查詢某個學生

public static void findOneStudent(ArrayList<Student> list){Scanner sc = new Scanner(System.in);while (true) {System.out.println("請輸入學生的id");String id = sc.next();int a = getIndex(list, id);if (a == -1) {System.out.println("這個學生id不存在,請重新輸入!");} else {Student stu = list.get(a);
// %-5s 表示輸出一個字符串,并且這個字符串應該左對齊,至少占用5個字符的寬度。如果字符串長度小于5,那么右邊會用空格填充;System.out.printf("學號為%s的學生的信息已成功查詢到!\n", id);System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", "id", "姓名", "年齡", "家庭住址");System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", stu.getId(), stu.getName(), stu.getAge(), stu.getAddress());break;}}}

(6)查詢全部學生

public static void query_allStudent(ArrayList<Student> list){if(list.isEmpty()){System.out.println("當前無學生信息,請添加后再查詢!");return;}System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", "id", "姓名", "年齡", "家庭住址");for(int i = 0; i < list.size(); i++){Student stu = list.get(i);System.out.printf("%-10s\t%-15s\t%-5s\t%-30s\n", stu.getId(), stu.getName(), stu.getAge(), stu.getAddress());}}

(7)根據學生id來查找學生信息在數組序列list中的索引

????????????????//如果查找的學生id不存在時返回-1
public static int getIndex(ArrayList<Student> list,String id){for(int i=0;i<list.size();i++){Student student=list.get(i);String sid=student.getId();if(sid.equals(id)){return i;}}return -1;   //如果查找的學生id不存在時返回-1}

(8)保存數據到文件(以覆蓋寫的方式)

public static void saveStudentsToFile(ArrayList<Student> list,String file) { //保存文件
//        每次調用 saveStudentsToFile 方法時,都會覆蓋 Student.txt 文件中的現有內容。
//        如果你希望在文件末尾追加內容,可以在 FileWriter 構造函數中傳遞一個額外的 true 參數,以啟用追加模式。
//        new FileWriter("Student.txt", true)try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {writer.write(String.format("%-10s\t%-15s\t%-5s\t%-30s\n", "id", "姓名", "年齡", "家庭住址"));for (Student stu : list) {writer.write(String.format("%-10s\t%-15s\t%-5s\t%-30s\n", stu.getId(), stu.getName(), stu.getAge(), stu.getAddress()));}} catch (IOException e) {e.printStackTrace();}}

(9)從文件讀取數據

public static ArrayList<Student> loadStudentsFromFile(String file) {ArrayList<Student> list = new ArrayList<>();try (BufferedReader reader = new BufferedReader(new FileReader(file))) {String line;// 跳過標題行reader.readLine();while ((line = reader.readLine()) != null) {// 使用正則表達式 \s+ 來匹配一個或多個空格作為分隔符String[] data = line.split("\\s+");if (data.length == 4) {Student student = new Student(data[0], data[1], Integer.parseInt(data[2]), data[3]);list.add(student);}}} catch (IOException e) {e.printStackTrace();}return list;}

3.運行測試

(1)添加學生

(2)刪除學生

?

?

(3)修改學生

?

4.補充筆記

(1)建議收藏的網站

網站:Java 教程 | 菜鳥教程

(2)ArrayList請問這個類型允許重復值嗎

在 Java 中,`ArrayList` 是一個允許重復元素的集合類型。它實現了 `List` 接口,可以包含重復的元素,并且沒有限制元素必須唯一。這意味著你可以在同一個 `ArrayList` 實例中多次添加同一個對象。

例如,如果你有一個 `ArrayList<Student>`,你可以添加多個 `Student` 對象,包括具有相同屬性的 `Student` 對象。這里是如何添加重復元素到 `ArrayList` 的一個簡單示例:

```java
ArrayList<Student> students = new ArrayList<>();
Student student1 = new Student("001", "Alice", 20, "123 Main St");
Student student2 = new Student("002", "Bob", 22, "456 Elm St");

// 添加學生到列表
students.add(student1);
students.add(student2);
students.add(student1); // 再次添加 student1,這將導致列表中有重復的元素

// 打印列表中的所有學生
for (Student student : students) {
? ? System.out.println(student);
}
```

在這個例子中,`student1` 被添加了兩次,所以在 `ArrayList` 中會有重復的元素。

如果你需要一個不允許重復元素的集合,你可以考慮使用 `Set` 接口的實現,如 `HashSet` 或 `LinkedHashSet`。這些集合類型不允許重復元素,如果嘗試添加一個已經存在的元素,它將被忽略。例如:

```java
Set<Student> studentsSet = new HashSet<>();
studentsSet.add(student1);
studentsSet.add(student2);
// studentsSet.add(student1); // 這將不會添加重復的 student1
```

請注意,使用 `Set` 時,元素的相等性是通過 `equals()` 方法來判斷的,所以你需要確保你的 `Student` 類正確地實現了 `equals()` 和 `hashCode()` 方法,以便 `Set` 能夠正確地識別重復的元素。
?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/62268.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/62268.shtml
英文地址,請注明出處:http://en.pswp.cn/web/62268.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JavaWeb學習(2)(Cookie原理(超詳細)、HTTP無狀態)

目錄 一、HTTP無狀態。 &#xff08;1&#xff09;"記住我"&#xff1f; &#xff08;2&#xff09;HTTP無狀態。 &#xff08;3&#xff09;信息存儲客戶端中。如何處理&#xff1f; 1、loaclStorage與sessionStorage。 2、Cookie。 二、Cookie。 &#xff08;1&…

SpringBoot教程(三十二) SpringBoot集成Skywalking鏈路跟蹤

SpringBoot教程&#xff08;三十二&#xff09; | SpringBoot集成Skywalking鏈路跟蹤 一、Skywalking是什么&#xff1f;二、Skywalking與JDK版本的對應關系三、Skywalking下載四、Skywalking 數據存儲五、Skywalking 的啟動六、部署探針 前提&#xff1a; Agents 8.9.0 放入 …

flask創建templates目錄存放html文件

首先&#xff0c;創建flask項目&#xff0c;在pycharm中File --> New Project&#xff0c;選擇Flask項目。 然后&#xff0c;在某一目錄下&#xff0c;新建名為templates的文件夾&#xff0c;這時會是一個普通的文件夾。 然后右擊templates文件夾&#xff0c;選擇Unmark as …

Java進階(注解,設計模式,對象克隆)

Java進階(注解&#xff0c;設計模式&#xff0c;對象克隆) 一. 注解 1.1 什么是注解 java中注解(Annotation)&#xff0c;又稱java標注&#xff0c;是一種特殊的注釋 可以添加在包&#xff0c;類&#xff0c;成員變量&#xff0c;方法&#xff0c;參數等內容上 注解會隨同…

部署loki,grafana 以及springcloud用法舉例

文章目錄 場景docker 部署grafanadocker-compose部署loki維護配置文件 local-config.yaml維護docker-compose.yml配置啟動 grafana 添加loki數據源springcloud用法舉例查看loki的explore,查看日志 場景 小公司缺少運維崗位&#xff0c;需要研發自己部署日志系統&#xff0c;elk…

keil報錯---connection refused due to device mismatch

解決辦法如下&#xff1a; 記得改成1 把Enable取消

第三節、電機定速轉動【51單片機-TB6600驅動器-步進電機教程】

摘要&#xff1a;本節介紹用定時器定時的方式&#xff0c;精準控制脈沖時間&#xff0c;從而控制步進電機速度 一、計算過程 1.1 電機每一步的角速度等于走這一步所花費的時間&#xff0c;走一步角度等于步距角&#xff0c;走一步的時間等于一個脈沖的時間 w s t e p t … ……

vue中pdf.js的使用,包括pdf顯示,跳轉指定頁面,高亮關鍵詞

目錄 一、下載pdf.js 二、引入到本地的項目中 三、實現預覽pdf 四、跳轉到指定頁面 五、利用pdf里面的find查找關鍵詞 六、修改頁面大小為實際大小 一、下載pdf.js https://github.com/mozilla/pdf.js 里面有很多的版本&#xff0c; 高版本的可能瀏覽器不兼容或者還要考…

OD B卷【連續字母長度】

題目 給定一個字符串&#xff0c;只包含大寫字母&#xff0c;求在包含同一字母的子串中&#xff0c;長度第k長的子串的長度&#xff0c;相同字母只取最長的那個子串。 輸入描述&#xff1a; 第一行輸入一個子串&#xff08;長【1,100】&#xff09;&#xff0c;只包含大寫字母…

python中的 Pydantic 框架介紹

Pydantic 框架介紹 Pydantic 是一個用于數據驗證和設置管理的 Python 庫。它主要通過數據模型類的定義來處理 JSON 數據、解析請求和響應數據&#xff0c;并提供自動化的驗證和轉換。Pydantic 主要用于處理 Python 類型的安全性和驗證&#xff0c;尤其在 FastAPI 等現代 Pytho…

橋接模式和組合模式的區別

橋接模式&#xff08;Bridge Pattern&#xff09;和組合模式&#xff08;Composite Pattern&#xff09;都是結構型設計模式&#xff0c;旨在解決對象結構的復雜性問題&#xff0c;但它們的應用場景和目的有所不同。以下是它們的區別&#xff1a; 1. 定義與目的 橋接模式&…

Qt 小項目 學生管理信息系統

主要是對數據庫的增刪查改的操作 登錄/注冊界面&#xff1a; 主頁面&#xff1a; 添加信息&#xff1a; 刪除信息&#xff1a; 刪除第一行&#xff08;支持多行刪除&#xff09; 需求分析&#xff1a; 用QT實現一個學生管理信息系統&#xff0c;數據庫為MySQL 要求&#xf…

14.數據容器-set集合

特點 無序的&#xff0c;元素不重復&#xff0c;自帶去重功能。 可以容納不同類型的元素數據。 # 定義一個空set my_set {} your_set set() my_set {aa, bb, bb, aa} # {aa, bb} print(my_set) 因為set集合是無序的&#xff0c;所以集合不支持下標索引訪問。所以set集合…

“量子躍遷與數據織網:深入探索K最近鄰算法在高維空間中的優化路徑、神經網絡融合技術及未來機器學習生態系統的構建“

&#x1f3bc;個人主頁&#xff1a;【Y小夜】 &#x1f60e;作者簡介&#xff1a;一位雙非學校的大二學生&#xff0c;編程愛好者&#xff0c; 專注于基礎和實戰分享&#xff0c;歡迎私信咨詢&#xff01; &#x1f386;入門專欄&#xff1a;&#x1f387;【MySQL&#xff0…

硬件選型規則

光源選型: 先用型號中帶H的&#xff0c;沒有的選標準的. 光源和光源控制器的搭配需要確保接口一致。 根據型號表中的最佳工作距離和相機的尺寸。 光源控制器選型&#xff1a; 首先選擇海康風格系列光源控制器考慮與光源的接口匹配。功率應該滿足接近光源功率。檢查是否退市…

【QNX+Android虛擬化方案】135 - QNX側如何Dump 88Q5152 MIBS報文計數

【QNX+Android虛擬化方案】135 - QNX側如何Dump 88Q5152 MIBS報文計數 一、讀取 88Q5152 MIBS 計數二、讀取 88Q5152 WDT 相關寄存器基于原生純凈代碼,自學總結 純技術分享,不會也不敢涉項目、不泄密、不傳播代碼文檔!!! 本文禁止轉載分享 !!! 匯總鏈接:《【QNX+Andro…

C#核心(15)繼承中的構造函數

前言 我們之前學過構造函數是什么東西&#xff0c;今天的內容也和構造函數緊密相關&#xff0c;一個繼承了父親的子類里面構造函數的規則是什么樣的&#xff0c;今天內容很簡單&#xff0c;請聽我慢慢講來。 基本概念 特點&#xff1a;當申明一個子類時&#xff0c;先執行父…

TVbox源貢獻指南

歡迎各路大佬踴躍提PR&#xff0c;分享爬蟲代碼。 源碼倉庫地址 https://github.com/lushunming/AndroidCatVodSpider 快速開始 本工程是一個完整的AndroidStudio工程&#xff0c;請你用AS打開編輯。 工程調試完畢后要需要導出生成jar文件配合軟件使用&#xff0c;執行根目…

FastAPI快速入門

文章目錄 了解FastAPI程序結構第一步&#xff0c;導入FastAPI第二步&#xff0c;創建一個app實例第三步&#xff0c;編寫一個 路徑操作裝飾器第五步、運行開發服務器uvicorn main:app --reload即可訪問api鏈接。符案例 聲明路徑參數聲明路徑參數的類型get請求查詢參數請求體如何…

云計算.運維.面試題

1、計算機能直接識別的語言( C )。 A、匯編語言 B、自然語言 C、機器語言 D、高級語言 2、應用軟件是指( D )。 A、所有能夠使用的軟件 B、能被各應用單位共同使用的某種軟件 C、所有計算機上都應使用的基本軟件D、專門為某一應用目的而編制的軟件 3、計算機的顯示器是一…