Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(IO版)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
圖解:

cade:
student.java
/*
?* 這是我的學(xué)生類
?*/
public class Student {
?? ?//學(xué)號(hào)
?? ?private String id;
?? ?//姓名
?? ?private String name;
?? ?//年齡
?? ?private String age;
?? ?//居住地
?? ?private String address;
?? ?
?? ?public Student() {
?? ??? ?
?? ?}
?
?? ?public Student(String id, String name, String 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 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 getAddress() {
?? ??? ?return address;
?? ?}
?
?? ?public void setAddress(String address) {
?? ??? ?this.address = address;
?? ?}
?? ?
}studentmangager類
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
?
/*
?* 這是我的學(xué)生管理系統(tǒng)的主類
?*?
?* 步驟如下:
?* A:定義學(xué)生類
?* B:學(xué)生管理系統(tǒng)的主界面的代碼編寫(xiě)
?* C:學(xué)生管理系統(tǒng)的查看所有學(xué)生的代碼編寫(xiě)
?* D:學(xué)生管理系統(tǒng)的添加學(xué)生的代碼編寫(xiě)
?* E:學(xué)生管理系統(tǒng)的刪除學(xué)生的代碼編寫(xiě)
?* F:學(xué)生管理系統(tǒng)的修改學(xué)生的代碼編寫(xiě)
?*/
public class StudentManagerTest {
?? ?public static void main(String[] args) throws IOException{
?? ??? ?//定義文件路徑
?? ??? ?String fileName = "students.txt";
?? ??? ?
?? ??? ?//為了讓程序能夠回到這里來(lái),我們使用循環(huán)
?? ??? ?while(true) {
?? ??? ??? ?//這是學(xué)生管理系統(tǒng)的主界面
?? ??? ??? ?System.out.println("--------歡迎來(lái)到學(xué)生管理系統(tǒng)--------");
?? ??? ??? ?System.out.println("1 查看所有學(xué)生");
?? ??? ??? ?System.out.println("2 添加學(xué)生");
?? ??? ??? ?System.out.println("3 刪除學(xué)生");
?? ??? ??? ?System.out.println("4 修改學(xué)生");
?? ??? ??? ?System.out.println("5 退出");
?? ??? ??? ?System.out.println("請(qǐng)輸入你的選擇:");
?? ??? ??? ?//創(chuàng)建鍵盤(pán)錄入對(duì)象
?? ??? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ??? ?String choiceString = sc.nextLine();
?? ??? ??? ?//用switch語(yǔ)句實(shí)現(xiàn)選擇
?? ??? ??? ?switch(choiceString) {
?? ??? ??? ?case "1":
?? ??? ??? ??? ?//查看所有學(xué)生
?? ??? ??? ??? ?findAllStudent(fileName);
?? ??? ??? ??? ?break;
?? ??? ??? ?case "2":
?? ??? ??? ??? ?//添加學(xué)生
?? ??? ??? ??? ?addStudent(fileName);
?? ??? ??? ??? ?break;
?? ??? ??? ?case "3":
?? ??? ??? ??? ?//刪除學(xué)生
?? ??? ??? ??? ?deleteStudent(fileName);
?? ??? ??? ??? ?break;
?? ??? ??? ?case "4":
?? ??? ??? ??? ?//修改學(xué)生
?? ??? ??? ??? ?updateStudent(fileName);
?? ??? ??? ??? ?break;
?? ??? ??? ?case "5":
?? ??? ??? ?default:
?? ??? ??? ??? ?System.out.println("謝謝你的使用");
?? ??? ??? ??? ?System.exit(0); //JVM退出
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?//從文件中讀數(shù)據(jù)到集合
?? ?public static void readData(String fileName,ArrayList<Student> array) throws IOException {
?? ??? ?//創(chuàng)建輸入緩沖流對(duì)象
?? ??? ?BufferedReader br = new BufferedReader(new FileReader(fileName));
?? ??? ?
?? ??? ?String line;
?? ??? ?while((line=br.readLine())!=null) {
?? ??? ??? ?String[] datas = line.split(",");
?? ??? ??? ?Student s = new Student();
?? ??? ??? ?s.setId(datas[0]);
?? ??? ??? ?s.setName(datas[1]);
?? ??? ??? ?s.setAge(datas[2]);
?? ??? ??? ?s.setAddress(datas[3]);
?? ??? ??? ?array.add(s);
?? ??? ?}
?? ??? ?
?? ??? ?br.close();
?? ?}
?? ?
?? ?//把集合中的數(shù)據(jù)寫(xiě)入文件
?? ?public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
?? ??? ?//創(chuàng)建輸出緩沖流對(duì)象
?? ??? ?BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
?? ??? ?
?? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ?Student s = array.get(x);
?? ??? ??? ?StringBuilder sb = new StringBuilder();
?? ??? ??? ?sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
?? ??? ??? ?
?? ??? ??? ?bw.write(sb.toString());
?? ??? ??? ?bw.newLine();
?? ??? ??? ?bw.flush();
?? ??? ?}
?? ??? ?
?? ??? ?bw.close();
?? ?}
?? ?
?? ?//修改學(xué)生
?? ?public static void updateStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對(duì)象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ?
?? ??? ?//修改學(xué)生的思路:鍵盤(pán)錄入一個(gè)學(xué)號(hào),到集合中去查找,看是否有學(xué)生使用的是該學(xué)號(hào),如果有就修改該學(xué)生
?? ??? ?//創(chuàng)建鍵盤(pán)錄入對(duì)象
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?System.out.println("請(qǐng)輸入你要修改的學(xué)生的學(xué)號(hào):");
?? ??? ?String id = sc.nextLine();
?? ??? ?
?? ??? ?//定義一個(gè)索引
?? ??? ?int index = -1;
?? ??? ?
?? ??? ?//遍歷集合
?? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ?//獲取每一個(gè)學(xué)生對(duì)象
?? ??? ??? ?Student s = array.get(x);
?? ??? ??? ?//拿學(xué)生對(duì)象的學(xué)號(hào)和鍵盤(pán)錄入的學(xué)號(hào)進(jìn)行比較
?? ??? ??? ?if(s.getId().equals(id)) {
?? ??? ??? ??? ?index = x;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(index == -1) {
?? ??? ??? ?System.out.println("不好意思,你要修改的學(xué)號(hào)對(duì)應(yīng)的學(xué)生信息不存在,請(qǐng)回去重新你的選擇");
?? ??? ?}else {
?? ??? ??? ?System.out.println("請(qǐng)輸入學(xué)生新姓名:");
?? ??? ??? ?String name = sc.nextLine();
?? ??? ??? ?System.out.println("請(qǐng)輸入學(xué)生新年齡:");
?? ??? ??? ?String age = sc.nextLine();
?? ??? ??? ?System.out.println("請(qǐng)輸入學(xué)生新居住地:");
?? ??? ??? ?String address = sc.nextLine();
?? ??? ??? ?
?? ??? ??? ?//創(chuàng)建學(xué)生對(duì)象
?? ??? ??? ?Student s = new Student();
?? ??? ??? ?s.setId(id);
?? ??? ??? ?s.setName(name);
?? ??? ??? ?s.setAge(age);
?? ??? ??? ?s.setAddress(address);
?? ??? ??? ?
?? ??? ??? ?//修改集合中的學(xué)生對(duì)象
?? ??? ??? ?array.set(index, s);
?? ??? ??? ?//把集合中的數(shù)據(jù)重新寫(xiě)回到文件
?? ??? ??? ?writeData(fileName, array);
?? ??? ??? ?//給出提示
?? ??? ??? ?System.out.println("修改學(xué)生成功");
?? ??? ?}
?? ?}
?? ?
?? ?//刪除學(xué)生
?? ?public static void deleteStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對(duì)象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ?
?? ??? ?//刪除學(xué)生的思路:鍵盤(pán)錄入一個(gè)學(xué)號(hào),到集合中去查找,看是否有學(xué)生使用的是該學(xué)號(hào),如果有就刪除該學(xué)生
?? ??? ?//創(chuàng)建鍵盤(pán)錄入對(duì)象
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?System.out.println("請(qǐng)輸入你要?jiǎng)h除的學(xué)生的學(xué)號(hào):");
?? ??? ?String id = sc.nextLine();
?? ??? ?
?? ??? ?//我們必須給出學(xué)號(hào)不存在的時(shí)候的提示
?? ??? ?
?? ??? ?//定義一個(gè)索引
?? ??? ?int index = -1;
?? ??? ?
?? ??? ?//遍歷集合
?? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ?//獲取到每一個(gè)學(xué)生對(duì)象
?? ??? ??? ?Student s = array.get(x);
?? ??? ??? ?//拿這個(gè)學(xué)生對(duì)象的學(xué)號(hào)和鍵盤(pán)錄入的學(xué)號(hào)進(jìn)行比較
?? ??? ??? ?if(s.getId().equals(id)) {
?? ??? ??? ??? ?index = x;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(index == -1) {
?? ??? ??? ?System.out.println("不好意思,你要?jiǎng)h除的學(xué)號(hào)對(duì)應(yīng)的學(xué)生信息不存在,請(qǐng)回去重新你的選擇");
?? ??? ?}else {
?? ??? ??? ?array.remove(index);
?? ??? ??? ?//把集合中的數(shù)據(jù)重新寫(xiě)回到文件
?? ??? ??? ?writeData(fileName, array);
?? ??? ??? ?System.out.println("刪除學(xué)生成功");
?? ??? ?}
?? ??? ?
?? ?}
?? ?
?? ?//添加學(xué)生
?? ?public static void addStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對(duì)象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ??? ??? ?
?? ??? ?//創(chuàng)建鍵盤(pán)錄入對(duì)象
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?
?? ??? ?//為了讓id能夠被訪問(wèn)到,我們就把id定義在了循環(huán)的外面
?? ??? ?String id;
?? ??? ?
?? ??? ?//為了讓代碼能夠回到這里,用循環(huán)
?? ??? ?while(true) {
?? ??? ??? ?System.out.println("請(qǐng)輸入學(xué)生學(xué)號(hào):");
?? ??? ??? ?//String id = sc.nextLine();
?? ??? ??? ?id = sc.nextLine();
?? ??? ??? ?
?? ??? ??? ?//判斷學(xué)號(hào)有沒(méi)有被人占用
?? ??? ??? ?//定義標(biāo)記
?? ??? ??? ?boolean flag = false;
?? ??? ??? ?//遍歷集合,得到每一個(gè)學(xué)生
?? ??? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ??? ?Student s = array.get(x);
?? ??? ??? ??? ?//獲取該學(xué)生的學(xué)號(hào),和鍵盤(pán)錄入的學(xué)號(hào)進(jìn)行比較
?? ??? ??? ??? ?if(s.getId().equals(id)) {
?? ??? ??? ??? ??? ?flag = true; //說(shuō)明學(xué)號(hào)被占用了
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?if(flag) {
?? ??? ??? ??? ?System.out.println("你輸入的學(xué)號(hào)已經(jīng)被占用,請(qǐng)重新輸入");
?? ??? ??? ?}else {
?? ??? ??? ??? ?break; //結(jié)束循環(huán)
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?System.out.println("請(qǐng)輸入學(xué)生姓名:");
?? ??? ?String name = sc.nextLine();
?? ??? ?System.out.println("請(qǐng)輸入學(xué)生年齡:");
?? ??? ?String age = sc.nextLine();
?? ??? ?System.out.println("請(qǐng)輸入學(xué)生居住地:");
?? ??? ?String address = sc.nextLine();
?? ??? ?
?? ??? ?//創(chuàng)建學(xué)生對(duì)象
?? ??? ?Student s = new Student();
?? ??? ?s.setId(id);
?? ??? ?s.setName(name);
?? ??? ?s.setAge(age);
?? ??? ?s.setAddress(address);
?? ??? ?
?? ??? ?//把學(xué)生對(duì)象作為元素添加到集合
?? ??? ?array.add(s);
?? ??? ?//把集合中的數(shù)據(jù)重新寫(xiě)回到文件
?? ??? ?writeData(fileName, array);
?? ??? ?
?? ??? ?//給出提示
?? ??? ?System.out.println("添加學(xué)生成功");
?? ?}
?? ?
?? ?//查看所有學(xué)生
?? ?public static void findAllStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對(duì)象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ?
?? ??? ?//首先來(lái)判斷集合中是否有數(shù)據(jù),如果沒(méi)有數(shù)據(jù),就給出提示,并讓該方法不繼續(xù)往下執(zhí)行
?? ??? ?if(array.size() == 0) {
?? ??? ??? ?System.out.println("不好意思,目前沒(méi)有學(xué)生信息可供查詢,請(qǐng)回去重新選擇你的操作");
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?
?? ??? ?//\t 其實(shí)就是一個(gè)tab鍵的位置
?? ??? ?System.out.println("學(xué)號(hào)\t\t姓名\t年齡\t居住地");
?? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ?Student s = array.get(x);
?? ??? ??? ?System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
?? ??? ?}
?? ?}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(控制臺(tái)版本)
- Java版學(xué)生管理系統(tǒng)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之學(xué)生管理系統(tǒng)的實(shí)現(xiàn)
- java控制臺(tái)實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- JavaSwing實(shí)現(xiàn)小型學(xué)生管理系統(tǒng)
- java實(shí)現(xiàn)簡(jiǎn)單的學(xué)生管理系統(tǒng)
- Java 實(shí)現(xiàn)完整功能的學(xué)生管理系統(tǒng)實(shí)例
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程
相關(guān)文章
Java刷題之最小k個(gè)數(shù)的思路及具體實(shí)現(xiàn)
這篇文章主要介紹了Java刷題之最小k個(gè)數(shù)的思路及具體實(shí)現(xiàn),最小K個(gè)數(shù)是一個(gè)經(jīng)典的top-K問(wèn)題,可以通過(guò)整體排序、建立小根堆或大根堆的方式解決,排序方式時(shí)間復(fù)雜度較高,適合數(shù)據(jù)量小的場(chǎng)景,小根堆適合k較小的情況,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
詳解Spring-boot中讀取config配置文件的兩種方式
這篇文章主要介紹了詳解Spring-boot中讀取config配置文件的兩種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Java調(diào)用瀏覽器打開(kāi)網(wǎng)頁(yè)完整實(shí)例
這篇文章主要介紹了Java調(diào)用瀏覽器打開(kāi)網(wǎng)頁(yè)的方法,以完整實(shí)例形式分析了java打開(kāi)網(wǎng)頁(yè)的相關(guān)技巧,需要的朋友可以參考下2015-05-05
淺談SpringCloud的微服務(wù)架構(gòu)組件
這篇文章主要介紹了淺談SpringCloud的微服務(wù)架構(gòu)組件,Spring Cloud根據(jù)分布式服務(wù)協(xié)調(diào)治理的需求成立了許多子項(xiàng)目,每個(gè)項(xiàng)目通過(guò)特定的組件去實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
SpringBoot通過(guò)token實(shí)現(xiàn)用戶互踢功能(具體實(shí)現(xiàn))
所謂token,既用戶能夠在一定時(shí)間內(nèi)證明自己身份的一長(zhǎng)串字符串,這篇文章主要介紹了SpringBoot通過(guò)token實(shí)現(xiàn)用戶互踢功能,需要的朋友可以參考下2024-04-04

