java學生管理系統

来源:https://www.cnblogs.com/liweichao-you/archive/2019/11/29/11960446.html
-Advertisement-
Play Games

student類 package cn.itheima.Manag;/** * *標準類 * **/public class Student { //學號 private String id; //姓名 private String name; //年齡 private String age; // ...


student類

package cn.itheima.Manag;

/**
*
*標準類
*
**/
public class Student {
//學號
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;
}

}
StudentManagerTest類
package cn.itheima.Manag;

/**
*
*
*
**/
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;

/*
* 這是我的學生管理系統的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統的主界面的代碼編寫
* C:學生管理系統的查看所有學生的代碼編寫
* D:學生管理系統的添加學生的代碼編寫
* E:學生管理系統的刪除學生的代碼編寫
* F:學生管理系統的修改學生的代碼編寫
*/
public class StudentManagerTest {
public static void main(String[] args) throws IOException{
//定義文件路徑
String fileName = "day_10\\students.txt";

//為了讓程式能夠回到這裡來,我們使用迴圈
while(true) {
//這是學生管理系統的主界面
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("請輸入你的選擇:");
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用switch語句實現選擇
switch(choiceString) {
case "1":
//查看所有學生
findAllStudent(fileName);
break;
case "2":
//添加學生
addStudent(fileName);
break;
case "3":
//刪除學生
deleteStudent(fileName);
break;
case "4":
//修改學生
updateStudent(fileName);
break;
case "5":
default:
System.out.println("謝謝你的使用");
System.exit(0); //JVM退出
break;
}
}
}

// 從文件中讀數據到集合
public static void readData(String fileName, ArrayList<Student> array) throws IOException {
// 創建輸入緩衝流對象
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();
}

// 把集合中的數據寫入文件
public static void writeData(String fileName, ArrayList<Student> array) throws IOException {
// 創建輸出緩衝流對象
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();
}

//修改學生
public static void updateStudent(String fileName) throws IOException {
//創建集合對象
ArrayList<Student> array = new ArrayList<Student>();
//從文件中把數據讀取到集合中
readData(fileName, array);

//修改學生的思路:鍵盤錄入一個學號,到集合中去查找,看是否有學生使用的是該學號,如果有就修改該學生
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要修改的學生的學號:");
String id = sc.nextLine();

//定義一個索引
int index = -1;

//遍歷集合
for(int x=0; x<array.size(); x++) {
//獲取每一個學生對象
Student s = array.get(x);
//拿學生對象的學號和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
index = x;
break;
}
}

if(index == -1) {
System.out.println("不好意思,你要修改的學號對應的學生信息不存在,請回去重新你的選擇");
}else {
System.out.println("請輸入學生新姓名:");
String name = sc.nextLine();
System.out.println("請輸入學生新年齡:");
String age = sc.nextLine();
System.out.println("請輸入學生新居住地:");
String address = sc.nextLine();

//創建學生對象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);

//修改集合中的學生對象
array.set(index, s);
//把集合中的數據重新寫回到文件
writeData(fileName, array);
//給出提示
System.out.println("修改學生成功");
}
}

//刪除學生
public static void deleteStudent(String fileName) throws IOException {
//創建集合對象
ArrayList<Student> array = new ArrayList<Student>();
//從文件中把數據讀取到集合中
readData(fileName, array);

//刪除學生的思路:鍵盤錄入一個學號,到集合中去查找,看是否有學生使用的是該學號,如果有就刪除該學生
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要刪除的學生的學號:");
String id = sc.nextLine();

//我們必須給出學號不存在的時候的提示

// //定義一個索引
int index = -1;
// boolean flag = false;

//遍歷集合
for(int x=0; x<array.size(); x++) {
//獲取到每一個學生對象
Student s = array.get(x);
//拿這個學生對象的學號和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
index = x;
// flag = true;
break;
}
}

if(index!=-1) {
array.remove(index);
//把集合中的數據重新寫回到文件
writeData(fileName, array);
System.out.println("刪除學生成功");

}else {
System.out.println("不好意思,你要刪除的學號對應的學生信息不存在,請回去重新你的選擇");
}

}

//添加學生
public static void addStudent(String fileName) throws IOException {
//創建集合對象
ArrayList<Student> array = new ArrayList<Student>();
//從文件中把數據讀取到集合中
readData(fileName, array);

//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);

//為了讓id能夠被訪問到,我們就把id定義在了迴圈的外面
String id;

//為了讓代碼能夠回到這裡,用迴圈
while(true) {
System.out.println("請輸入學生學號:");
//String id = sc.nextLine();
id = sc.nextLine();

//判斷學號有沒有被人占用
//定義標記
boolean flag = false;
//遍歷集合,得到每一個學生
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
//獲取該學生的學號,和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
flag = true; //說明學號被占用了
break;
}
}

if(flag) {
System.out.println("你輸入的學號已經被占用,請重新輸入");
}else {
break; //結束迴圈
}
}


System.out.println("請輸入學生姓名:");
String name = sc.nextLine();
System.out.println("請輸入學生年齡:");
String age = sc.nextLine();
System.out.println("請輸入學生居住地:");
String address = sc.nextLine();

//創建學生對象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);

//把學生對象作為元素添加到集合
array.add(s);
//把集合中的數據重新寫回到文件
writeData(fileName, array);

//給出提示
System.out.println("添加學生成功");
}

//查看所有學生
public static void findAllStudent(String fileName) throws IOException {
//創建集合對象
ArrayList<Student> array = new ArrayList<Student>();
//從文件中把數據讀取到集合中
readData(fileName, array);

//首先來判斷集合中是否有數據,如果沒有數據,就給出提示,並讓該方法不繼續往下執行
if(array.size() == 0) {
System.out.println("不好意思,目前沒有學生信息可供查詢,請回去重新選擇你的操作");
return;
}

//\t 其實就是一個tab鍵的位置
System.out.println("學號\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());
}
}
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • AQS是併發編程中非常重要的概念,它是juc包下的許多併發工具類,如CountdownLatch,CyclicBarrier,Semaphore 和鎖, 如ReentrantLock, ReaderWriterLock的實現基礎,提供了一個基於int狀態碼和隊列來實現的併發框架。本文將對AQS框架的 ...
  • ``` # 迴文單詞是從左到右和從右到左讀相同的單詞。 # 例如:“detartrated”和“evitative”是迴文 str_in = input('Input:') # 方法一 count = 0 for i in range(len(str_in)): if str_in[i] == st... ...
  • 一、效果圖 二、具體效果實現代碼 1 public static void main(String[] args) { 2 int[][] array = new int[10][10]; 3 int num = 1; 4 for(int i=0;i<array.length;i++){ 5 6 i ...
  • 任何一個java對象都天然繼承於Object類,線上程間實現通信的往往會應用到Object的幾個方法,比如wait(),wait(long timeout),wait(long timeout, int nanos)與notify(),notifyAll()幾個方法實現等待/通知機制,同樣的, 在j... ...
  • 2.1、anaconda的安裝 ​ 1 、安裝可執行程式 ​ 2 、配置環境變數 ​ 根據環境變數的 去查找可執行程式文件,如果查找到就執行,如果查找不到就報錯。 ​ 3 、python的多版本相容問題 ​ 修改可執行程式的文件名,再配置環境變數 ​ 4 、hash案例 2.2、requests模塊 ...
  • 生活中,當你閑暇之餘瀏覽資訊的時候,當你搜索資料但繁雜信息夾雜時候,你就會想,如何更為準確的定位需求信息。今天就為你帶來: 分頁查詢 需求分析:在列表頁面中,顯示指定條數的數據,通過翻頁按鈕完成首頁/上一頁/下一頁/尾頁的查詢 數據分析: 通過觀察,頁面上需要顯示下麵的幾個數據:當前頁:curren ...
  • 安裝 SQLAlchemy 報錯 安裝命令 報錯截圖 編碼錯誤,這裡我們需要改下源碼 解決方案 重新安裝,安裝成功 參數文章: ...
  • 在python3.7 環境下 函數聲明時能在參數後加冒號,如圖: 可能有疑問,python不是動態類型語言 ,難不成還能指定參數類型? 來看一下列印結果: 但同時也確實能傳其他類型的值 如:f("test",123) 那結果如何呢? 如下: 當然會報錯了啊,返回值是一個字元串,int型不能參與字元串 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...