实体类 student:
//实体类 student
public class student {
private String name;
private String age;
private String address;
private String id;
public student(){
}
public student(String id,String name,String age,String address){
this.name=name;
this.address=address;
this.age=age;
this.id=id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return 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;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
主函数:
public static void main(String[] args) throws IOException {
String file="StudentData.txt";
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.退出系统----------");
Scanner sc=new Scanner(System.in);
System.out.println("请输入你的选择: ");
String choice=sc.nextLine();
switch(choice){
case "1":
selectStudent(file);
break;
case "2":
addStudent(file);
break;
case "3":
modifyStudent(file);
break;
case "4":
deleStudent(file);
break;
case "5":
System.out.println("退出系统成功");
System.exit(0);
default:
System.out.println("没有这个选项");
}
}
}
//将文件读入集合
public static void readData(String file,ArrayList<student> array) throws IOException{
BufferedReader br =new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null){
String[] data=line.split(",");
student s=new student();
s.setId(data[0]);
s.setName(data[1]);
s.setAge(data[2]);
s.setAddress(data[3]);
array.add(s);
}
br.close();
}
//将集合内容写到文件
public static void writerData(String file,ArrayList<student> array) throws IOException{
BufferedWriter bw =new BufferedWriter(new FileWriter(file));
for(int x=0;x<array.size();x++){
student s=array.get(x);
StringBuilder sb=new StringBuilder();
sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
//查询所有学生
public static void selectStudent(String file) throws IOException{
ArrayList<student> array=new ArrayList<student>();
readData(file,array);
if(array.size()==0){
System.out.println("没有学生,请先添加学生");
}else{
System.out.println("学号\t\t姓名\t年龄\t居住地");
for(int x=0;x<array.size();x++){
student s= array.get(x);
System.out.println(s.getId()+"\t\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}
//添加学生
public static void addStudent(String file) throws IOException{
ArrayList<student> array=new ArrayList<student>();
readData(file,array);
Scanner sc =new Scanner(System.in);
String id;
while(true){
boolean flag =false;
System.out.println("请输入学生学号");
id=sc.nextLine();
for(int x=0;x<array.size();x++){
student s=array.get(x);
if(s.getId().equals(id)){
flag=true;
break;
}
}
if(flag)
{
System.out.println("学号重复,请重新输入");
}else{
break;
}
}
System.out.println("请输入学生姓名");
String name=sc.nextLine();
System.out.println("请输入学生年龄");
String age=sc.nextLine();
System.out.println("请输入学生居住地");
String address =sc.nextLine();
student s=new student(id,name,age,address);
array.add(s);
writerData(file,array);
System.out.println("添加学生成功");
}
//修改学生
public static void modifyStudent(String file) throws IOException{
ArrayList<student> array=new ArrayList<student>();
readData(file,array);
Scanner sc =new Scanner(System.in);
String id;
System.out.println("请输入要修改的学号");
int index=-1;
id=sc.nextLine();
for(int x=0;x<array.size();x++){
student s=array.get(x);
if(s.getId().equals(id)){
index=x;
break;
}
}
if(index==-1)
{
System.out.println("您要修改的学号不存在,请重新选择");
}else{
System.out.println("请输入新学生姓名");
String name=sc.nextLine();
System.out.println("请输入新学生年龄");
String age=sc.nextLine();
System.out.println("请输入新学生居住地");
String address =sc.nextLine();
student s=new student(id,name,age,address);
array.set(index, s);
System.out.println("修改学生成功");
writerData(file,array);
}
}
//删除学生
public static void deleStudent(String file) throws IOException{
ArrayList<student> array=new ArrayList<student>();
readData(file,array);
Scanner sc=new Scanner(System.in);
String id;
int index=-1;
System.out.println("请输入要删除的学号 :");
id=sc.nextLine();
for(int x=0;x<array.size();x++){
student s=array.get(x);
if(s.getId().equals(id)){
index=x;
break;
}
}
if(index==-1){
System.out.println("您要删除的学生 不存在,请重新选择");
}
else{
array.remove(index);
writerData(file,array);
System.out.println("删除学生成功");
}
}
还需要一个文本文件读写数据