📄 student.java
字号:
package baseClass;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Student implements Serializable{
private String classId;
private String studentId;
private String studentNmae;
private static final long serialVersionUID = 1L;
public Student()
{
}
/**
* Student Constructors
* @param pcid class id
* @param psid student id
* @param psname student name
*/
public Student(String psid,String psname,String pcid)
{
classId=pcid;
studentId=psid;
studentNmae=psname;
}
/**
* return a class obeject in allClass by id
* @param id classid
* @param allClass student arraylist
* @return class object if not exsit return null
*/
public Class getClassByClassId(String id,Class[] allClass)
{
for(int i=0;i<allClass.length;i++)
{
if(allClass[i].getClassId()==id)
{
return allClass[i];
}
}
return null;
}
/**
*
* @return
*/
public ArrayList loadAllStudent()
{
ArrayList alldate=new ArrayList();
try
{
String[] classmessage=new String[2];
File read = new File(GlobalValue.STUDENTDATESOURSE);//
if(!read.exists())
read.createNewFile();
InputStreamReader isr = new InputStreamReader(
new FileInputStream(read),GlobalValue.DEFAULTCODINGSHEME);
BufferedReader br = new BufferedReader(isr);
String temp = null;
temp = br.readLine();
// int i=0,j=0;
// if(temp != null)//// {
//
// j=Integer.parseInt(temp);
// temp = br.readLine();
// }
while(temp != null)//
{
classmessage=temp.split(GlobalValue.SPLITCHAR);
Student tempstudent=new Student();
tempstudent.studentId=classmessage[0];
tempstudent.studentNmae=classmessage[1];
tempstudent.classId=classmessage[2];
alldate.add(tempstudent);
//i++;
temp = br.readLine();
}
br.close();
alldate.trimToSize();
//showMessage(alldate);
return alldate;
}
catch(FileNotFoundException e){ //
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
catch(Exception e){
System.out.println (e);
}
return null;
}
/**
* * @param newstudent * @return
*/
public int addnewStudent(Student newstudent)
{
if(isNumeric(newstudent.classId)&&isNumeric(newstudent.studentId))
{
//
ArrayList allstudent=loadAllStudent();
for(int i=0;i<allstudent.size();i++)
{
if(newstudent.myequals((Student)allstudent.get(i)))
{
return 2;
}
}
allstudent.add(newstudent);
allstudent.trimToSize();
Studentcomprator classco=new Studentcomprator();
Collections.sort(allstudent,classco);
saveAllStudent(allstudent);
return 3;
}
else
{
return 0;
}
}
public int deleteStudentBystudenId(String studentid)
{
//
if(studentid==null)
{
return -2;
}
try
{
ArrayList allstudent=loadAllStudent();
for(int i=0;i<allstudent.size();i++)
{
String allstudentidfor=((Student)allstudent.get(i)).studentId;
if(studentid.equals(allstudentidfor))
{
allstudent.remove(i);
allstudent.trimToSize();
Studentcomprator classco=new Studentcomprator();
Collections.sort(allstudent,classco);
saveAllStudent(allstudent);
return 2;
}
}
return 1;
}
catch(Exception e)
{
return -1;
}
}
/**
*
* @param allstudent
* @return
*/
public boolean myequals(Student allstudent)
{
//if(classId==newclass.classId&&studentNmae==newclass.studentNmae&&newclass.classId==classId)
if(studentId.equals(allstudent.studentId))
{
return true;
}
else
{
return false;
}
}
/**
*
* @param allclass
*/
public void saveAllStudent(ArrayList allstudent)
{
try
{
allstudent.trimToSize();
Studentcomprator classco=new Studentcomprator();
Collections.sort(allstudent,classco);
File write = new File(GlobalValue.STUDENTDATESOURSE);
if(!write.exists())
write
.createNewFile();
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(write,false), GlobalValue.DEFAULTCODINGSHEME);
//int i=0;
if(allstudent!=null)// {
//bw.write(allstudent.size()+"\n");
for(int j=0;j<allstudent.size();j++)//
{
Student tempstudent=((Student)allstudent.get(j));
String temp=tempstudent.studentId+GlobalValue.SPLITCHAR+tempstudent.studentNmae+GlobalValue.SPLITCHAR+tempstudent.classId+"\n";
osw.write(temp);
}
}
else
{
//bw.write(0+"\n");
}
osw.close();
}
catch(FileNotFoundException e){ //
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
catch(Exception e){
System.out.println (e);
}
}
/**
*
* @param classArray
*/
public void showMessage(ArrayList classArray)
{
if(classArray!=null)
{
for(int i=0;i<classArray.size();i++)
{
System.out.println("stuedent id:"+((Student)classArray.get(i)).studentId+" student name:"+((Student)classArray.get(i)).studentNmae+" classid of student:"+((Student)classArray.get(i)).classId);
}
}
else
{
System.out.println("date null!");
}
}
/**
*
* @return
*/
public String exportalldate()
{
try
{
File file = new File(GlobalValue.STUDENTDATESOURSE);//
if(!file.exists())
file.createNewFile();
StringBuffer sb=new StringBuffer();
BufferedReader br = new BufferedReader(
new FileReader(file));
//br.readLine();//
String temp=null;
temp=br.readLine();
while(temp!=null)
{
sb.append(temp+"\n");
temp=br.readLine();
//
}
br.close();
return sb.toString();
}
catch(FileNotFoundException e){ //
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
catch(Exception e){
System.out.println (e);
}
return "export erro";
}
public static void main(String[] arg)
{
Student temp=new Student();
ArrayList message=temp.loadAllStudent();
temp.saveAllStudent(message);
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentNmae() {
return studentNmae;
}
public void setStudentNmae(String studentNmae) {
this.studentNmae = studentNmae;
}
/**
*
* @param sourseclassid
* @param targetid
* @return
*/
public void changestudentclassinfo(String sourseclassid,String targetid)
{
ArrayList allstudent=loadAllStudent();
for(int i=0;i<allstudent.size();i++)
{
Student temp=(Student)allstudent.get(i);
if(temp.getClassId().equals(sourseclassid))
{
temp.setClassId(targetid);
}
}
saveAllStudent(allstudent);
}
/**
* judge singlestudent is in allstudent or not
* @param allclass Student ArrayList
* @param singlestudent the jugded Student
* @return
*/
public boolean singleclassinclassarray(ArrayList allstudent,Student singlestudent)
{
for(int i=0;i<allstudent.size();i++)
{
if(singlestudent.myequals((Student)allstudent.get(i)))
{
return true;
}
}
return false;
}
/**
* Impotcontent will import the contents of the class data files
* @param impotcontent
* @return return import results
*/
public String importstudentdate(String impotcontent)
{
StringBuffer importinfo=new StringBuffer();
int importnum=0;
if(impotcontent==null)
{
importinfo.append("Import data can not be empty, please check the division symbol coding and data separator per line number is to meet the requirements");
}
else
{
try
{
String[] impotrowsplit=impotcontent.split("\n");
ArrayList allclass=loadAllStudent();
ArrayList adderrostudents=new ArrayList();
ArrayList adderroforclassid=new ArrayList();
ArrayList adderroidisnotnum=new ArrayList();
ArrayList adderroidisrowerro=new ArrayList();//string
for(int i=0;i<impotrowsplit.length;i++)
{
String[] classinfo=impotrowsplit[i].split(GlobalValue.SPLITCHAR);
if(classinfo.length==3)//exclude redundant data data
{
Student tempclass=new Student(classinfo[0],classinfo[1],classinfo[2]);
if((!(isNumeric(tempclass.classId)))&&(!(isNumeric(tempclass.studentId))))//id must be number
{
adderroidisnotnum.add(tempclass);
}
else
{
if(singleclassinclassarray(allclass,tempclass))//id repeat willnot be add
{
adderrostudents.add(tempclass);
}
else//id not repeat
{
if(!new Class().isexistclass(tempclass.classId))//class not exsit
{
adderroforclassid.add(tempclass);
}
else
{
allclass.add(tempclass);
importnum++;
}
}
}
}//row number >3
else
{
adderroidisrowerro.add(impotrowsplit[i]);
//return importinfo.append("").toString();
}
}//for
allclass.trimToSize();
Studentcomprator classco=new Studentcomprator();
Collections.sort(allclass,classco);
saveAllStudent(allclass);
//Organization feedback string
importinfo.append(importnum+"data import successful !\n");
if(adderroforclassid.size()>0)//class impor erro
{
importinfo.append("Following the failure to import content\nReaso:classiderro or the id is not exsit\n");
for(int j=0;j<adderroforclassid.size();j++)
{
Student erro=(Student)adderroforclassid.get(j);
importinfo.append(erro.getStudentId()+GlobalValue.SPLITCHAR+erro.getStudentNmae()+GlobalValue.SPLITCHAR+erro.getClassId());
importinfo.append("\n");
}
}
if(adderrostudents.size()>0)//id repeat
{
importinfo.append("Following the failure to import content\nReason:id repeat\n");
for(int j=0;j<adderrostudents.size();j++)
{
Student erro=(Student)adderrostudents.get(j);
importinfo.append(erro.getStudentId()+GlobalValue.SPLITCHAR+erro.getStudentNmae()+GlobalValue.SPLITCHAR+erro.getClassId());
importinfo.append("\n");
}
}
if(adderroidisnotnum.size()>0)
{
importinfo.append("Following the failure to import content \n Reason??id is not number\n");
for(int j=0;j<adderroidisnotnum.size();j++)
{
Student erro=(Student)adderroidisnotnum.get(j);
importinfo.append(erro.getStudentId()+GlobalValue.SPLITCHAR+erro.getStudentNmae()+GlobalValue.SPLITCHAR+erro.getClassId());
importinfo.append("\n");
}
}
if(adderroidisrowerro.size()>0)
{
importinfo.append("Data format is wrong, please check the division symbol coding and data separator per line number is in line with requirements of each line and only have two ',' ");
for(int j=0;j<adderroidisrowerro.size();j++)
{
importinfo.append(adderroidisrowerro.get(j));
importinfo.append("\n");
}
}
}//try
//other erro ,exampel date fprmat wrong
catch(Exception e)
{
importinfo.append("Data format is wrong, please check the division symbol coding and data separator per line number is to meet the requirements, and when is the number");
}
}
return importinfo.toString();
}
/**
* judge a string is a number or not
* @param str the string need judge
* @return number return true not number return false
*/
public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )
{
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -