📄 studentdao.java
字号:
import java.io.*;
import java.util.*;
public class StudentDAO{
private ArrayList studentList=new ArrayList();
private ArrayList TempList=new ArrayList();
public StudentDAO(){
}
public void addNew(StudentVo student)throws DuplicateException{
boolean duplicate=false;
Iterator itr=studentList.iterator();
while(itr.hasNext()){
StudentVo stud=(StudentVo)itr.next();
if(stud.getNum().equals(student.getNum()))
duplicate=true;
}
if(duplicate)
throw(new DuplicateException("操作失败:该学生学号记录已存在系统中!!!\n"));
else
studentList.add(student);
}
public void update(StudentVo student)throws NotFoundException{
boolean foundIt=false;
String num=student.getNum();
Iterator itr=studentList.iterator();
while(itr.hasNext()){
StudentVo stud=(StudentVo)itr.next();
if(stud.getNum().equals(num)){
foundIt=true;
itr.remove();
break;
}
}
if(foundIt)
studentList.add(student);
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
public void delete(StudentVo student)throws NotFoundException{
boolean foundIt=false;
String num=student.getNum();
Iterator itr=studentList.iterator();
while(itr.hasNext()){
StudentVo stud=(StudentVo)itr.next();
if(stud.getNum().equals(num)){
foundIt=true;
itr.remove();
break;
}
}
if(foundIt)
return;
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
public StudentVo search(String num)throws NotFoundException{
StudentVo stud=null;
boolean foundIt=false;
Iterator itr=studentList.iterator();
while(itr.hasNext()){
stud=(StudentVo)itr.next();
if(stud.getNum().equals(num))
{ foundIt=true;
break;}
}
if(foundIt)
return stud;
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
public void searchCourse(String course)throws NotFoundException{
StudentVo stud=null;
boolean foundIt=false;
int sum=0;
Iterator itr=studentList.iterator();
while(itr.hasNext())
{
foundIt=false;
stud=(StudentVo)itr.next();
if(stud.getCourse().equals(course))
foundIt=true;
if(foundIt)
{
stud.print();sum++;
}
}
if(sum!=0)
System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~参加"+course+"考试的共有"+sum+"人~~~~~~~~~~~~~~~~~~~~~~~ \n");
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
public void searchMajor(String major)throws NotFoundException{
StudentVo stud=null;
boolean foundIt=false;
int sum=0;
Iterator itr=studentList.iterator();
while(itr.hasNext())
{
foundIt=false;
stud=(StudentVo)itr.next();
if(stud.getMajor().equals(major))
foundIt=true;
if(foundIt)
{
stud.print();sum++;
}
}
if(sum!=0)
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~记录有"+major+"专业考试成绩共有"+sum+"人~~~~~~~~~~~~~~~~~~~~~~~\n");
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
public void init(){
studentList.add(new StudentVo("200360101","a",21,"a1","a2",61));
studentList.add(new StudentVo("200360102","b",22,"b1","b2",62));
studentList.add(new StudentVo("200360103","c",23,"c1","c2",63));
studentList.add(new StudentVo("200360104","d",24,"d1","d2",64));
}
public void init(String fileName){
FileReader fr=null;
BufferedReader br=null;
String str=null;
try{
fr=new FileReader(fileName);
br=new BufferedReader(fr);
while((str=br.readLine())!=null){
String[] strLine=null;
try{
strLine=str.split(",");
}
catch(Exception e){
}
finally{
}
String num=strLine[0];
String name=strLine[1];
int age=Integer.parseInt(strLine[2]);
String major=strLine[3];
String course=strLine[4];
int score=Integer.parseInt(strLine[5]);
StudentVo stud=new StudentVo(num,name,age,major,course,score);
studentList.add(stud);
}
br.close();
fr.close();
}
catch(FileNotFoundException e){
System.out.println("erro"+e);
}
catch(IOException e){
System.out.println("erro"+e);
}
}
public void printAll()throws NotFoundException{
boolean foundIt=false;
String num;
String name;
int age;
String major;
String course;
int score;
Iterator itr=studentList.iterator();
while(itr.hasNext()){
StudentVo stud=(StudentVo)itr.next();
num=stud.getNum();
name=stud.getName();
age=stud.getAge();
major=stud.getMajor();
course=stud.getCourse();
score=stud.getScore();
System.out.println("\n学号:"+num+",姓名:"+name+",年龄:"+age+",专业:"+major+",学科:"+course+",成绩:"+score+"\n");
foundIt=true;
}
if(foundIt)
return;
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
public void save(String fileName){
Iterator itr=studentList.iterator();
try{
BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
while(itr.hasNext()){
StudentVo stud=(StudentVo)itr.next();
String num=stud.getNum();
String name=stud.getName();
int age=stud.getAge();
String major=stud.getMajor();
String course=stud.getCourse();
int score=stud.getScore();
String s=(num+","+name+","+age+","+major+","+course+","+score+"\r\n");
bw.write(s);
}
bw.close();
}
catch(IOException e){
System.out.println("Error"+e);
}
}
public boolean isExit(StudentVo student){
boolean ok=false;
Iterator itr=studentList.iterator();
while(itr.hasNext()){
StudentVo stud=(StudentVo)itr.next();
if(stud.getNum().equals(student.getNum()))
ok=true;
}
return ok;
}
public void arrayscore()throws IOException{
StudentVo Tempstud1;
StudentVo Tempstud2;
int sum=0;
boolean foundIt=false;
int min;
int a;
int b;
int index;
int scan;
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~按成绩顺序排序~~~~~~~~~~~~~~~~~~~~~~~");
try
{
System.out.println("请输入所需要排列的科目:");
String s3;
BufferedReader in6=new BufferedReader(new InputStreamReader(System.in));
s3=in6.readLine();
Iterator itr=studentList.iterator();
while(itr.hasNext())
{
foundIt=false;
StudentVo stud=(StudentVo)itr.next();
if(stud.getCourse().equals(s3))
foundIt=true;
if(foundIt)
{
TempList.add(stud);
sum++;
}
}
if(sum!=0)
{
for(index=0;index<sum-1;index++)
{
for(scan=index+1;scan<sum;scan++)
{
Tempstud2=(StudentVo)TempList.get(scan);
Tempstud1=(StudentVo)TempList.get(index);
if(Tempstud1.getScore()<Tempstud2.getScore())
{ a=index+1;
TempList.add(index,Tempstud2);
TempList.remove(a);
b=scan+1;
TempList.add(scan,Tempstud1);
TempList.remove(b);
}
}
}
Iterator xiao=TempList.iterator();
while(xiao.hasNext())
{
StudentVo stud=(StudentVo)xiao.next();
stud.print();
}
}
else
throw(new NotFoundException("操作失败:未找到!!!\n"));
}
catch(NotFoundException e)
{
System.out.print(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -