Java基于集合的学生信息管理系统
清内存,希望各位大佬别介意
ArrayList
学生类
package 大作业;
public class Student {
String id,name,phone,dormitory,classroom;
Student(){
}
Student(String id,String name,String phone,String dormitory,String classroom){
this.id=id;
this.name=name;
this.phone=phone;
this.dormitory=dormitory;
this.classroom=classroom;
}
public void setId(String id) {
this.id=id;
}
String getId(){
return id;
}
public void setName(String name) {
this.name=name;
}
String getName(){
return name;
}
public void setPhone(String phone) {
this.phone=phone;
}
String getPhone(){
return phone;
}
public void setDormitory(String dormitory) {
this.dormitory=dormitory;
}
String getDormitory(){
return dormitory;
}
public void setClassroom(String classroom) {
this.classroom=classroom;
}
String getClassroom(){
return classroom;
}
String getMessage() {
return "学号:"+id+" 姓名:"+name+" 电话号码:"+phone+" 宿舍号:"+dormitory+" 班级:"+classroom;
}
}
Manage类
package 大作业;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
public class Manage {
public static void main(String[] args) {
ArrayList<Student> array=new ArrayList<Student>();
//读取文件
File fr=new File("学生档案.txt");
try {
Reader in=new FileReader(fr);
BufferedReader br=new BufferedReader(in);
String b=null;
int count=0;
while((b=br.readLine())!=null) {
count++;
String str[]=b.split(",");
Student st=new Student(str[0],str[1],str[2],str[3],str[4]);
array.add(st);
}
//System.out.println("档案学生个数"+count);
//System.out.println("集合中学生个数"+array.size());
}
catch(Exception ioe) {
System.out.println(ioe);
}
while(true){ //while(true)就是无限循环语句。因为括号中的条件为true,所以永远不会跳出循环,除非语句块中有break语句才都能跳出循环。
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("6.退出");
System.out.println("请输入你的选择:");
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
switch(line) {
case"1":
addstudent(array);
write(array);
break;
case"2":
remove(array);
write(array);
break;
case"3":
change(array);
write(array);
break;
case"4":
find(array);
write(array);
break;
case"5":
System.out.println("以下为所有学生的基本信息");
Iterator<Student> iter=array.iterator();//用迭代器遍历排序后的list
while(iter.hasNext()) {
Student st1=iter.next();
System.out.println(st1.getMessage());
}
break;
case"6":
System.out.println("谢谢使用!");
System.exit(0); //system.exit(int status);正常退出。status为0时为正常退出程序,也就是结束当前正在运行中的java虚拟机。
}
}
}
//增加学生信息
public static void addstudent(ArrayList<Student> array) {
int i,k=0;
Scanner sc1=new Scanner(System.in);
System.out.println("请输入学号:");
String id=sc1.nextLine();
for(i=0;i<array.size();i++) {
Student s=array.get(i);
if(s.getId().equals(id)) {
Scanner sc=new Scanner(System.in);
System.out.println("输入的学号有重复!\n重新输入请扣1,返回欢迎界面请扣0");//输入错误重新输入,输入重复返回界面查询或修改
String sc2=sc.nextLine();
switch(sc2) {
case"0":
Manage.main(null);
break;
case"1":
addstudent (array);
break;
}
k++;
break;
}
}
if(k==0) {
System.out.println("请输入学生姓名:");
String name=sc1.nextLine();
System.out.println("请输入学生电话号码:");
String phone=sc1.nextLine();
System.out.println("请输入学生宿舍号:");
String dormitory=sc1.nextLine();
System.out.println("请输入学生班级:");
String classroom=sc1.nextLine();
Student s=new Student();
s.setId(id);
s.setName(name);
s.setPhone(phone);
s.setDormitory(dormitory);
s.setClassroom(classroom);
System.out.println("该学生完整信息为\n"+s.getMessage());
Scanner sc3=new Scanner(System.in);
System.out.println("确定增加请扣1,重新增加请扣0");
String sc4=sc3.nextLine();
switch(sc4) {
case"0":
addstudent (array);
break;
case"1":
array.add(s);
System.out.println("添加学生成功!");
break;
}
}
}
//删除学生信息
public static void remove(ArrayList<Student> array) {
Scanner sc1=new Scanner(System.in);
System.out.println("请输入要删除学生的学号:");
String id=sc1.nextLine();
int i,k;
for(i=k=0;i<array.size();i++) {
Student s=array.get(i);
if(s.getId().equals(id)) {
System.out.println("删除学生的信息为:"+s.getMessage());
Scanner sc=new Scanner(System.in);
System.out.println("确定删除请扣1,重新输入请扣0");
String sc2=sc.nextLine();
switch(sc2) {
case"0":
remove(array);
k++;
break;
case"1":
array.remove(i);
System.out.println("删除成功!");
k++;
break;
}
}
}
if(k==0) {
Scanner sc3=new Scanner(System.in);
System.out.println("找不到该学生!\n重新输入请扣1,返回欢迎界面请扣0");
String sc4=sc3.nextLine();
switch(sc4) {
case"0":
Manage.main(null);
break;
case"1":
remove(array);
break;
}
}
}
//修改学生信息
public static void change(ArrayList<Student> array) {
Scanner sc1=new Scanner(System.in);
System.out.println("请输入要修改学生的学号:");
String id=sc1.nextLine();
int i,k;
for(i=k=0;i<array.size();i++) {
Student s=array.get(i);
if(s.getId().equals(id)) {
System.out.println("该学生的基本信息为:"+s.getMessage());
Scanner sc=new Scanner(System.in);
System.out.println("请输入要修改内容的序号:");
System.out.println("1.学号");
System.out.println("2.姓名");
System.out.println("3.电话号码");
System.out.println("4.宿舍号");
System.out.println("5.班级");
String sc2=sc.nextLine();
Scanner sc3=new Scanner(System.in);
System.out.println("请输入修改内容:");
String date=sc3.nextLine();
switch(sc2) {
case"1":
s.setId(date);
break;
case"2":
s.setName(date);
break;
case"3":
s.setPhone(date);
break;
case"4":
s.setDormitory(date);
break;
case"5":
s.setClassroom(date);
break;
}
System.out.println("修改成功!\n修改后学生信息为:"+s.getMessage());
k++;//缺点,每改一次都会退出,返回到欢迎页面,没办法一次性该多个信息
}
}
if(k==0) {
Scanner sc5=new Scanner(System.in);
System.out.println("找不到该学生!\n重新输入请扣1,返回欢迎界面请扣0");
String sc4=sc5.nextLine();
switch(sc4) {
case"0":
Manage.main(null);
break;
case"1":
change(array);//重新回到change方法
break;
}
}
}
//查询学生信息
public static void find(ArrayList<Student> array) {
int i,k=0;
Scanner sc1=new Scanner(System.in);
System.out.println("请输入查询学生的学号:");
String id=sc1.nextLine();
for(i=0;i<array.size();i++) {
Student s=array.get(i);
if(s.getId().equals(id)) {
System.out.println("学生信息为:"+s.getMessage());
k++;//找到一个匹配的,k计数一次
break;
}
}
if(k==0) {//k==0,没有找到
Scanner sc=new Scanner(System.in);
System.out.println("找不到该学生!\n重新输入请扣1,返回欢迎界面请扣0");
String sc2=sc.nextLine();
switch(sc2) {
case"0":
Manage.main(null);
break;
case"1":
find(array);
break;
}
}
}
//写成学生信息
public static void write(ArrayList<Student> array) {
File fw=new File("学生档案.txt");
try {
Writer out=new FileWriter(fw);
BufferedWriter bw=new BufferedWriter(out);
Iterator<Student> iter=array.iterator();//用迭代器遍历排序后的list
while(iter.hasNext()) {
Student st=iter.next();
String ss=st.id+","+st.name+","+st.phone+","+st.dormitory+","+st.classroom;
System.out.println(ss);
bw.write(ss);
bw.newLine();
}
bw.close();
out.close();
}
catch(Exception ioe) {
System.out.println(ioe);
}
}
}
说明书链接: https://pan.baidu.com/s/17CkrL28M2rlvIQC_-vtVlA 提取码: e1ve