📄 studentdao.java
字号:
/*
* Created on 2005-4-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package dao;
import java.io.*;
import java.util.*;
import vo.*;
import exception.*;
/**
* @author ywl
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class StudentDAO {
private ArrayList studentList=new ArrayList();
public StudentDAO(){
//init();
}
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("duplicate"));
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("not found"));
}
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("not found"));
}
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;
}
if(foundIt)
return stud;
else
throw(new NotFoundException("not found"));
}
public void init(){
studentList.add(new StudentVo("200360101","a",21));
studentList.add(new StudentVo("200360102","b",22));
studentList.add(new StudentVo("200360103","c",23));
studentList.add(new StudentVo("200360104","d",24));
}
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]);
StudentVo stud=new StudentVo(num,name,age);
studentList.add(stud);
}
br.close();
fr.close();
}
catch(FileNotFoundException e){
System.out.println("erro "+e);
}
catch(IOException e){
System.out.println("Erro "+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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -