Question
i need help please. My Program isnt serving its purpose. Given... Write a simple file matching account receivable program using object serialization. Modify the Account
i need help please. My Program isnt serving its purpose.
Given...
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.
this is what ive done..
package filematch_package_NM;
// Serializable Account class for storing records as objects.
import java.io.Serializable;
public class AccountRecordSerializable implements Serializable
{
public static final long serialVersionUID = 1L;
private int account;
private String firstName;
private String lastName;
private double balance;
// initializes an Account with default values
public AccountRecordSerializable()
{
this(0, "", "", 0.0); // call other constructor
}
// initializes an Account with provided values
public AccountRecordSerializable(int account, String firstName,
String lastName, double balance)
{
this.account = account;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
// set account number
public void setAccount(int acct)
{
this.account = account;
}
// get account number
public int getAccount()
{
return account;
}
// set first name
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
// get first name
public String getFirstName()
{
return firstName;
}
// set last name
public void setLastName(String lastName)
{
this.lastName = lastName;
}
// get last name
public String getLastName()
{
return lastName;
}
// set balance
public void setBalance(double balance)
{
this.balance = balance;
}
// get balance
public double getBalance()
{
return balance;
}
} // end class AccountRecordSerializable
package filematch_package_NM; // Create data to put into an account file and a transactions file. import java.io.IOException; import java.io.FileOutputStream; import java.io.ObjectOutputStream;
public class CreateData { private static ObjectOutputStream outOldMaster, outTransaction;
public static void creatingData() { try { try { // file streams for output files outOldMaster = new ObjectOutputStream( new FileOutputStream("oldmast.ser")); outTransaction = new ObjectOutputStream( new FileOutputStream("trans.ser")); } catch (IOException io) { System.err.println("Error opening the file."); } try { outOldMaster.writeObject(new AccountRecordSerializable( 100, "Alan", "Jones", 348.17)); outOldMaster.writeObject(new AccountRecordSerializable( 300, "Mary", "Smith", 27.19)); outOldMaster.writeObject(new AccountRecordSerializable( 500, "Sam", "Sharp", 0.00)); outOldMaster.writeObject(new AccountRecordSerializable( 700, "Suzy", "Green", -14.22)); outTransaction.writeObject( new TransactionRecord(100, 27.14)); outTransaction.writeObject( new TransactionRecord(300, 62.11)); outTransaction.writeObject( new TransactionRecord(300, -10.00)); outTransaction.writeObject( new TransactionRecord(400, 100.56)); outTransaction.writeObject( new TransactionRecord(900, 82.17)); } catch (IOException io) { System.out.println("Error writing to the files."); System.exit(1); } } finally // close the files { try { if (outTransaction != null) outTransaction.close();
if (outOldMaster != null) outOldMaster.close(); } catch (IOException io) { System.err.println("Error closing the files."); System.exit(1); } } } } // end class CreateData
package filematch_package_NM;
//File Match program //Created by: Nehemias Miranda //Class: Java Development
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
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
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 filematch_package_NM;
//TransactionRecord.java
//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
package filematch_package_NM; //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 { CreateData.creatingData(); FileMatch.matching(); FileMatch.writeLogFile(); AccountRecordSerializable myMeth= new AccountRecordSerializable(); double bal= myMeth.getBalance(); System.out.println("Balance is " + bal); // 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 number " +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
Thanks for your help.
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. 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 orderStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started