Hello,
I am trying to allow my program to accept multiple transaction records. I have created a file creator (CreateData) to populate two text files (oldmast.txt, trans.txt). CreateData will need to be run to create these two files. I have also created a file matching class (FileMatch) to process and write the processed information to newmast.txt and log the unnecessary transactions to log.txt. FileMatchTest will need to be ran to process the files. The problem that I am having is that my program will not allow multiple transactions. Specifically transactions[4], transactions[5], and transactions[6]. In theory, my newmast.txt file should have all the accounts listed with the correct balances. Can anyone give me any pointers for how to accomplish this? Thank you.
public class AccountRecord
{
private int account;
private String firstName;
private String lastName;
private double balance;
// no-argument constructor calls other constructor with default values
public AccountRecord()
{
this( 0, "", "", 0.0 ); // call four-argument constructor
} // end no-argument AccountRecord constructor
// initialize a record
public AccountRecord( int acct, String first, String last, double bal )
{
setAccount( acct );
setFirstName( first );
setLastName( last );
setBalance( bal );
} // end four-argument AccountRecord constructor
// set account number
public void setAccount( int acct )
{
account = acct;
} // end method setAccount
// get account number
public int getAccount()
{
return account;
} // end method getAccount
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// get first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// get last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set balance
public void setBalance( double bal )
{
balance = bal ;
} // end method setBalance
// get balance
public double getBalance()
{
return balance;
} // end method getBalance
//Patrick: added to combine AccountRecord object and the amount value of the TransactionRecord object
public void combine (TransactionRecord transaction){
balance = balance + transaction.getAmount();
}
} // end class AccountRecord
import java.lang.SecurityException;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.IllegalFormatException;
public class CreateData {
public static void main(String args[]) {
Formatter outOldMaster = null;
Formatter outTransaction = null;
AccountRecord account = new AccountRecord();
AccountRecord accounts[] = new AccountRecord[4];
TransactionRecord transactions[] = new TransactionRecord[7];
// create account records
accounts[0] = new AccountRecord(100, "Alan", "Jones", 348.17);
accounts[1] = new AccountRecord(300, "Mary", "Smith", 27.19);
accounts[2] = new AccountRecord(500, "Sam", "Sharp", 0.00);
accounts[3] = new AccountRecord(700, "Suzy", "Green", -14.22);
// create transactions
transactions[0] = new TransactionRecord(100, 27.14);
transactions[1] = new TransactionRecord(300, 62.11);
transactions[2] = new TransactionRecord(400, 100.56);
transactions[3] = new TransactionRecord(900, 82.17);
//9-3-08: Create additional transactions for 14.9, figure 14.26
transactions[4] = new TransactionRecord(300, 83.89);
transactions[5] = new TransactionRecord(700, 80.78);
transactions[6] = new TransactionRecord(700, 1.53);
try {
outOldMaster = new Formatter("oldmast.txt");
for (int i = 0; i < accounts.length; i++) {
outOldMaster.format("%d %s %s %.2f\n",
accounts[i].getAccount(), accounts[i].getFirstName(),
accounts[i].getLastName(), accounts[i].getBalance());
} // end for
//9-5-08: This for loop creates the transaction record.
//duplicate records are allowed using this method
// file stream for output file
outTransaction = new Formatter("trans.txt");
for (int i = 0; i < transactions.length; i++) {
outTransaction.format("%d %.2f\n",
transactions[i].getAccount(), transactions[i].getAmount());
} // end for
} // end try
catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1);
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error creating file.");
System.exit(1);
} // end catch
catch (IllegalFormatException formatException) {
System.err.println("Error with output.");
System.exit(1);
} // end catch
catch (FormatterClosedException closedException) {
System.err.println("Error writing to file.");
System.exit(1);
} // end catch
finally {
if (outOldMaster != null)
outOldMaster.close();
if (outTransaction != null)
outTransaction.close();
} // end finally
} // end main
} // end class CreateData
import java.io.File;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.util.IllegalFormatException;
import java.util.ArrayList;
public class FileMatch {
private static Scanner inOldMaster;//OldMaster Scanner
private static Scanner inTransaction;//Trans Scanner
private static Formatter outNewMaster;//NewMast formatter
private static Formatter logFile;//Log formatter
private static TransactionRecord transaction;
private static AccountRecord account;
public FileMatch(){
transaction = new TransactionRecord();
account = new AccountRecord();
}//end FileMatch
public void openFiles(){
try {
inOldMaster = new Scanner(new File("oldmast.txt"));
//oldMastRecordList.add(account);
inTransaction = new Scanner(new File("trans.txt"));
//transRecordList.add(transaction);
outNewMaster = new Formatter("newmast.txt");
logFile = new Formatter("log.txt");
}//end try
catch(Exception exception){
System.err.println("Error opening file.");
}//end catch
}//end openFiles
public void processFiles(){
int transactionAccountNumber;
int accountNumber;
//block to read and write all records
try{
transaction = getTransactionRecord();//get trans record and acct number
if (transaction == null)
return;//if the transaction is empty, do not go further
transactionAccountNumber = transaction.getAccount();
account = getAccountRecord();//get the account record and number
if(account == null)
return;//if account is null, stop
accountNumber = account.getAccount();
while(accountNumber != 0){
while(accountNumber < transactionAccountNumber){
outNewMaster.format("%d %s %s %.2f\n", account.getAccount(),
account.getFirstName(), account.getLastName(),
account.getBalance());
account = getAccountRecord(); //get new account
if(account == null)
return;
accountNumber = account.getAccount();
}//end while
// 9-5-08: Combine method adds the transaction amounts to newmast.txt
if(accountNumber == transactionAccountNumber){
account.combine(transaction);//combine records
outNewMaster.format("%d %s %s %.2f\n", account.getAccount(),
account.getFirstName(), account.getLastName(),
account.getBalance());//write to the master file
//account.combine(transaction);//combine records
transaction = getTransactionRecord();//get new transaction
if(transaction == null)
return;
transactionAccountNumber = transaction.getAccount();
account= getAccountRecord();//get new account
if (account == null)
return;
accountNumber = account.getAccount();
}//end if
if (transactionAccountNumber < accountNumber){
logFile.format("%s %d\n", "Unmatched transaction record for account number",
transactionAccountNumber);
transaction = getTransactionRecord();//get new transaction
if(transaction==null)
return;
transactionAccountNumber = transaction.getAccount();
}//end while
}//end outer while
}//end try
catch(IllegalFormatException formatException){
System.err.println("Output Error.");
System.exit(1);
}//end catch
}//end processFiles
public void closeFiles(){
try{
if(inTransaction != null)
inTransaction.close();
if(outNewMaster != null)
outNewMaster.close();
if(inOldMaster != null)
inOldMaster.close();
if(logFile != null)
logFile.close();
}//end try
catch(Exception exception){
System.err.println("Error closing files.");
System.exit(1);
}//end catch
}//end closeFiles
private TransactionRecord getTransactionRecord(){
try{
if(inTransaction.hasNext()){
transaction.setAccount(inTransaction.nextInt());
transaction.setAmount(inTransaction.nextDouble());
return transaction;
}//end if
else{
while(inOldMaster.hasNext()){
account.setAccount(inOldMaster.nextInt());
account.setFirstName(inOldMaster.next());
account.setLastName(inOldMaster.next());
account.setBalance(inOldMaster.nextDouble());
//store in new master
outNewMaster.format("%d %s %.2f\n",account.getAccount(),
account.getFirstName(), account.getLastName(),
account.getBalance());
//account.combine(transaction);//combine records
}//end while
}//end else
}//end try
catch(FormatterClosedException closedException){
System.err.println("Error writing to file- file closed.");
System.exit(1);
}//end catch
catch(IllegalFormatException formatException){
System.err.println("Error with output.");
System.exit(1);
}//end catch
catch(NoSuchElementException elementException){
System.err.println("Invalid input from file.");
}//end catch
return null; //no more records
}//end getTransactionRecord
private AccountRecord getAccountRecord(){
try{
if(inOldMaster.hasNext()){
account.setAccount(inOldMaster.nextInt());
account.setFirstName(inOldMaster.next());
account.setLastName(inOldMaster.next());
account.setBalance(inOldMaster.nextDouble());
return account;
}//end if
else{//end of old master file
logFile.format("%s %d\n", "Unmatched transaction record for account number",
transaction.getAccount());
//these records are transactions without accounts
while(inTransaction.hasNext()){
transaction.setAccount(inTransaction.nextInt());
transaction.setAmount(inTransaction.nextDouble());
}//end while
}//end else
}//end try
catch(FormatterClosedException closedException){
System.err.println("Error writing to file..closing file.");
System.exit(1);
}//end catch
catch(NoSuchElementException elementException){
System.err.println("Invalid input from file.");
}//end catch
return null;
}//end getAccountRecord
}//end FileMatch
public class FileMatchTest {
public static void main(String args[]){
FileMatch application = new FileMatch();
application.openFiles();
application.processFiles();
application.closeFiles();
}//end main
}//end class FileMatchTest
public class TransactionRecord {
private int account;
private double amount;
public TransactionRecord() {
this(0, 0.0);
} // end no-argument constructor
public TransactionRecord(int acct, double amt) {
setAccount(acct);
setAmount(amt);
} // end two-argument constructor
public void setAccount(int acct) {
account = acct;
} // end setAccount
public int getAccount() {
return account;
} // end getAccount
public void setAmount(double amt) {
amount = amt;
} // end setAmount
public double getAmount() {
return amount;
} // end getAmount
}//end TransactionRecord