studentManager.java
package StudentsManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class StudentsManager {
public static void main(String[] args) throws IOException {
ArrayList<Student> studentArray = new ArrayList<Student>();
Date time = new Date();
System.out.println("time : " + time.toString());
studentArray = getStudetsInfoFromfile();
for (int i=0; i<studentArray.size(); i++) {
System.out.println(studentArray.get(i).getName());
}
while(true) {
System.out.println("欢迎来到学生系统");
System.out.println("1 产看所有学生");
System.out.println("2 添加学生");
System.out.println("3 删除学生");
System.out.println("4 修改学生");
System.out.println("其他输入即退出");
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choice = sc.nextLine();
switch(choice) {
case "1":
findAllStudents(studentArray);
break;
case "2":
addStudent(studentArray);
break;
case "3":
deleteStudent(studentArray);
break;
case "4":
updateStudent(studentArray);
break;
default:
System.out.println("谢谢你的使用!choice = " + choice);
System.out.println("谢谢你的使用!");
System.exit(0);
break;
}
}
}
public static void findAllStudents(ArrayList<Student> arr) {
if (arr.size() == 0) {
System.out.println("不好意思,暂时没有数据,请先添加数据后再查询");
return;
}
System.out.println("学号\t姓名\t年龄\t地址");
for (int i=0; i<arr.size(); i++) {
Student s = arr.get(i);
System.out.println("" + s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
public static void addStudent(ArrayList<Student> arr) throws IOException {
boolean falg = false;
String id = "0";
while(true) {
falg = false;
System.out.println("请输入学生的id");
Scanner sc = new Scanner(System.in);
id = sc.nextLine();
for (int i=0; i<arr.size(); i++) {
Student s = arr.get(i);
if (s.getId().equals(id)) {
System.out.println();
falg = true;
}
}
if (falg) {
System.out.println("您输入的ID已存在请重新输入");
} else {
break;
}
}
System.out.println("请输入学生的姓名");
Scanner sc1 = new Scanner(System.in);
String name = sc1.nextLine();
System.out.println("请输入学生的年龄");
Scanner sc2 = new Scanner(System.in);
String age = sc2.nextLine();
System.out.println("请输入学生的地址");
Scanner sc3 = new Scanner(System.in);
String address = sc3.nextLine();
// Student s = new Student(id, name, age, address);
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
arr.add(s);
saveStudentsInfoTofile(arr);
System.out.println("添加学生成功");
}
public static void deleteStudent(ArrayList<Student> arr) throws IOException {
System.out.println("请输入您要删除的学生的学号");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
boolean hasDelete = false;
for (int i=0; i<arr.size(); i++) {
Student s = arr.get(i);
if (s.getId().equals(id)) {
arr.remove(i);
hasDelete = true;
break;
}
}
saveStudentsInfoTofile(arr);
if (hasDelete)
System.out.println("删除学生成功");
else
System.out.println("您输入的学生id不存在");
}
public static void updateStudent(ArrayList<Student> arr) throws IOException {
System.out.println("请输入要修改的学生的id");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
int index = -1;
for (int i=0; i<arr.size(); i++) {
Student s = arr.get(i);
if (s.getId().equals(id)) {
index = i;
}
}
if (index == -1) {
System.out.println("您要修改的学生id不存在");
}
else {
Student s = arr.get(index);
System.out.println("请输入学生的姓名");
Scanner sc1 = new Scanner(System.in);
String name = sc1.nextLine();
System.out.println("请输入学生的年龄");
Scanner sc2 = new Scanner(System.in);
String age = sc2.nextLine();
System.out.println("请输入学生的地址");
Scanner sc3 = new Scanner(System.in);
String address = sc3.nextLine();
s.setName(name);
s.setAddress(address);
s.setAge(age);
System.out.println("修改修改成功");
}
saveStudentsInfoTofile(arr);
}
public static void saveStudentsInfoTofile(ArrayList<Student> arr) throws IOException {
BufferedWriter bw = null;
String destFile = "./students.text";
File file = new File(destFile);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
StringBuilder data = new StringBuilder();
for (int i = 0; i<arr.size(); i++) {
data.append(arr.get(i).getId());
data.append("," + arr.get(i).getName());
data.append("," + arr.get(i).getAge());
data.append("," + arr.get(i).getAddress());
data.append("\n");
}
System.out.println(data);
bw.write(data.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
System.out.println("文件写入成功");
}
public static ArrayList<Student> getStudetsInfoFromfile() throws FileNotFoundException {
String path = "./students.text";
BufferedReader br = null;
ArrayList<Student> arr = new ArrayList<Student>();
File file = new File(path);
String studentsInfo = "";
try {
if (file.exists()) {
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
br = new BufferedReader(inputStreamReader);
String infoData = null;
while ((infoData = br.readLine()) != null) {
studentsInfo += infoData;
studentsInfo += "\n";
}
// System.out.println("----------" + studentsInfo);
br.close();
String[] infoArr = studentsInfo.split("\n");
for (int i=0; i<infoArr.length; i++) {
String[] stuArr = infoArr[i].split(",");
if (stuArr.length > 0) {
Student s = new Student();
s.setId(stuArr[0]);
s.setName(stuArr[1]);
s.setAge(stuArr[2]);
s.setAddress(stuArr[3]);
arr.add(s);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return arr;
}
}
student.java
package StudentsManager;
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;
}
}