本文实例为大家分享了java实现学生宿舍管理系统的具体代码,供大家参考,具体内容如下
学生类代码
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package dormitory;
public class Student { private String id; private String name; private String sex; private String dormid; 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 getSex() { return sex; } public void setSex(String sex) { this .sex = sex; } public String getDormid() { return dormid; } public void setDormid(String dormid) { this .dormid = dormid; }
} |
主操作代码
IntailStudent.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
package dormitory; import java.awt.List; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner;
import javax.print.DocFlavor.INPUT_STREAM; import javax.swing.event.ListSelectionEvent;
import org.omg.PortableInterceptor.IORInterceptor;
public class InitailStudent { private static int n= 0 ; private static Student[] stu= new Student[ 100 ]; //主函数 public static void main(String[] args) throws IOException { boolean a= false ; boolean b= false ; InitailStudent student= new InitailStudent(); student.judge(a, b); } //登入函数 private void judge( boolean a, boolean b) throws IOException { do { System.out.println( "欢迎进入登入页面!" ); Scanner input= new Scanner(System.in); System.out.println( "请输入账号:" ); String account=input.nextLine(); System.out.println( "请输入密码:" ); String code=input.nextLine(); a=account.equals( "admin" ); b=code.equals( "admin" ); } while (!(a== true &&b== true ));
Menu(); }
//系统菜单页面 private void Menu() throws IOException{ Scanner input= new Scanner(System.in); System.out.println( "------ 欢迎进入宿舍管理系统 ------" ); System.out.println( "------ 请选择下列操作 ------" ); System.out.println( "--- 1.显示所有学生信息 ---" ); //Show() System.out.println( "--- 2.查询学生信息 ---" ); //Find() System.out.println( "--- 3.增加学生信息 ---" ); //Add() System.out.println( "--- 4.修改学生信息 ---" ); //Renew() System.out.println( "--- 5.删除学生信息 ---" ); //Delete() System.out.println( "--- 0.退出系统 ---" ); System.out.println( "请输入1~5:" ); int a=input.nextInt(); while (a< 0 ||a> 5 ) { System.out.println( "输入有误,请重新输入:" ); a=input.nextInt(); } switch (a) { case 1 : Show(); break ; case 2 : Find(); break ; case 3 : Add(); break ; case 4 : Renew(); break ; case 5 : Delete(); break ; case 0 : System.out.println( "成功退出系统!" ); System.exit( 0 ); break ; } }
//显示学生的全部信息 private void Show() throws IOException{ System.out.println( "您总录入的信息如下:" ); System.out.println( "*****************************" ); BufferedReader br= new BufferedReader( new FileReader( "student.txt" )); String line; while ((line=br.readLine())!= null ){ System.out.println(line); } br.close(); System.out.println( "\n\r" ); System.out.println( "此次录入的信息为" ); System.out.println( "*****************************" ); int i; for (i= 0 ;i<n;i++) { System.out.println( "学号:" +stu[i].getId()+ "\t姓名:" +stu[i].getName()+ "\t性别:" +stu[i].getSex()+ "\t宿舍号:" +stu[i].getDormid()); } System.out.println( "返回主菜单" ); Menu();
}
//查询学生信息 private void Find() throws IOException{ ArrayList<ArrayList<String>> lists = new ArrayList<>(); BufferedReader br= new BufferedReader( new FileReader( "student.txt" )); String line; ArrayList<String> list = new ArrayList<>(); ArrayList<String> validlist = new ArrayList<>(); while ((line=br.readLine())!= null ){ list.add(line.toString()); } br.close(); for ( int i = 0 ;i<list.size();i++)
if (i!= 0 &&list.get(i- 1 ).startsWith( "学号" )){ validlist.add(list.get(i));
} for (String string : validlist) { String[] split = string.split( " " ); ArrayList<String> tempString = new ArrayList<>(); for (String string2 : split) { tempString.add(string2); }
lists.add(tempString); } System.out.println( "共有" +lists.size()+ "个学生信息" ); String[][] stu1= new String[lists.size()][ 4 ]; for ( int i= 0 ;i<lists.size();i++) for ( int j= 0 ;j< 4 ;j++){ stu1[i][j]=lists.get(i).get(j); } System.out.println( "请输入学生的学号:" ); Scanner input= new Scanner(System.in); String d=input.next(); for ( int i= 0 ;i<stu1.length;i++) { if (d.equals(stu1[i][ 0 ])) { System.out.println( "查询成功,以下为该学生的信息" ); System.out.println( "学号:" +stu1[i][ 0 ]+ "\t姓名:" +stu1[i][ 1 ]+ "\t性别:" +stu1[i][ 2 ]+ "\t宿舍号:" +stu1[i][ 3 ]); System.out.println( "是否继续查询,否返回菜单,是Y否N" ); String cho=input.next(); char ch=cho.charAt( 0 ); while (ch!= 'Y' &&ch!= 'y' &&ch!= 'N' &&ch!= 'n' ) { System.out.println( "输入有误!请重新输入:" ); cho=input.next(); ch=cho.charAt( 0 ); } while (ch== 'Y' ||ch== 'y' ){ Find(); } while (ch== 'N' ||ch== 'n' ){ Menu(); }
} } System.out.println( "没有找到该学生,是继续输入,否返回菜单,是Y否N" ); String cho=input.next(); char ch=cho.charAt( 0 ); while (ch!= 'Y' &&ch!= 'y' &&ch!= 'N' &&ch!= 'n' ) { System.out.println( "输入有误!请重新输入:" ); cho=input.next(); ch=cho.charAt( 0 ); } while (ch== 'Y' ||ch== 'y' ){ Find(); } while (ch== 'N' ||ch== 'n' ){ Menu(); } }
//增加一个学生 private void Add() throws IOException{ String id; String dormid; String name; String sex; String cho; char ch; stu[n]= new Student(); Scanner input= new Scanner(System.in); if (n== 0 ) {
System.out.println( "您此次还没有录入任何信息,是否录入,是Y否N" ); cho=input.next(); ch=cho.charAt( 0 );
while (ch!= 'Y' &&ch!= 'y' &&ch!= 'N' &&ch!= 'n' ) { System.out.println( "输入有误!请重新输入:" ); cho=input.next(); ch=cho.charAt( 0 ); }
while (ch== 'Y' ||ch== 'y' ){ break ; } while (ch== 'N' ||ch== 'n' ){ Menu(); } } FileWriter fw= new FileWriter( "student.txt" , true ); fw.write( "\r\n" ); fw.write( "学号 姓名 性别 宿舍号 \r\n" ); System.out.println( "请输入学生的学号:" ); id=input.next(); stu[n].setId(id); fw.write(stu[n].getId()+ " " ); System.out.println( "请输入学生的姓名:" ); name=input.next(); stu[n].setName(name); fw.write(stu[n].getName()+ " " ); System.out.println( "请输入学生的性别:" ); sex=input.next(); stu[n].setSex(sex); fw.write(stu[n].getSex()+ " " ); System.out.println( "请输入学生的宿舍号:" ); dormid=input.next(); stu[n].setDormid(dormid); fw.write(stu[n].getDormid()+ " " ); n++; fw.close(); System.out.println( "是否继续添加学生?否返回主菜单,是Y否N" ); cho=input.next(); ch=cho.charAt( 0 ); while (ch!= 'Y' &&ch!= 'y' &&ch!= 'N' &&ch!= 'n' ) { System.out.println( "输入有误!请重新输入:" ); cho=input.next(); ch=cho.charAt( 0 ); } while (ch== 'Y' ||ch== 'y' ){ Add(); } while (ch== 'N' ||ch== 'n' ){ Menu(); } }
//修改学生信息 private void Renew() throws IOException{ ArrayList<ArrayList<String>> lists = new ArrayList<>(); BufferedReader br= new BufferedReader( new FileReader( "student.txt" )); String line; ArrayList<String> list = new ArrayList<>(); ArrayList<String> validlist = new ArrayList<>(); while ((line=br.readLine())!= null ){ list.add(line.toString()); } br.close(); for ( int i = 0 ;i<list.size();i++)
if (i!= 0 &&list.get(i- 1 ).startsWith( "学号" )){ validlist.add(list.get(i));
} for (String string : validlist) { String[] split = string.split( " " ); ArrayList<String> tempString = new ArrayList<>(); for (String string2 : split) { tempString.add(string2); }
lists.add(tempString); } String[][] stu1= new String[lists.size()][ 4 ]; for ( int i= 0 ;i<lists.size();i++) for ( int j= 0 ;j< 4 ;j++){ stu1[i][j]=lists.get(i).get(j); }
int temp= 0 ; boolean flag= false ; System.out.println( "请输入要修改学生的学号:" ); Scanner input= new Scanner(System.in); String d=input.next(); for ( int i= 0 ;i<stu1.length;i++) { while (d.equals(stu1[i][ 0 ])) { temp=i; flag= true ; break ; } } if (!flag) { System.out.println( "输入的学号有误,未找到该学生,是否再次进入修改,是Y,否N" ); String cho1=input.next(); char ch1=cho1.charAt( 0 ); while (ch1!= 'N' &&ch1!= 'n' &&ch1!= 'Y' &&ch1!= 'y' ) { System.out.println( "输入无效,请重新输入:" ); cho1=input.next(); ch1=cho1.charAt( 0 ); } if (ch1== 'y' ||ch1== 'Y' ){ Renew(); } if (ch1== 'N' ||ch1== 'n' ){ System.out.println( "返回主菜单" ); Menu(); } } else { System.out.println( "您要修改的学生的信息如下:" ); System.out.println( "学号:" +stu1[temp][ 0 ]+ "\t姓名:" +stu1[temp][ 1 ]+ "\t性别:" +stu1[temp][ 2 ]+ "\t宿舍号:" +stu1[temp][ 3 ]); System.out.println( "请以下选择要修改的内容:" ); System.out.println( "------ 1.姓名 ------" ); System.out.println( "------ 2.性别 ------" ); System.out.println( "------ 3.宿舍号 ------" ); Scanner input1= new Scanner(System.in); int a=input1.nextInt(); if (a== 1 ) { System.out.println( "请输入新的姓名:" ); String name=input1.next(); stu1[temp][ 1 ]=name; FileWriter fw1= new FileWriter( "student.txt" ); fw1.write( " " ); fw1.close(); FileWriter fw= new FileWriter( "student.txt" , true ); fw.write( "\r\n" + " " + "学生信息表\r\n" ); for ( int i= 0 ;i<stu1.length;i++) { fw.write( "\r\n学号 姓名 性别 宿舍号 \r\n" ); fw.write(stu1[i][ 0 ]+ " " ); fw.write(stu1[i][ 1 ]+ " " ); fw.write(stu1[i][ 2 ]+ " " ); fw.write(stu1[i][ 3 ]+ " " ); } fw.close(); System.out.println( "修改成功!" ); System.out.println( "还要继续修改吗?是继续修改,否返回主菜单,是Y否N" ); String cho1=input1.next(); char ch1=cho1.charAt( 0 ); while (ch1!= 'N' &&ch1!= 'n' &&ch1!= 'Y' &&ch1!= 'y' ) { System.out.println( "输入无效,请重新输入:" ); cho1=input.next(); ch1=cho1.charAt( 0 ); } if (ch1== 'y' ||ch1== 'Y' ){ Renew(); } if (ch1== 'N' ||ch1== 'n' ){ System.out.println( "返回主菜单" ); Menu(); } } else if (a== 2 ) { System.out.println( "请输入新的性别:" ); String sex=input1.next(); stu1[temp][ 2 ]=sex; FileWriter fw1= new FileWriter( "student.txt" ); fw1.write( " " ); fw1.close(); FileWriter fw= new FileWriter( "student.txt" , true ); fw.write( "\r\n" + " " + "学生信息表\r\n" ); for ( int i= 0 ;i<stu1.length;i++) { fw.write( "\r\n学号 姓名 性别 宿舍号 \r\n" ); fw.write(stu1[i][ 0 ]+ " " ); fw.write(stu1[i][ 1 ]+ " " ); fw.write(stu1[i][ 2 ]+ " " ); fw.write(stu1[i][ 3 ]+ " " ); } fw.close(); System.out.println( "修改成功!" ); System.out.println( "还要继续修改吗?是继续修改,否返回主菜单,是Y否N" ); String cho1=input1.next();
char ch1=cho1.charAt( 0 ); while (ch1!= 'N' &&ch1!= 'n' &&ch1!= 'Y' &&ch1!= 'y' ) { System.out.println( "输入无效,请重新输入:" ); cho1=input.next(); ch1=cho1.charAt( 0 ); } if (ch1== 'y' ||ch1== 'Y' ){ Renew(); } if (ch1== 'N' ||ch1== 'n' ){ System.out.println( "返回主菜单" ); Menu(); } } else if (a== 3 ) { System.out.println( "请输入新的宿舍号:" ); String dormid=input1.next(); stu1[temp][ 3 ]=dormid; FileWriter fw1= new FileWriter( "student.txt" ); fw1.write( " " ); fw1.close(); FileWriter fw= new FileWriter( "student.txt" , true ); fw.write( "\r\n" + " " + "学生信息表\r\n" ); for ( int i= 0 ;i<stu1.length;i++) { fw.write( "\r\n学号 姓名 性别 宿舍号 \r\n" ); fw.write(stu1[i][ 0 ]+ " " ); fw.write(stu1[i][ 1 ]+ " " ); fw.write(stu1[i][ 2 ]+ " " ); fw.write(stu1[i][ 3 ]+ " " ); } fw.close(); System.out.println( "修改成功!" ); System.out.println( "还要继续修改吗?是继续修改,否返回主菜单,是Y否N" ); String cho1=input1.next(); char ch1=cho1.charAt( 0 ); while (ch1!= 'N' &&ch1!= 'n' &&ch1!= 'Y' &&ch1!= 'y' ) { System.out.println( "输入无效,请重新输入:" ); cho1=input.next(); ch1=cho1.charAt( 0 ); } if (ch1== 'y' ||ch1== 'Y' ){ Renew(); } if (ch1== 'N' ||ch1== 'n' ){ System.out.println( "返回主菜单" ); Menu(); } } else { System.out.println( "输入有误,请重新输入:" ); Renew(); } } }
//删除学生信息 private void Delete() throws IOException{ ArrayList<ArrayList<String>> lists = new ArrayList<>(); BufferedReader br= new BufferedReader( new FileReader( "student.txt" )); String line; ArrayList<String> list = new ArrayList<>(); ArrayList<String> validlist = new ArrayList<>(); while ((line=br.readLine())!= null ){ list.add(line.toString()); } br.close(); for ( int i = 0 ;i<list.size();i++)
if (i!= 0 &&list.get(i- 1 ).startsWith( "学号" )){ validlist.add(list.get(i));
} for (String string : validlist) { String[] split = string.split( " " ); ArrayList<String> tempString = new ArrayList<>(); for (String string2 : split) { tempString.add(string2); }
lists.add(tempString); } String[][] stu1= new String[lists.size()][ 4 ]; for ( int i= 0 ;i<lists.size();i++) for ( int j= 0 ;j< 4 ;j++){ stu1[i][j]=lists.get(i).get(j); } int temp= 0 ; boolean flag= true ; System.out.println( "请输入你想要删除该学生的学号:" ); Scanner input2= new Scanner(System.in); String d=input2.next(); for ( int i= 0 ;i<stu1.length;i++) { while (d.equals(stu1[i][ 0 ])) { temp=i; flag= true ; break ; } } if (!flag) { System.out.println( "输入的学号有误,未找到该学生,再次进入删除,请重新输入:" ); String cho1=input2.next(); char ch1=cho1.charAt( 0 ); while (ch1!= 'N' &&ch1!= 'n' &&ch1!= 'Y' &&ch1!= 'y' ) { System.out.println( "输入无效,请重新输入:" ); cho1=input2.next(); ch1=cho1.charAt( 0 ); } if (ch1== 'y' ||ch1== 'Y' ){ Delete(); } if (ch1== 'N' ||ch1== 'n' ){ System.out.println( "返回主菜单" ); Menu(); }
} else { System.out.println( "您要删除的学生的信息如下:" ); System.out.println( "学号:" +stu1[temp][ 0 ]+ "\t姓名:" +stu1[temp][ 1 ]+ "\t性别:" +stu1[temp][ 2 ]+ "\t宿舍号:" +stu1[temp][ 3 ]); for ( int i=temp;i<stu1.length- 1 ;i++) { stu1[i]=stu1[i+ 1 ]; } FileWriter fw1= new FileWriter( "student.txt" ); fw1.write( " " ); fw1.close(); FileWriter fw= new FileWriter( "student.txt" , true ); fw.write( "\r\n" + " " + "学生信息表\r\n" ); for ( int i= 0 ;i<stu1.length- 1 ;i++) { fw.write( "\r\n学号 姓名 性别 宿舍号 \r\n" ); fw.write(stu1[i][ 0 ]+ " " ); fw.write(stu1[i][ 1 ]+ " " ); fw.write(stu1[i][ 2 ]+ " " ); fw.write(stu1[i][ 3 ]+ " " ); } fw.close(); System.out.println( "删除该学生信息成功!" ); System.out.println( "---------------------" ); } System.out.println( "还要继续删除吗?是继续删除,否返回主菜单,是Y否N" ); String cho2=input2.next(); char ch2=cho2.charAt( 0 ); while (ch2!= 'N' &&ch2!= 'n' &&ch2!= 'Y' &&ch2!= 'y' ) { System.out.println( "输入无效,请重新输入:" ); cho2=input2.next(); ch2=cho2.charAt( 0 ); } if (ch2== 'y' ||ch2== 'Y' ){ Delete(); } if (ch2== 'N' ||ch2== 'n' ){ System.out.println( "返回主菜单" ); Menu(); }
} } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/DialogD/article/details/77758480