• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sequential of 2 accounts file processing concurrently to update balance

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having lot of problem to program and read from two data file.
1. trouble identifying unmatched transactions

Hope to correct the problem with help from panel members. Thanks

The code so far:




//Transaction processing of accounts file sequentially
// having trouble identifying unmatched transactions

import java.io.*;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException; // for os file checking
import java.util.NoSuchElementException; // for os file checking
import java.util.Scanner;

public class TransAccProc
{
private static Scanner input; // input for oldmast file
private static Scanner inputTrans; // input for trans file

private static String oldMast = "oldmast.txt";
private static String transFile = "trans.txt";
private static String newMast = "newmast.txt";
private static String newLog = "log.txt"; // logs unknown accounts from new transaction file

private static int newAccountNumber ; //get this out of here

public static void main( String args[] )
{
openFile();// should be able to send it file name and input variable and reuse this.
// add processing here, and break up into methods after program functioning.
readRecords();
closeFile();
// need to put an error checking routine here before outputting this
System.out.println(newMast+" and possibly "+newLog+" outputed to file(s)");
}


public static void openFile()
{
try
{
input = new Scanner( new File( oldMast ) );
} // end try
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
} // end catch
} // end method openFile


// read record from file
public static void readRecords()
{
//System.out.printf( "%-10s%-12s%-12s%10s\n", "Account","First Name", "Last Name", "Balance" );

try // read records from file using Scanner object
{
while ( input.hasNext() )
{

int account = input.nextInt() ; // read account number
String firstName = input.next(); // read first name
String lastName = input.next(); // read last name
double balance = input.nextDouble(); // read balance
double newBalance = balance;
boolean newAccount = true;

// display info
//System.out.printf( "%-10d%-12s%-12s%10.2f\n", account,firstName,lastName,balance);

//**********inner loop******************************
try
{
inputTrans = new Scanner( new File( transFile));
} // end try
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
} // end catch


while ( inputTrans.hasNext() )
{
int accountTrans = inputTrans.nextInt() ; // read account number
double balanceTrans = inputTrans.nextDouble(); // read balance


if (account == accountTrans)
{
newBalance = balance +balanceTrans;
//System.out.printf( "Accounts match---- account %-10d account trans %-10d new balance %-10.2f \n",account, accountTrans,newBalance); //@@@@@@@@@@@@@@@@@@@@@@@@ use this for debugging
newAccount = false;
newAccountNumber = accountTrans;
// problem here, I am unable to grab the concept to stop the loop from counting and pick the transaction account number
// I am just picking the last one in the loop and loging that for now.
// I need to come back and figure out the logic to write the unknown account numbers to a log file
// currently only writing new account and last account number found.
// I spent about 4 hours on this part and was my only obstacle, and the cleanup into proper methods and structure.

}//end if
}
//Write out to the new master file
fileWrite(account,firstName,lastName,newBalance);

//write out log file for new accounts
if (newAccount)
{
newAccount = true;
//System.out.printf( "new account %d \n",newAccountNumber); //@@@@@@@@@@@@@@@@@@@@@@@@ use this to print to screen for debugging
fileWriteLog(newAccount,newAccountNumber);
}

//System.out.println(" "); //@@@@@@@@@@@@@@@@@@@@@@@@ use this to print to screen for debugging
} // end while

} // end try

catch ( NoSuchElementException elementException )
{
System.err.println( "File improperly formed." );
input.close();
System.exit( 1 );
} // end catch
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
System.exit( 1 );
} // end catch
} // end method readRecords

// close file
public static void closeFile()
{
if ( input != null )
input.close(); // close file
} // end method closeFile


// these should be overladed so I only have to write the code once.
public static void fileWrite(int account,String first, String last, double bal)
{
try{
FileWriter fstream = new FileWriter(newMast,true);
BufferedWriter out = new BufferedWriter(fstream);
//System.out.println("file write method ---writing \n");


String outString = String.format("%d %s %s %.2f %n", account,first,last,bal) ;
out.write(outString);

out.close(); //Close the output stream
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}


public static void fileWriteLog(boolean newAccount,int accountNewTrans)
{
try{
FileWriter fstream = new FileWriter(newLog,true); //new log is filename
BufferedWriter out = new BufferedWriter(fstream);
//System.out.println("file write method ---writing \n");

//String outString = String.format("%b %d %n", newAccount,accountNewTrans) ;
String outString = String.format("%d %n", accountNewTrans) ;
out.write(outString);

out.close(); //Close the output stream
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}






} // end class
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags. In its current form it's really unreadable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic