import java.util.*;
import java.io.*;
class Week3Question2
{
public static void main (String args[])
{
TreeSet OldAccount = new TreeSet();
TreeSet CurrentAccount = new TreeSet();
TreeSet PseudoAccount = new TreeSet();
try
{
RetrieveAcc(OldAccount,CurrentAccount);
}
catch(Exception exc)
{
System.out.println("No such file!");
}
try
{
WriteAcc(OldAccount,CurrentAccount,PseudoAccount);
}
catch(Exception exc)
{
System.out.println("Process failed !");
}
System.out.println(CurrentAccount);
System.out.println(CurrentAccount.size()+"\n\n");
System.out.println(OldAccount);
System.out.println(OldAccount.size());
}
public static void RetrieveAcc(TreeSet Old, TreeSet Current)throws Exception
{
String dataStore = "";
Scanner oldAcc = new Scanner(new FileReader("oldAccounts.txt"));
Scanner currAcc = new Scanner(new FileReader("currAccounts.txt"));
oldAcc.skip("ID\tName");
currAcc.skip("ID\tName");
while(oldAcc.hasNext())
{
dataStore = oldAcc.nextLine();
Old.add(dataStore);
}
while(currAcc.hasNext())
{
dataStore = currAcc.nextLine();
Current.add(dataStore);
}
Old.remove("\t");
Current.remove("\t");
oldAcc.close();
currAcc.close();
}
public static void WriteAcc(TreeSet Old, TreeSet Current,TreeSet Pseudo)throws Exception
{
FileWriter gradAcc = new FileWriter("gradAccounts.txt");
FileWriter newAcc = new FileWriter("newAccounts.txt");
Pseudo.addAll(Old);
Pseudo.removeAll(Current);
Object IteData;
Iterator PseudoIte = Pseudo.iterator();
gradAcc.write("ID\tName\r\n");
while(PseudoIte.hasNext())
{
IteData = PseudoIte.next();
gradAcc.write(IteData+"\r\n");
}
gradAcc.close();
newAcc.write("ID\tName\r\n");
Pseudo.clear();
Pseudo.addAll(Current);
Pseudo.removeAll(Old);
PseudoIte = Pseudo.iterator();
while(PseudoIte.hasNext())
{
IteData = PseudoIte.next();
newAcc.write(IteData+"\r\n");
}
newAcc.close();
//System.exit(0);
}
}
&
import java.util.*;
import java.io.*;
class Week3Question3
{
public static void main (String args[])
{
String[] OldAccounts = new String[31];
String[] CurrentAccounts = new String[15];
try
{
loadOldAcc(OldAccounts,CurrentAccounts);
}
catch(Exception e)
{
System.err.println("Error in reading file!");
}
try
{
loadCurrAccounts(OldAccounts,CurrentAccounts);
}
catch(Exception e)
{
System.err.println("Error in reading file!");
}
try
{
writeRecord(OldAccounts,CurrentAccounts);
}
catch(Exception e)
{
System.err.println("Error in reading file!");
}
}
public static void writeRecord(String[] Old,String[] Current) throws Exception
{
BufferedWriter gradAccounts = new BufferedWriter(new FileWriter("gradAccounts.txt"));
gradAccounts.write("ID\tName\r\n");
boolean GradIsAvailable = false;
for(int m=1; m<Old.length; m++)
{
for(int n=1; n<Current.length; n++)
{
if(Old[m].trim().equals(Current[n].trim()))
{
GradIsAvailable = true;
break;
}
}
if(!GradIsAvailable)
{
gradAccounts.write(Old[m] + "\r\n");
}
GradIsAvailable = false;;
}
gradAccounts.close();
BufferedWriter newAccounts = new BufferedWriter(new FileWriter("newAccounts.txt"));
newAccounts.write("ID\tName\r\n");
boolean NewIsAvailable = false;
for(int m=1; m<Current.length; m++)
{
for(int n=1; n<Old.length; n++)
{
if(Current[m].trim().equals(Old[n].trim()))
{
NewIsAvailable = true;
break;
}
}
if(!NewIsAvailable)
{
newAccounts.write(Current[m] + "\r\n");
}
NewIsAvailable = false;;
}
newAccounts.close();
}
public static void loadOldAcc(String[] Old, String[] Current) throws Exception
{
Scanner oldAccounts = new Scanner(new FileReader("oldAccounts.txt"));
for(int m=0; m<Old.length; m++){
Old[m] = oldAccounts.nextLine();
}
}
public static void loadCurrAccounts(String[] Old, String[] Current) throws Exception
{
Scanner currAccounts = new Scanner(new FileReader("currAccounts.txt"));
for(int n=0; n<Current.length; n++){
Current[n] = currAccounts.nextLine();
}
}
}
// Please help me to explain these codes by writing the explanation beside the codes using '//'.thanks