Assignment 2 File Matching Write a simple file matching acco
Assignment 2- File Matching Write a simple file matching account receivable program using object serialization. Modify the Account class as needed. Use package name filematch_package_yourinitials There should be a master file (eg: oldmaster.ser) containing details about each account. As transactions occur, data is entered into a transaction file.(Eg: trans.ser ) Your program should mimic a file matching process that occurs at the end of a business term period, where these transactions from the transaction file are applied to the old master file and it gets rewritten as a new master file ( eg: newmaster.ser\")
A) Create a class FileMatch to perform file matching functionality. This class should have methods that read the old master and transaction files.
Case 1) When a match occurs (records with same account number appear in both master file and transaction file) update the current balance in the master record and add records to newmaster file. Account for multiple transaction records with same account number.
Case 2) When there\'s a master record for a particular account but no corresponding transaction record , write the master record to newmaster file.
Case 3) When there is a transaction record but no corresponding master record print to a log file the message \" Unmatched transaction record for account number...\". The Log file should be a text file called log.txt
B) Make sure you have programs that create test data for the master and transaction files. (Refer Sample data given above) You could also instead use the sample CreateData.java file I have attached which loads these two files.
C) Also write a program to read data stored in all the three files (oldmaster, trans, newmaster) to check the file match results.
Attached are sample data for the master file and the transaction file. Master File Account Number Name Balance 100 348.17 Alan Jones 27.19 300 Mary Smith 500 Sam Sharp 0.00 uzy Green -14.22 700 Transaction File Account Number Transaction Amount (-ve values indicate payments 27.14 100 300 62.11 -10.00 300 400 100.56 82.17 900 Use the account number on each file as the record key for matching purposes. Assume that each file is a sequential file where records are stored in increasing account number order.Solution
//File Match program
package Solution;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileMatch {
private static ObjectInputStream inOldMaster, inTransaction;
private static ObjectOutputStream outNewMaster;
static List<String>matchingTxnRecords = new ArrayList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
// file streams for reading file
inOldMaster = new ObjectInputStream(new FileInputStream(\"oldmast.ser\"));
outNewMaster = new ObjectOutputStream(new FileOutputStream(\"newmast.ser\"));
Object object = null;
while ((object = inOldMaster.readObject()) != null)
{
if (object instanceof AccountRecordSerializable)
{
//deserialize the object;
AccountRecordSerializable accountRecord= (AccountRecordSerializable)(object);
double newBalance = readTransactionFile(accountRecord.getAccount());
if(newBalance == 0)
newBalance = accountRecord.getBalance();
//write to the new file
outNewMaster.writeObject(new AccountRecordSerializable(
accountRecord.getAccount(), accountRecord.getFirstName(), accountRecord.getLastName(), newBalance));
} //end of if
} // end of while
outNewMaster.close();
inOldMaster.close();
} // end of try
catch (EOFException ex) { //This exception will be caught when EOF is reached
// System.out.println(\"End of file reached.\");
}
catch (IOException io)
{
System.err.println(\"Error opening the file.\");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write unmatched transaction records to the file
writeLogFile();
}
public static double readTransactionFile(int accountNo)
{
double balance=0;
try
{
// file streams for reading file
inTransaction = new ObjectInputStream(new FileInputStream(\"trans.ser\"));
Object object = null;
while ((object = inTransaction.readObject()) != null)
{
if (object instanceof TransactionRecord)
{
TransactionRecord txnRecord= (TransactionRecord)(object);
//check matching accounts
if(txnRecord.getAccount() == accountNo)
{
//sum all the transaction balance amount of same account number
balance +=txnRecord.getBalance();
//store all matched account numbers
matchingTxnRecords.add(String.valueOf(accountNo));
}
} // end of if loop
} //end of while loop
inTransaction.close();
inTransaction=null;
} // end of try
catch (EOFException ex) { //This exception will be caught when EOF is reached
// System.out.println(\"End of file reached.\");
}
catch (IOException io)
{
System.err.println(\"Error opening the transaction file.\");
io.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return balance;
} // end of method
public static void writeLogFile()
{
try{
//write the unmatched accounts to the log.txt file
FileWriter fileWriter = new FileWriter(\"log.txt \");
BufferedWriter bwriter = new BufferedWriter(fileWriter);
try
{
// file streams for reading file
inTransaction = new ObjectInputStream(new FileInputStream(\"trans.ser\"));
Object object = null;
while ((object = inTransaction.readObject()) != null)
{
if (object instanceof TransactionRecord)
{
TransactionRecord txnRecord = (TransactionRecord)(object);
boolean isAccountExist=false;
for (int i = 0; i < matchingTxnRecords.size(); i++)
{
//check if account is in transaction file
if(matchingTxnRecords.get(i).equals(String.valueOf(txnRecord.getAccount())))
{
isAccountExist=true;
break;
} //end of if loop
} //end of for loop
if(!isAccountExist)
{
//write to the new file
bwriter.write(\"Unmatched transaction record for account number \" + String.valueOf(txnRecord.getAccount()));
bwriter.newLine();
}
} //end of if
} // end of while
} //end of try
catch (EOFException ex) { //This exception will be caught when EOF is reached
// System.out.println(\"End of file reached.\");
}
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
bwriter.flush();
bwriter.close();
fileWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
} // end of method
} //end of class
package Solution;
//Reconcillation file to check the 3 files
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Reconcillation {
private static ObjectInputStream inOldMaster, inTransaction, inNewMaster;
public static void main(String[] args) {
try
{
// file streams for reading file
inNewMaster = new ObjectInputStream(new FileInputStream(\"newmast.ser\"));
Object object = null;
while ((object = inNewMaster.readObject()) != null)
{
if (object instanceof AccountRecordSerializable)
{
//deserialize the object;
AccountRecordSerializable accountRecord= (AccountRecordSerializable)(object);
double txnBalance = readTransactionFile(accountRecord.getAccount());
double oldmasterBalance = readOldMasterFile(accountRecord.getAccount());
if(accountRecord.getBalance() == txnBalance + oldmasterBalance)
{
System.out.println(\"All 3 files match the result for account numner \" +accountRecord.getAccount() );
}
} //end of if
} // end of while
inNewMaster.close();
} // end of try
catch (EOFException ex) { //This exception will be caught when EOF is reached
// System.out.println(\"End of file reached.\");
}
catch (IOException io)
{
System.err.println(\"Error opening the file.\");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static double readTransactionFile(int accountNo)
{
double balance=0;
try
{
// file streams for reading file
inTransaction = new ObjectInputStream(new FileInputStream(\"trans.ser\"));
Object object = null;
while ((object = inTransaction.readObject()) != null)
{
if (object instanceof TransactionRecord)
{
TransactionRecord txnRecord= (TransactionRecord)(object);
//check matching accounts
if(txnRecord.getAccount() == accountNo)
{
//sum all the transaction balance amount of same account number
balance +=txnRecord.getBalance();
}
} // end of if loop
} //end of while loop
inTransaction.close();
inTransaction=null;
} // end of try
catch (EOFException ex) { //This exception will be caught when EOF is reached
// System.out.println(\"End of file reached.\");
}
catch (IOException io)
{
System.err.println(\"Error opening the transaction file.\");
io.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return balance;
} // end of method
public static double readOldMasterFile(int accountNo)
{
double balance=0;
try
{
// file streams for reading file
inOldMaster = new ObjectInputStream(new FileInputStream(\"oldmast.ser\"));
Object object = null;
while ((object = inOldMaster.readObject()) != null)
{
if (object instanceof TransactionRecord)
{
AccountRecordSerializable accountRecord= (AccountRecordSerializable)(object);
//check matching accounts
if(accountRecord.getAccount() == accountNo)
{
//sum all the transaction balance amount of same account number
balance=accountRecord.getBalance();
break;
}
} // end of if loop
} //end of while loop
inOldMaster.close();
inOldMaster=null;
} // end of try
catch (EOFException ex) { //This exception will be caught when EOF is reached
// System.out.println(\"End of file reached.\");
}
catch (IOException io)
{
System.err.println(\"Error opening the transaction file.\");
io.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return balance;
} // end of method
} //end of class
//TransactionRecord.java
package Solution;
//Serializable Account class for storing records as objects.
import java.io.Serializable;
public class TransactionRecord implements Serializable
{
public static final long serialVersionUID = 1L;
private int account;
private double balance;
// initializes an Account with default values
public TransactionRecord()
{
this(0, 0.0); // call other constructor
}
// initializes an Account with provided values
public TransactionRecord(int account, double balance)
{
this.account = account;
this.balance = balance;
}
// set account number
public void setAccount(int acct)
{
this.account = account;
}
// get account number
public int getAccount()
{
return account;
}
// set balance
public void setBalance(double balance)
{
this.balance = balance;
}
// get balance
public double getBalance()
{
return balance;
}
} // end class AccountRecordSerializable











