亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現(xiàn)學(xué)生管理系統(tǒng)(IO版)

 更新時間:2022年02月24日 17:07:36   作者:_Rick  
這篇文章主要為大家詳細介紹了Java實現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

圖解: 

cade: 

student.java

/*
?* 這是我的學(xué)生類
?*/
public class Student {
?? ?//學(xué)號
?? ?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)的主界面的代碼編寫
?* C:學(xué)生管理系統(tǒng)的查看所有學(xué)生的代碼編寫
?* D:學(xué)生管理系統(tǒng)的添加學(xué)生的代碼編寫
?* E:學(xué)生管理系統(tǒng)的刪除學(xué)生的代碼編寫
?* F:學(xué)生管理系統(tǒng)的修改學(xué)生的代碼編寫
?*/
public class StudentManagerTest {
?? ?public static void main(String[] args) throws IOException{
?? ??? ?//定義文件路徑
?? ??? ?String fileName = "students.txt";
?? ??? ?
?? ??? ?//為了讓程序能夠回到這里來,我們使用循環(huán)
?? ??? ?while(true) {
?? ??? ??? ?//這是學(xué)生管理系統(tǒng)的主界面
?? ??? ??? ?System.out.println("--------歡迎來到學(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("請輸入你的選擇:");
?? ??? ??? ?//創(chuàng)建鍵盤錄入對象
?? ??? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ??? ?String choiceString = sc.nextLine();
?? ??? ??? ?//用switch語句實現(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)建輸入緩沖流對象
?? ??? ?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ù)寫入文件
?? ?public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
?? ??? ?//創(chuàng)建輸出緩沖流對象
?? ??? ?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)建集合對象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ?
?? ??? ?//修改學(xué)生的思路:鍵盤錄入一個學(xué)號,到集合中去查找,看是否有學(xué)生使用的是該學(xué)號,如果有就修改該學(xué)生
?? ??? ?//創(chuàng)建鍵盤錄入對象
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?System.out.println("請輸入你要修改的學(xué)生的學(xué)號:");
?? ??? ?String id = sc.nextLine();
?? ??? ?
?? ??? ?//定義一個索引
?? ??? ?int index = -1;
?? ??? ?
?? ??? ?//遍歷集合
?? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ?//獲取每一個學(xué)生對象
?? ??? ??? ?Student s = array.get(x);
?? ??? ??? ?//拿學(xué)生對象的學(xué)號和鍵盤錄入的學(xué)號進行比較
?? ??? ??? ?if(s.getId().equals(id)) {
?? ??? ??? ??? ?index = x;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(index == -1) {
?? ??? ??? ?System.out.println("不好意思,你要修改的學(xué)號對應(yīng)的學(xué)生信息不存在,請回去重新你的選擇");
?? ??? ?}else {
?? ??? ??? ?System.out.println("請輸入學(xué)生新姓名:");
?? ??? ??? ?String name = sc.nextLine();
?? ??? ??? ?System.out.println("請輸入學(xué)生新年齡:");
?? ??? ??? ?String age = sc.nextLine();
?? ??? ??? ?System.out.println("請輸入學(xué)生新居住地:");
?? ??? ??? ?String address = sc.nextLine();
?? ??? ??? ?
?? ??? ??? ?//創(chuàng)建學(xué)生對象
?? ??? ??? ?Student s = new Student();
?? ??? ??? ?s.setId(id);
?? ??? ??? ?s.setName(name);
?? ??? ??? ?s.setAge(age);
?? ??? ??? ?s.setAddress(address);
?? ??? ??? ?
?? ??? ??? ?//修改集合中的學(xué)生對象
?? ??? ??? ?array.set(index, s);
?? ??? ??? ?//把集合中的數(shù)據(jù)重新寫回到文件
?? ??? ??? ?writeData(fileName, array);
?? ??? ??? ?//給出提示
?? ??? ??? ?System.out.println("修改學(xué)生成功");
?? ??? ?}
?? ?}
?? ?
?? ?//刪除學(xué)生
?? ?public static void deleteStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ?
?? ??? ?//刪除學(xué)生的思路:鍵盤錄入一個學(xué)號,到集合中去查找,看是否有學(xué)生使用的是該學(xué)號,如果有就刪除該學(xué)生
?? ??? ?//創(chuàng)建鍵盤錄入對象
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?System.out.println("請輸入你要刪除的學(xué)生的學(xué)號:");
?? ??? ?String id = sc.nextLine();
?? ??? ?
?? ??? ?//我們必須給出學(xué)號不存在的時候的提示
?? ??? ?
?? ??? ?//定義一個索引
?? ??? ?int index = -1;
?? ??? ?
?? ??? ?//遍歷集合
?? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ?//獲取到每一個學(xué)生對象
?? ??? ??? ?Student s = array.get(x);
?? ??? ??? ?//拿這個學(xué)生對象的學(xué)號和鍵盤錄入的學(xué)號進行比較
?? ??? ??? ?if(s.getId().equals(id)) {
?? ??? ??? ??? ?index = x;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if(index == -1) {
?? ??? ??? ?System.out.println("不好意思,你要刪除的學(xué)號對應(yīng)的學(xué)生信息不存在,請回去重新你的選擇");
?? ??? ?}else {
?? ??? ??? ?array.remove(index);
?? ??? ??? ?//把集合中的數(shù)據(jù)重新寫回到文件
?? ??? ??? ?writeData(fileName, array);
?? ??? ??? ?System.out.println("刪除學(xué)生成功");
?? ??? ?}
?? ??? ?
?? ?}
?? ?
?? ?//添加學(xué)生
?? ?public static void addStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ??? ??? ?
?? ??? ?//創(chuàng)建鍵盤錄入對象
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?
?? ??? ?//為了讓id能夠被訪問到,我們就把id定義在了循環(huán)的外面
?? ??? ?String id;
?? ??? ?
?? ??? ?//為了讓代碼能夠回到這里,用循環(huán)
?? ??? ?while(true) {
?? ??? ??? ?System.out.println("請輸入學(xué)生學(xué)號:");
?? ??? ??? ?//String id = sc.nextLine();
?? ??? ??? ?id = sc.nextLine();
?? ??? ??? ?
?? ??? ??? ?//判斷學(xué)號有沒有被人占用
?? ??? ??? ?//定義標記
?? ??? ??? ?boolean flag = false;
?? ??? ??? ?//遍歷集合,得到每一個學(xué)生
?? ??? ??? ?for(int x=0; x<array.size(); x++) {
?? ??? ??? ??? ?Student s = array.get(x);
?? ??? ??? ??? ?//獲取該學(xué)生的學(xué)號,和鍵盤錄入的學(xué)號進行比較
?? ??? ??? ??? ?if(s.getId().equals(id)) {
?? ??? ??? ??? ??? ?flag = true; //說明學(xué)號被占用了
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?if(flag) {
?? ??? ??? ??? ?System.out.println("你輸入的學(xué)號已經(jīng)被占用,請重新輸入");
?? ??? ??? ?}else {
?? ??? ??? ??? ?break; //結(jié)束循環(huán)
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?System.out.println("請輸入學(xué)生姓名:");
?? ??? ?String name = sc.nextLine();
?? ??? ?System.out.println("請輸入學(xué)生年齡:");
?? ??? ?String age = sc.nextLine();
?? ??? ?System.out.println("請輸入學(xué)生居住地:");
?? ??? ?String address = sc.nextLine();
?? ??? ?
?? ??? ?//創(chuàng)建學(xué)生對象
?? ??? ?Student s = new Student();
?? ??? ?s.setId(id);
?? ??? ?s.setName(name);
?? ??? ?s.setAge(age);
?? ??? ?s.setAddress(address);
?? ??? ?
?? ??? ?//把學(xué)生對象作為元素添加到集合
?? ??? ?array.add(s);
?? ??? ?//把集合中的數(shù)據(jù)重新寫回到文件
?? ??? ?writeData(fileName, array);
?? ??? ?
?? ??? ?//給出提示
?? ??? ?System.out.println("添加學(xué)生成功");
?? ?}
?? ?
?? ?//查看所有學(xué)生
?? ?public static void findAllStudent(String fileName) throws IOException {
?? ??? ?//創(chuàng)建集合對象
?? ??? ?ArrayList<Student> array = new ArrayList<Student>();
?? ??? ?//從文件中把數(shù)據(jù)讀取到集合中
?? ??? ?readData(fileName, array);
?? ??? ?
?? ??? ?//首先來判斷集合中是否有數(shù)據(jù),如果沒有數(shù)據(jù),就給出提示,并讓該方法不繼續(xù)往下執(zhí)行
?? ??? ?if(array.size() == 0) {
?? ??? ??? ?System.out.println("不好意思,目前沒有學(xué)生信息可供查詢,請回去重新選擇你的操作");
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?
?? ??? ?//\t 其實就是一個tab鍵的位置
?? ??? ?System.out.println("學(xué)號\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());
?? ??? ?}
?? ?}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java使用POI操作excel文件

    java使用POI操作excel文件

    本文主要介紹了java使用POI操作excel文件,實現(xiàn)批量導(dǎo)出和導(dǎo)入的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • SpringBoot中的共享Session域詳解

    SpringBoot中的共享Session域詳解

    這篇文章主要介紹了SpringBoot中的共享Session域詳解,使用Redis解決Session共享問題的原理非常簡單,就是把原本存儲在不同服務(wù)器上的Session拿出來放在一個獨立的服務(wù)器上,需要的朋友可以參考下
    2024-01-01
  • SpringBoot整合JWT Token的完整步驟

    SpringBoot整合JWT Token的完整步驟

    JSON Web Token是目前最流行的跨域認證解決方案,適合前后端分離項目通過Restful API進行數(shù)據(jù)交互時進行身份認證,這篇文章主要給大家介紹了關(guān)于SpringBoot整合JWT Token的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Opencv創(chuàng)建車牌圖片識別系統(tǒng)方法詳解

    Opencv創(chuàng)建車牌圖片識別系統(tǒng)方法詳解

    本文主要介紹了一個基于spring?boot+maven+opencv實現(xiàn)的圖像識別及訓(xùn)練項目,可以實現(xiàn)車牌識別功能,感興趣的可以跟隨小編一起試一試
    2022-01-01
  • 如何使用Spring Boot ApplicationRunner解析命令行中的參數(shù)

    如何使用Spring Boot ApplicationRunner解析命令行中的參數(shù)

    這篇文章主要介紹了使用Spring Boot ApplicationRunner解析命令行中的參數(shù),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-12-12
  • 親手教你SpringBoot中的多數(shù)據(jù)源集成問題

    親手教你SpringBoot中的多數(shù)據(jù)源集成問題

    本文主要是介紹基于springboot的多數(shù)據(jù)源切換,輕量級的一種集成方案,對于小型的應(yīng)用可以采用這種方案,我之前在項目中用到是因為簡單,便于擴展以及優(yōu)化,對SpringBoot多數(shù)據(jù)源集成問題感興趣的朋友一起看看吧
    2022-03-03
  • springboot整合mybatis-plus基于注解實現(xiàn)一對一(一對多)查詢功能

    springboot整合mybatis-plus基于注解實現(xiàn)一對一(一對多)查詢功能

    這篇文章主要介紹了springboot整合mybatis-plus基于純注解實現(xiàn)一對一(一對多)查詢功能,因為本人采用的是spring-boot進行開發(fā),本身springboot就提倡采用不用配置自動配置的方式,所以真心希望mybatis(不是mybatis-plus)這點需要繼續(xù)努力
    2021-09-09
  • mybatis-plus多表聯(lián)查join的實現(xiàn)

    mybatis-plus多表聯(lián)查join的實現(xiàn)

    本文主要介紹了mybatis-plus多表聯(lián)查join的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2023-01-01
  • Java BigDecimal和double示例及相關(guān)問題解析

    Java BigDecimal和double示例及相關(guān)問題解析

    這篇文章主要介紹了Java BigDecimal和double示例及相關(guān)問題解析,簡單介紹了BigDecimal類的相關(guān)內(nèi)容,分享了兩則相關(guān)實例,對問題進行了分析,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • SpringFactoriesLoader類作用詳解

    SpringFactoriesLoader類作用詳解

    SpringFactoriesLoader可以加載jar包下META-INF下的spring.factories,把相關(guān)接口的實現(xiàn)按照key,value的形式加載到內(nèi)存,一個接口的多個實現(xiàn)可以按照","進行分割
    2022-10-10

最新評論