package com.Brooke01;
public class Student {
private String name;
private String id;
private String age;
private String address;
public Student() {
}
public Student(String name, String id, String age, String address) {
this.name = name;
this.id = id;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
package com.Brooke01;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
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 choiceStr = sc.nextLine();
switch(choiceStr) {
case "1":
findStudent(array);
break;
case "2":
addStudent(array);
break;
case "3":
delectStudent(array);
break;
case "4":
updateStudent(array);
break;
default:
System.out.println("谢谢您的使用!");
System.exit(0);
break;
}
}
}
public static void findStudent(ArrayList<Student> array) {
if(array.size() == 0){
System.out.println("对不起,暂无学生信息!");
}
else{
System.out.println("学号\t姓名\t年龄\t地址");
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
}
}
}
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String id;
while(true) {
System.out.println("请输入学生学号:");
id = sc.nextLine();
boolean flag = true;
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
if(id.equals(stu.getId())) {
flag = false;
System.out.println("您输入的学号已经被占用,请重新输入!");
}
}
if(flag == true) {
break;
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
System.out.println("请输入学生居住地:");
String address = sc.nextLine();
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
array.add(stu);
System.out.println("添加成功!");
}
public static void delectStudent(ArrayList<Student> array) {
System.out.println("请输入你要删除的学生学号:");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
int flag = -1;
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
if(id.equals(stu.getId())) {
array.remove(x);
flag = x;
}
}
if(flag == -1){
System.out.println("对不起,您输入的学号不存在!");
}
else {
System.out.println("删除成功!");
}
}
public static void updateStudent(ArrayList<Student> array) {
System.out.println("请输入你要修改的学生学号:");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
int flag = -1;
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
if(id.equals(stu.getId())) {
flag = x;
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();
stu.setId(newId);
stu.setName(newName);
stu.setAge(newAge);
stu.setAddress(newAddress);
array.set(x,stu);
}
}
if(flag == -1) {
System.out.println("对不起,您输入的学生学号不存在!");
}
else {
System.out.println("修改成功!");
}
}
}
package com.Brooke01;
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 {
String studentFile = "StudentFile.txt";
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 choiceStr = sc.nextLine();
switch(choiceStr) {
case "1":
findStudent(studentFile);
break;
case "2":
addStudent(studentFile);
break;
case "3":
delectStudent(studentFile);
break;
case "4":
updateStudent(studentFile);
break;
default:
System.out.println("谢谢您的使用!");
System.exit(0);
break;
}
}
}
public static void readData(String studentFile,ArrayList<Student> array) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(studentFile));
String line;
while((line = br.readLine()) != null) {
String[] strArray = line.split(",");
Student stu = new Student();
stu.setId(strArray[0]);
stu.setName(strArray[1]);
stu.setAge(strArray[2]);
stu.setAddress(strArray[3]);
array.add(stu);
}
br.close();
}
public static void writeData(String studentFile,ArrayList<Student> array) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(studentFile));
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
StringBuilder sb = new StringBuilder();
sb.append(stu.getId()).append(",").append(stu.getName()).append(",").append(stu.getAge()).append(",").append(stu.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
public static void findStudent(String studentFile) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readData(studentFile,array);
if(array.size() == 0){
System.out.println("对不起,暂无学生信息!");
}
else{
System.out.println("学号\t姓名\t年龄\t地址");
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
}
}
}
public static void addStudent(String studentFile) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readData(studentFile,array);
Scanner sc = new Scanner(System.in);
String id;
while(true) {
System.out.println("请输入学生学号:");
id = sc.nextLine();
boolean flag = true;
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
if(id.equals(stu.getId())) {
flag = false;
System.out.println("您输入的学号已经被占用,请重新输入!");
}
}
if(flag == true) {
break;
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
System.out.println("请输入学生居住地:");
String address = sc.nextLine();
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
array.add(stu);
writeData(studentFile, array);
System.out.println("添加成功!");
}
public static void delectStudent(String studentFile) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readData(studentFile,array);
System.out.println("请输入你要删除的学生学号:");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
int flag = -1;
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
if(id.equals(stu.getId())) {
array.remove(x);
flag = x;
}
}
if(flag == -1){
System.out.println("对不起,您输入的学号不存在!");
}
else {
writeData(studentFile, array);
System.out.println("删除成功!");
}
}
public static void updateStudent(String studentFile) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
readData(studentFile,array);
System.out.println("请输入你要修改的学生学号:");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
int flag = -1;
for(int x=0;x<array.size();x++) {
Student stu = array.get(x);
if(id.equals(stu.getId())) {
flag = x;
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();
stu.setId(newId);
stu.setName(newName);
stu.setAge(newAge);
stu.setAddress(newAddress);
array.set(x,stu);
}
}
if(flag == -1) {
System.out.println("对不起,您输入的学生学号不存在!");
}
else {
writeData(studentFile, array);
System.out.println("修改成功!");
}
}
}