StudentManagerPlus.java
package ly.com;
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 StudentManagerPlus {
public static void main(String[] args) throws IOException {
addMainPage("array.txt");
return;
}
public static void addMainPage(String fileName) throws IOException {
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 inputStr = sc.nextLine();
switch (inputStr) {
case "1": // 1.查看所有學生信息
getAllStudent(fileName);
break;
case "2": // 2.添加學生
addStudent(fileName);
break;
case "3": // 3.刪除學生
removeStudent(fileName);
break;
case "4": // 4.修改學生信息
modifyStudent(fileName);
break;
case "5": // 5.退出
default:
System.out.println("謝謝使用!");
System.exit(0);
break;
}
}
}
public static void readDataFromFile(ArrayList<Student> array, String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while (null != (line = br.readLine())) {
String[] strArr = line.split("\t");
Student stu = new Student(strArr[0], strArr[1], strArr[2], strArr[3]);
array.add(stu);
// System.out.println(line);
}
br.close();
return;
}
public static void writeDataToFile(ArrayList<Student> array, String fileName) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i < array.size(); i++) {
Student stu = array.get(i);
StringBuffer bs = new StringBuffer();
bs.append(stu.getId()).append("\t");
bs.append(stu.getName()).append("\t");
bs.append(stu.getAge()).append("\t");
bs.append(stu.getAddress()).append("\t");
bw.write(bs.toString());
bw.newLine();
bw.flush();
}
bw.close();
return;
}
public static void getAllStudent(String fileName) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readDataFromFile(array, fileName);
if (0 == array.size()) {
System.out.println(fileName + "文件中沒有學生信息");
return;
}
System.out.println("學號\t姓名\t年齡\t住址");
for (int i = 0; i < array.size(); i++) {
Student stu = array.get(i);
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
}
return;
}
public static void addStudent(String fileName) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readDataFromFile(array, fileName);
Scanner sc = new Scanner(System.in);
System.out.println("請輸入需要添加的學生學號");
String strId = sc.nextLine();
if (0 != array.size()) {
while (true) {
int i = 0;
for (; i < array.size(); i++) {
Student stu = array.get(i);
if (strId.equals(stu.getId())) {
System.out.println("該學號已被占用,請重新輸入添加的學生學號");
strId = sc.nextLine();
break;
}
}
if (i == array.size()) {
break;
}
}
}
System.out.println("請輸入學生姓名"); String strName = sc.nextLine();
System.out.println("請輸入學生年齡"); String strAge = sc.nextLine();
System.out.println("請輸入學生住址"); String strAddress = sc.nextLine();
//sc.close();
Student newStu = new Student(strId, strName, strAge, strAddress);
array.add(newStu);
writeDataToFile(array, fileName);
System.out.println("添加學生成功");
return;
}
public static void removeStudent(String fileName) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readDataFromFile(array, fileName);
if (0 == array.size()) {
System.out.println("學生信息管理系統中沒有學生信息,請重新輸入");
return;
}
Scanner sc = new Scanner(System.in);
System.out.println("請輸入要刪除的學生學號");
String strId = sc.nextLine();
int i = 0;
while (true) {
i = 0;
for (; i < array.size(); i++) {
if (true == strId.equals(array.get(i).getId())) {
break;
}
}
if (i == array.size()) {
System.out.println("未查找到該學生學號,請重新輸入需要刪除的學生學號");
strId = sc.nextLine();
} else {
break;
}
}
//sc.close();
array.remove(i);
writeDataToFile(array, fileName);
System.out.println("刪除學生成功");
return;
}
public static void modifyStudent(String fileName) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readDataFromFile(array, fileName);
if (0 == array.size()) {
System.out.println("學生信息系統中沒有學生信息,請重新選擇");
return;
}
Scanner sc = new Scanner(System.in);
System.out.println("請輸入需要修改的學生學號");
String strId = sc.nextLine();
int i = 0;
while (true) {
i = 0;
for (; i < array.size(); i++) {
if (true == strId.equals(array.get(i).getId())) {
break;
}
}
if (i == array.size()) {
System.out.println("未查找到該學生學號,請重新輸入需要修改的學生學號");
strId = sc.nextLine();
} else {
break;
}
}
System.out.println("請輸入新學生學號"); String newId = sc.nextLine();
System.out.println("請輸入新學生姓名"); String newName = sc.nextLine();
System.out.println("請輸入新學生年齡"); String newAge = sc.nextLine();
System.out.println("請輸入新學生住址"); String newAddress = sc.nextLine();
//sc.close();
Student stu = new Student(newId, newName, newAge, newAddress);
array.set(i, stu);
writeDataToFile(array, fileName);
System.out.println("修改學生成功");
return;
}
}
小記:
昨晚為了消除Eclipse中的警告,添加了sc.close();
,今晚運行的時候出問題了。實踐證明,不能加這一句。