⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.java

📁 JAVA CLONE 用于CLASS范式的CLONE
💻 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 Class implements Serializable{
	private String  classId;//classid,single
	private String  className;//classname,single
    private static final long serialVersionUID = 1L;
	/**
	 * The default constructor
	 */
	public Class()
	{
		classId=null;
		className=null;
	}
	/**
	 * 
	 * @param classid
	 * @param classname
	 */
	public Class(String classid,String classname)
	{
		classId=classid;
		className=classname;
	}
	/**
	 * load data From the data file
	 * @return return all classes by the form of ArrayList
	 */
	public static ArrayList loadAllClass()
	{
		ArrayList list = new ArrayList();
		try
		{
			String[] classmessage=new String[2];
			File read = new File(GlobalValue.CLASSDATESOURSE);//
		    if(!read.exists())   
		    	read.createNewFile();
		    InputStreamReader isr = new InputStreamReader(
		            new FileInputStream(read),GlobalValue.DEFAULTCODINGSHEME);
		      BufferedReader br = new BufferedReader(isr);

			//BufferedReader br = new BufferedReader(new FileReader(read));
			String temp = null;
			temp = br.readLine();
//			int i=0,j=0;
//			if(temp != null)//read the number of date
//			{
//					j=Integer.parseInt(temp);
//					temp = br.readLine();
//			}
			while(temp != null)//read date
			{
				classmessage=temp.split(GlobalValue.SPLITCHAR);
				list.add(new Class(classmessage[0],classmessage[1]));
				temp = br.readLine();
			}

			br.close();
			list.trimToSize();
			//showMessage(list);
			return list;
		}
		catch(FileNotFoundException e){ //file not found
			System.out.println (e);
		}catch(IOException e){
			System.out.println (e);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * The same class category judgement function
	 * @param newclass
	 * @return
	 */
	public boolean myequals(Class newclass)
	{
		if(newclass.classId.equals("0"))
		{
			return false;
		}
		else
		{
			if(classId.equals(newclass.classId))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	/**
	 * add a new class
	 * @param newclass the new class
	 * @return 1 means the of classe is not number  , add failure. 0 Mean data is empty , can add. 
	 * 2 Mean data is not empty .3 means new class adds success
	 */
	public int addnewClass(Class newclass)
	{
		
		if(isNumeric(newclass.classId))
			{
				ArrayList allclass=loadAllClass();
				if(singleclassinclassarray(allclass,newclass))
				{
					return 2;
				}
				allclass.add(newclass);
				allclass.trimToSize();
				Classcomparator classco=new Classcomparator();
				
				Collections.sort(allclass,classco);  
				saveAllClass(allclass);
				return 3;
			}
		return 1;
	}
	/**
	 * delete one class by classid
	 * @param classid the id of class which want to delete
	 * @return -1 delete falile, 1:date is empty ,2:delete sucess,-2:classid is empty
	 */
	public int deleteClassByClassId(String classid)
	{
		
		if(classid==null)
		{
			return -2;
		}
		try
		{
			ArrayList allclass=loadAllClass();
			for(int i=0;i<allclass.size();i++)
			{
				String classidfor=((Class)allclass.get(i)).classId;
				if(classid.equals(classidfor))
				{
					allclass.remove(i);
					allclass.trimToSize();
					Classcomparator classco=new Classcomparator();					
					Collections.sort(allclass,classco); 
					saveAllClass(allclass);
					new Student().changestudentclassinfo(classid,"0");
					return 2;
				}
			}
			return 1;
		}
		catch(Exception e)
		{
			return -1;
		}	
	}
	/**
	 * save all info to date
	 * @param allclass all classes's ArrayList
	 */
	public static void saveAllClass(ArrayList allclass)
	{
		try
		{
			allclass.trimToSize();
			Classcomparator classco=new Classcomparator();					
			Collections.sort(allclass,classco); 
			File write = new File(GlobalValue.CLASSDATESOURSE);
		    if(!write.exists())   
		    	write
		    	.createNewFile();
			//BufferedWriter bw = new BufferedWriter(new FileWriter(write,false));
			OutputStreamWriter osw = new OutputStreamWriter(
                    new  FileOutputStream(write,false), GlobalValue.DEFAULTCODINGSHEME);

			if(allclass.size()!=0)//
			{
				//bw.write(allclass.size()+"\n");	
				for(int j=0;j<allclass.size();j++)//Organization string and wrte line in date
				{
					String temp=((Class)allclass.get(j)).classId+GlobalValue.SPLITCHAR+((Class)allclass.get(j)).className+"\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);
		}
	}
	/**
	 * use to debug,show all info of classArray 
	 * @param classArray
	 */
	public void showMessage(ArrayList classArray)
	{
		if(classArray!=null)
		{
			for(int i=0;i<classArray.size();i++)
			{
				System.out.println("classid:"+((Class)classArray.get(i)).classId+"  classname:"+((Class)classArray.get(i)).className);
			}
		}
		else
		{
			System.out.println("date is empty!");
		}
	}
	public static void main(String[] arg)
	{
		Class temp=new Class("111","111");
		ArrayList  message=Class.loadAllClass();
		Class.saveAllClass(message);
		temp.addnewClass(temp);
	}
	public String getClassId() {
		return classId;
	}
	public void setClassId(String classId) {
		this.classId = classId;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	/**
	 * Determine whether a string is the number 
	 * @param str the string
	 * @return number then return true,or 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;
	} 
	/**
	 * Export data of classes
	 * @return return the date string of all classes
	 */
	public String exportalldate()
	{
		try
		{
			File file = new File(GlobalValue.CLASSDATESOURSE);
		    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();
		       //index ++   
		    }  
		    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 filed";
	}
	/**
	 * Impotcontent will import the contents of the class data files
	 * @param impotcontent
	 * @return return  import results
	 */
	public String importclassdate(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=loadAllClass();
				ArrayList adderroclasses=new ArrayList();//class is exsit
				ArrayList adderroidisnotnum=new ArrayList();//id is not number
				ArrayList adderroidisrowerro=new ArrayList();//string
				for(int i=0;i<impotrowsplit.length;i++)
				{
					String[] classinfo=impotrowsplit[i].split(GlobalValue.SPLITCHAR);
					if(classinfo.length==2)
					{
						Class tempclass=new Class(classinfo[0],classinfo[1]);
						if(!isNumeric(tempclass.classId))//id number only
						{
							adderroidisnotnum.add(tempclass);
						}
						else//id is number
						{
							if(singleclassinclassarray(allclass,tempclass))//Repeat id will not be added
							{
								adderroclasses.add(tempclass);
							}
							else//not Repeat id will be added
							{
								allclass.add(tempclass);
								importnum++;
							}
						}
					}//if line number is more than 2				
					else
					{
						adderroidisrowerro.add(impotrowsplit[i]);
						//return	
					}
				}//for
				allclass.trimToSize();
				Classcomparator classco=new Classcomparator();					
				Collections.sort(allclass,classco); 
				saveAllClass(allclass);
				//Organization feedback string
				importinfo.append(importnum+"data import successful !\n");
				if(adderroclasses.size()>0)//import failure
				{
					importinfo.append("Following the failure to import content\nReason:id repeat\n");
					for(int j=0;j<adderroclasses.size();j++)
					{ 
						Class erro=(Class)adderroclasses.get(j);
						importinfo.append(erro.getClassId()+GlobalValue.SPLITCHAR+erro.getClassName());
						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
			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 whether singleclass is inallclass
	 * @param allclass Class ArrayList
	 * @param singleclass Class to determine
	 * @return
	 */
	public boolean singleclassinclassarray(ArrayList allclass,Class singleclass)
	{
		for(int i=0;i<allclass.size();i++)
		{
			if(singleclass.myequals((Class)allclass.get(i)))
			{
				return true;
			}
		}
		return false;
	}
	/**
	 * Judge whether the id is singleclassid is in allclass
	 * @param allclass Class ArrayList
	 * @param singleclass the Judged id 
	 * @return return the exist  number
	 */
	public int isclassidexsitclassarray(ArrayList allclass,int singleclassid)
	{
		int j=0;
		for(int i=0;i<allclass.size();i++)
		{
			Class temp=(Class)allclass.get(i);
			int id=Integer.parseInt(temp.classId);
			if(singleclassid==id)
			{
				j++;
			}
		}
		return j;
	}
	/**
	 * Judge the id is classid's class is exist 
	 * @param classid
	 * @return exist :true,or false
	 */
	public boolean isexistclass(String classid)
	{
		ArrayList allclass=loadAllClass();
		for(int i=0;i<allclass.size();i++)
		{
			Class temp=(Class)allclass.get(i);
			if(classid.equals(temp.getClassId()))
			{
				return true;
			}
		}
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -