???博客首頁:痛而不言笑而不語的淺傷
??歡迎關(guān)注??點(diǎn)贊 ?? 收藏 ?留言 ?? 歡迎討論!
??本文由痛而不言笑而不語的淺傷原創(chuàng),CSDN首發(fā)!
??系列專欄:《Java每日一練》
??首發(fā)時(shí)間:2022年6月4日
?:熱愛Java學(xué)習(xí),期待一起交流!
????作者水平有限,如果發(fā)現(xiàn)錯(cuò)誤,求告知,多謝!
??有問題可以私信交流?。?!
目錄
運(yùn)行展示
界面初始化
查看所有學(xué)生
?添加學(xué)生
刪除學(xué)生
修改學(xué)生
退出系統(tǒng)
?完整源代碼?
?文章來源地址http://www.zghlxwxcb.cn/news/detail-795580.html
?* 標(biāo)準(zhǔn)的IO版學(xué)生管理系統(tǒng)
?*?
?* * 分析:
?* ?? ??? ?1.定義學(xué)生類
?* ?? ??? ?2.學(xué)生管理系統(tǒng)主界面的代碼編寫
?* ?? ??? ?3.學(xué)生管理系統(tǒng)的查看所有學(xué)生的代碼編寫
?* ?? ??? ?4.學(xué)生管理系統(tǒng)的添加學(xué)生的代碼編寫
?* ?? ??? ?5.學(xué)生管理系統(tǒng)的刪除學(xué)生的代碼編寫
?* ?? ??? ?6.學(xué)生管理系統(tǒng)的修改學(xué)生的代碼編寫
????????其實(shí)挺簡單的,就最基本的Java基礎(chǔ)語法部分。很適合初學(xué)者練習(xí)。其實(shí)主要從Java面向?qū)ο蠛虸O流的一個(gè)熟練掌握。最后是轉(zhuǎn)換成jar文件,通過軟件弄成了.exe的文件。執(zhí)行exe文件就是如下運(yùn)行結(jié)果。數(shù)據(jù)的保存是在同目錄下的TXT文件,需要注意的是TXT文件和可執(zhí)行的exe文件一定要放在同一文件夾內(nèi),不然會(huì)報(bào)錯(cuò)。
運(yùn)行展示
界面初始化
public class StudentManagerTest_IO {
public static void main(String[] args) throws Exception {
File f1 = new File("?G:\\學(xué)生管理系統(tǒng)\\Student1.txt");
// 創(chuàng)建持久相對(duì)路勁
String findName = f1.getName();
// 學(xué)生管理系統(tǒng)主界面的代碼編寫
// 死循環(huán)執(zhí)行程序
while (true) {
System.out.println("----------歡迎使用老馬教育學(xué)生管理系統(tǒng)----------");
System.out.println("請(qǐng)輸入你要執(zhí)行的操作:");
System.out.println("1 查看所有學(xué)生");
System.out.println("2 添加學(xué)生");
System.out.println("3 刪除學(xué)生");
System.out.println("4 修改學(xué)生");
System.out.println("5 退出系統(tǒng)");
查看所有學(xué)生
// 學(xué)生管理系統(tǒng)的查看所有學(xué)生的代碼編寫
public static void findAllStudent(String findName) throws Exception {
// 創(chuàng)建集合對(duì)象存儲(chǔ)學(xué)生數(shù)據(jù)
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 遍歷集合到輸出控制臺(tái)
// 首先判斷集合中是否有數(shù)據(jù)
if (array.size() == 0) {
System.out.println("不好意思,目前沒有學(xué)生信息可供查詢,請(qǐng)重新你的選擇!");
} else {
System.out.println("所有學(xué)生信息如下:");
System.out.println("|-------|-------|-------|-------|");
System.out.println("|" + "學(xué)號(hào)" + "\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();
}
}
?文章來源:http://www.zghlxwxcb.cn/news/detail-795580.html
?添加學(xué)生
// 學(xué)生管理系統(tǒng)的添加學(xué)生的代碼編寫
public static void addStudent(String findName) throws Exception {
// 創(chuàng)建存儲(chǔ)數(shù)據(jù)的集合對(duì)象
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
String id;
// 判斷該學(xué)號(hào)是否已被占用
while (true) {
// 定義標(biāo)記
boolean flag = false;
// 添加學(xué)號(hào)
System.out.println("請(qǐng)您輸入要添加的學(xué)生學(xué)號(hào):");
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("不好意思,你輸入的學(xué)號(hào)已被占用,請(qǐng)你重新你的選擇:");
break;
} else {
// 添加姓名
System.out.println("請(qǐng)您輸入學(xué)生姓名:");
String name = sc.nextLine();
System.out.println("請(qǐng)您輸入學(xué)生年齡:");
String ages = 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.setAges(ages);
s.setAddress(address);
// 把學(xué)生對(duì)象作為元素添加到集合中
array.add(s);
// 調(diào)用寫數(shù)據(jù)方法
writerData(findName, array);
// 添加成功的提示語
System.out.println("添加學(xué)生信息成功!");
break;
}
}
}
?
刪除學(xué)生
// 學(xué)生管理系統(tǒng)的刪除學(xué)生的代碼編寫
public static void deleteStudent(String findName) throws Exception {
// 創(chuàng)建學(xué)生數(shù)據(jù)存儲(chǔ)的空集合
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
// 定義標(biāo)記
int index = -1;
// 定義學(xué)號(hào)變量
String id;
// 刪除學(xué)生信息
// 數(shù)據(jù)要?jiǎng)h除學(xué)生信息的學(xué)生學(xué)號(hào)
System.out.println("請(qǐng)您輸入要?jiǎng)h除學(xué)生信息的學(xué)生學(xué)號(hào):");
id = sc.nextLine();
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
// 判斷是否有要?jiǎng)h除的這個(gè)學(xué)號(hào)
if (s.getId().equalsIgnoreCase(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("您想要?jiǎng)h除的學(xué)生信息不存在,請(qǐng)重新您的選擇!");
} else {
array.remove(index);
// 調(diào)用寫文件方法
writerData(findName, array);
// 提示語
System.out.println("刪除學(xué)生信息成功!");
}
}
?
修改學(xué)生
// 學(xué)生管理系統(tǒng)的修改學(xué)生的代碼編寫
public static void alterStudent(String findName) throws Exception {
// 創(chuàng)建存儲(chǔ)學(xué)生數(shù)據(jù)的空集合
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
// 定義標(biāo)記
int index = -1;
// 定義學(xué)生學(xué)號(hào)變量
String id;
// 輸入學(xué)號(hào)判斷
while (true) {
// 輸入學(xué)號(hào)
System.out.println("請(qǐng)您輸入想要修改學(xué)生信息的學(xué)號(hào):");
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("您想要修改的學(xué)生信息不存在,請(qǐng)您重新輸入學(xué)號(hào):");
} else {
// 修改姓名
System.out.println("請(qǐng)您輸入學(xué)生姓名:");
String name = sc.nextLine();
// 修改年齡
System.out.println("請(qǐng)您輸入學(xué)生年齡:");
String ages = sc.nextLine();
// 修改居住地址
System.out.println("請(qǐng)您輸入居住地址:");
String address = sc.nextLine();
// 創(chuàng)建學(xué)生對(duì)象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAges(ages);
s.setAddress(address);
// 添加到集合
array.set(index, s);
// 調(diào)用寫文件方法
writerData(findName, array);
// 提示語
System.out.println("修改學(xué)生信息成功!");
break;
}
}
}
}
退出系統(tǒng)
// 退出系統(tǒng)
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;
/*
* 標(biāo)準(zhǔn)的IO版學(xué)生管理系統(tǒng)
*
* * 分析:
* 1.定義學(xué)生類
* 2.學(xué)生管理系統(tǒng)主界面的代碼編寫
* 3.學(xué)生管理系統(tǒng)的查看所有學(xué)生的代碼編寫
* 4.學(xué)生管理系統(tǒng)的添加學(xué)生的代碼編寫
* 5.學(xué)生管理系統(tǒng)的刪除學(xué)生的代碼編寫
* 6.學(xué)生管理系統(tǒng)的修改學(xué)生的代碼編寫
*
* */
public class StudentManagerTest_IO {
public static void main(String[] args) throws Exception {
File f1 = new File("?G:\\學(xué)生管理系統(tǒng)\\Student1.txt");
// 創(chuàng)建持久相對(duì)路勁
String findName = f1.getName();
// 學(xué)生管理系統(tǒng)主界面的代碼編寫
// 死循環(huán)執(zhí)行程序
while (true) {
System.out.println("----------歡迎使用老馬教育學(xué)生管理系統(tǒng)----------");
System.out.println("請(qǐng)輸入你要執(zhí)行的操作:");
System.out.println("1 查看所有學(xué)生");
System.out.println("2 添加學(xué)生");
System.out.println("3 刪除學(xué)生");
System.out.println("4 修改學(xué)生");
System.out.println("5 退出系統(tǒng)");
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
// 輸入所匹配的序號(hào)并執(zhí)行操作
String option = sc.nextLine();
switch (option) {
case "1":
// 查看所有學(xué)生信息
findAllStudent(findName);
break;
case "2":
// 添加學(xué)生信息
addStudent(findName);
break;
case "3":
// 刪除學(xué)生信息
deleteStudent(findName);
break;
case "4":
// 修改學(xué)生信息
alterStudent(findName);
break;
case "5":
// 退出系統(tǒng)
System.out.println("感謝您的使用,辛苦了!");
System.exit(0);
break;
default:
System.out.println("您輸入的選擇不存在,請(qǐng)您重新輸入!");
break;
}
}
}
// 把文件中的數(shù)據(jù)讀到集合中
public static void readData(String findName, ArrayList<Student> array) throws Exception {
// 創(chuàng)建輸入緩沖流對(duì)象
BufferedReader br = new BufferedReader(new FileReader(findName));
// 創(chuàng)建數(shù)組讀取文本文件數(shù)據(jù)并按照?qǐng)?zhí)行格式分割,然后把讀取的數(shù)據(jù)作為元素存儲(chǔ)到集合,最后遍歷集合到輸出控制臺(tái)
// 定義索引
String line;
while ((line = br.readLine()) != null) {
// 讀取文本文件數(shù)據(jù)并按照?qǐng)?zhí)行格式分割
String[] strArray = line.split(",");
// 創(chuàng)建學(xué)生對(duì)象
Student s = new Student();
s.setId(strArray[0]);
s.setName(strArray[1]);
s.setAges(strArray[2]);
s.setAddress(strArray[3]);
// 把讀取的數(shù)據(jù)作為元素存儲(chǔ)到集合
array.add(s);
}
// 釋放資源你
br.close();
}
// 把集合中的數(shù)據(jù)寫入文件中
public static void writerData(String findName, ArrayList<Student> array) throws Exception {
// 創(chuàng)建輸出緩沖流對(duì)象
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();
}
// 學(xué)生管理系統(tǒng)的查看所有學(xué)生的代碼編寫
public static void findAllStudent(String findName) throws Exception {
// 創(chuàng)建集合對(duì)象存儲(chǔ)學(xué)生數(shù)據(jù)
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 遍歷集合到輸出控制臺(tái)
// 首先判斷集合中是否有數(shù)據(jù)
if (array.size() == 0) {
System.out.println("不好意思,目前沒有學(xué)生信息可供查詢,請(qǐng)重新你的選擇!");
} else {
System.out.println("所有學(xué)生信息如下:");
System.out.println("|-------|-------|-------|-------|");
System.out.println("|" + "學(xué)號(hào)" + "\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();
}
}
// 學(xué)生管理系統(tǒng)的添加學(xué)生的代碼編寫
public static void addStudent(String findName) throws Exception {
// 創(chuàng)建存儲(chǔ)數(shù)據(jù)的集合對(duì)象
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
String id;
// 判斷該學(xué)號(hào)是否已被占用
while (true) {
// 定義標(biāo)記
boolean flag = false;
// 添加學(xué)號(hào)
System.out.println("請(qǐng)您輸入要添加的學(xué)生學(xué)號(hào):");
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("不好意思,你輸入的學(xué)號(hào)已被占用,請(qǐng)你重新你的選擇:");
break;
} else {
// 添加姓名
System.out.println("請(qǐng)您輸入學(xué)生姓名:");
String name = sc.nextLine();
System.out.println("請(qǐng)您輸入學(xué)生年齡:");
String ages = 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.setAges(ages);
s.setAddress(address);
// 把學(xué)生對(duì)象作為元素添加到集合中
array.add(s);
// 調(diào)用寫數(shù)據(jù)方法
writerData(findName, array);
// 添加成功的提示語
System.out.println("添加學(xué)生信息成功!");
break;
}
}
}
// 學(xué)生管理系統(tǒng)的刪除學(xué)生的代碼編寫
public static void deleteStudent(String findName) throws Exception {
// 創(chuàng)建學(xué)生數(shù)據(jù)存儲(chǔ)的空集合
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
// 定義標(biāo)記
int index = -1;
// 定義學(xué)號(hào)變量
String id;
// 刪除學(xué)生信息
// 數(shù)據(jù)要?jiǎng)h除學(xué)生信息的學(xué)生學(xué)號(hào)
System.out.println("請(qǐng)您輸入要?jiǎng)h除學(xué)生信息的學(xué)生學(xué)號(hào):");
id = sc.nextLine();
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
// 判斷是否有要?jiǎng)h除的這個(gè)學(xué)號(hào)
if (s.getId().equalsIgnoreCase(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("您想要?jiǎng)h除的學(xué)生信息不存在,請(qǐng)重新您的選擇!");
} else {
array.remove(index);
// 調(diào)用寫文件方法
writerData(findName, array);
// 提示語
System.out.println("刪除學(xué)生信息成功!");
}
}
// 學(xué)生管理系統(tǒng)的修改學(xué)生的代碼編寫
public static void alterStudent(String findName) throws Exception {
// 創(chuàng)建存儲(chǔ)學(xué)生數(shù)據(jù)的空集合
ArrayList<Student> array = new ArrayList<Student>();
// 調(diào)用讀數(shù)據(jù)方法
readData(findName, array);
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
// 定義標(biāo)記
int index = -1;
// 定義學(xué)生學(xué)號(hào)變量
String id;
// 輸入學(xué)號(hào)判斷
while (true) {
// 輸入學(xué)號(hào)
System.out.println("請(qǐng)您輸入想要修改學(xué)生信息的學(xué)號(hào):");
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("您想要修改的學(xué)生信息不存在,請(qǐng)您重新輸入學(xué)號(hào):");
} else {
// 修改姓名
System.out.println("請(qǐng)您輸入學(xué)生姓名:");
String name = sc.nextLine();
// 修改年齡
System.out.println("請(qǐng)您輸入學(xué)生年齡:");
String ages = sc.nextLine();
// 修改居住地址
System.out.println("請(qǐng)您輸入居住地址:");
String address = sc.nextLine();
// 創(chuàng)建學(xué)生對(duì)象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAges(ages);
s.setAddress(address);
// 添加到集合
array.set(index, s);
// 調(diào)用寫文件方法
writerData(findName, array);
// 提示語
System.out.println("修改學(xué)生信息成功!");
break;
}
}
}
}
最后呢?文章到這里就結(jié)束啦,你們學(xué)廢了嗎?
?
好啦!今天的練習(xí)就到這里??窗蛇@么努力的你又學(xué)到了很多,新的一天加油鴨!?。?/em>
你的點(diǎn)贊是對(duì)我最大的鼓勵(lì)。
你的收藏是對(duì)我文章的認(rèn)可。
你的關(guān)注是對(duì)我創(chuàng)作的動(dòng)力。
?
?
到了這里,關(guān)于用Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)【簡化版】基礎(chǔ)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!