java学生管理系统代码
//導入的包名
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;
?
//學生管理系統類
public?class?StudentManager {
public?static?void?main(String[] args) throws?IOException {
ArrayList<Student> array = new?ArrayList<Student>();//定義一個集合,用于接收學生對象的信息。
Scanner sc = new?Scanner(System.in);//定義Scanner對象用于接收指令信息。
System.out.println("----------歡迎來到學生管理系統----------");
//建立循環使程序能夠循環進行。
while(true){
顯示();//包含指令信息已經對應的操作。
System.out.println("請輸入一個數字進行操作:");
int?num = sc.nextInt();//根據提示,錄入指令。
switch(num){//根據指令,選擇需要進行的操作。
case?1:
查看學生(array);//具體如方法名
break;
case?2:
添加學生(array);//具體如方法名
break;
case?3:
修改學生(array);//具體如方法名
break;
case?4:
刪除學生(array);//具體如方法名
break;
case?5:
是否保存信息(array);//具體如方法名
System.out.println("系統已退出!");
System.exit(0);//退出系統的代碼。
break;
default:
System.out.println("你輸入有誤請重新輸入!");
break;
}
}
}
public?static?void?是否保存信息(ArrayList<Student> array) throws?IOException {//退出系統提示是否保存信息。
Scanner sc = new?Scanner(System.in);//定義Scanner對象用于接收指令信息。
System.out.println("是否保留本次操作的結果!(輸入1保存,輸入2不保存):");
//建立循環使程序在輸入有問題的時候能夠循環進行。
while(true){
int?num =sc.nextInt();//獲取指令信息。
if(num==1){//當指令為1時,保存此次操作的結果。
System.out.println("請輸入保存數據的文件路徑:");//選擇一個保存文件的路徑。
String f = sc.next();
BufferedWriter bw = new?BufferedWriter(new?FileWriter(f));//建立對象用于接收數據。
bw.write("學號"+"\t"+"姓名"+"\t"+"年齡"+"\t"+"戶籍");//文檔抬頭。
bw.newLine();//換行
for?(int?i = 0; i < array.size(); i++) {//建立for循環,將集合中的數據循環錄入到文檔中。
Student s = array.get(i);//得到集合中i索引的對象。
bw.write(s.get學號()+"\t"+s.get姓名()+"\t"+s.get年齡()+"\t"+s.get戶籍());//將集合中i索引的對象的信息數據錄入到文檔中。
bw.newLine();//換行
}
bw.close();//結束輸入。
break;//跳出while循環,從而結束方法。
}else?if(num==2){
break;//當不保存操作結果的時候,不用進行任何操作,跳出循環即可。
}else{
System.out.println("你輸入有誤!請重新輸入(輸入1保存,輸入2不保存):");//輸入錯誤的提示。
}
}
}
public?static?void?顯示() {//包含指令信息已經對應的操作。
System.out.println("1.輸入1查看(查看系統中所有的學生信息);");
System.out.println("2.輸入2添加(添加學生信息到系統中);");
System.out.println("3.輸入3修改(根據學號修改學生信息);");
System.out.println("4.輸入4刪除(根據學號刪除學生信息);");
System.out.println("5.輸入5退出。");
}
public?static?void?刪除學生(ArrayList<Student> array) throws?IOException {//按照錄入與導入的方法修改集合當中學生的信息。
Scanner sc = new?Scanner(System.in);//定義Scanner對象用于接收指令信息。
System.out.println("請輸入刪除的方法(1、導入 ?2、手動輸入):");
while(true){
int?num = sc.nextInt();
if(num==2){//手動輸入。
System.out.println("請輸入學生學號:");
String code = sc.nextLine();//學號。
int?index = 判斷(array, code);
if(index!=-1){//當索引值為true的時候,即集合中存在輸入的學號的時候,根據該對象的索引刪除該對象。
array.remove(index);
System.out.println("刪除成功!");
}else{
System.out.println("系統中不存在該學號,請重新輸入!");//不存在就循環錄入。
}
}else?if(num==1){//導入。
System.out.println("請輸入需要刪除數據的文件路徑:");//選擇一個文件的路徑。
String filename = sc.next();
BufferedReader br = new?BufferedReader(new?FileReader (filename));//建立導入數據的對象
String line="";//定義字符串用于接收整行數據。
int?sum = 0 ;//用于接收刪除的學生總數。
while((line = br.readLine())!=null){//判斷讀取的數據是否為空。
String[] s = line.split(" ");//利用空格分割字符串得到Student對象的信息。
//下面的方法同上面手動錄入的過程。
int?index = 判斷(array, s[0]);
if(index!=-1){
array.remove(index);
sum++ ;
}else{
System.out.println("學號'"+s[0]+"不存在");
}
}
br.close();
System.out.println("成功刪除了"+sum+"位同學的信息!");
break;
}else{
System.out.println("你輸入有誤,請重新選擇刪除的方法(1、導入 ?2、手動輸入):");
}
}
}
public?static?void?修改學生(ArrayList<Student> array) throws?IOException {//按照錄入與導入的方法修改集合當中學生的信息。
Scanner sc = new?Scanner(System.in);
System.out.println("請輸入修改的方法(1、導入 ?2、手動輸入):");
while(true){
int?num = sc.nextInt();
if(num==1){
System.out.println("請輸入需要修改數據的文件路徑:");
String filename = sc.next();
BufferedReader br = new?BufferedReader(new?FileReader (filename));
String line="";
int?sum = 0 ;
//while循環判斷是否錄入完畢。
while((line = br.readLine())!=null){
String[] s = line.split(" ");
//for循環判斷是否存在錄入的學號
int?index = 判斷(array, s[0]);
if(index==-1){
System.out.println("學號'"+s[0]+"不存在");
}else{
Student st = new?Student(s);
array.set(index,st);
sum++ ;
}
}
br.close();
System.out.println("成功修改了"+sum+"位同學的信息!");
break;
}else?if(num==2){
System.out.println("請輸入學生學號:");
String code = sc.nextLine();
//for循環判斷是否存在錄入的學號
int?index = 判斷(array, code);
if(index==-1){
System.out.println("系統中不存在學號'"+code+"',請重新輸入!");
}else{
System.out.println("請輸入學生新姓名:");
String name = sc.next();
System.out.println("請輸入學生新年齡:");
String age = sc.next();
System.out.println("請輸入學生新戶籍:");
String place=sc.next();
Student s = new?Student(code,name,age,place);
array.set(index,s);
System.out.println("修改成功!");
break;
}
}else{
System.out.println("你輸入有誤,請重新選擇修改的方法(1、導入 ?2、手動輸入):");
}
}
}
public?static?void?查看學生(ArrayList<Student> array) {//將集合中的數據以特定格式遍歷出來。
if(array.isEmpty()==true){
System.out.println("系統中還未存儲學生信息,請你添加之后再查看!");
}else{
System.out.println("學號"+"\t"+"姓名"+"\t"+"年齡"+"\t"+"戶籍");
for?(int?i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.get學號()+"\t"+s.get姓名()+"\t"+s.get年齡()+"\t"+s.get戶籍());
}
}
}
public?static?void?添加學生(ArrayList<Student> array) throws?IOException {//按照錄入與導入的方法添加學生的信息到集合中。
Scanner sc = new?Scanner(System.in);
System.out.println("請輸入添加的方法(1、導入 ?2、手動輸入):");
while(true){
int?num = sc.nextInt();
if(num==1){
System.out.println("請輸入需要導入數據的文件路徑:");
String filename = sc.next();
BufferedReader br = new?BufferedReader(new?FileReader (filename));
String line="";
int?sum = 0;
//while循環判斷是否錄入完畢。
while((line = br.readLine())!=null){
String[] s = line.split(" ");
int?index = 判斷(array, s[0]);
if(index!=-1){
System.out.println("學號'"+s[0]+"'在系統中已存在!");
}else{
Student st = new?Student(s);
array.add(st);//將對象存入集合。
sum++;
}
}
br.close();
System.out.println("成功添加了"+sum+"位同學的信息!");
return;
}else?if(num==2){
System.out.println("請輸入學生學號:");
String code = sc.next();
int?index = 判斷(array, code);
if(index!=-1){
System.out.println("學號'"+code+"'已經存在,請重新輸入!");
}else{
System.out.println("請輸入學生姓名:");
String name = sc.next();
System.out.println("請輸入學生年齡:");
String age = sc.next();
System.out.println("請輸入學生戶籍:");
String place=sc.next();
Student s = new?Student(code,name,age,place);
array.add(s);//將對象存入集合。
System.out.println("添加成功!");
return;
}
}else{
System.out.println("你輸入有誤,請重新選擇添加的方法:");
}
}
}
public?static?int?判斷(ArrayList<Student> array, String code) {//判斷集合中是否存在與code相同的學號,如果存在,返回值為索引值,不存在返回值為-1。
for?(int?i = 0; i < array.size(); i++) {//遍歷集合。
if?(array.get(i).get學號().equals(code)){//判斷集合中是否存在與code相同的學號,如果存在,同時得到索引值。
return?i ;
}
}
return?-1;//不存在返回值為-1。
}
}
?
//學生類
public?class?Student {
private?String 學號?;
private?String 姓名?;
private?String 年齡?;
private?String 戶籍?;
public?Student() {
}
public?Student(String[] s) {
this.學號?= s[0];
this.姓名?= s[1];
this.年齡?= s[2];
this.戶籍?= s[3];
}
public?Student(String 學號, String 姓名, String 年齡, String 戶籍) {
this.學號?= 學號;
this.姓名?= 姓名;
this.年齡?= 年齡;
this.戶籍?= 戶籍;
}
public?String get學號() {
return?學號;
}
public?void?set學號(String 學號) {
this.學號?= 學號;
}
public?String get姓名() {
return?姓名;
}
public?void?set姓名(String 姓名) {
this.姓名?= 姓名;
}
public?String get年齡() {
return?年齡;
}
public?void?set年齡(String 年齡) {
this.年齡?= 年齡;
}
public?String get戶籍() {
return?戶籍;
}
public?void?set戶籍(String 戶籍) {
this.戶籍?= 戶籍;
}
}
總結
以上是生活随笔為你收集整理的java学生管理系统代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker swarm 实战-部署wo
- 下一篇: TensorFlow Keras 官方