Reputation: 31
The instructions are:
To process the transactions, you will need to read one line at a time from the transactions.txt file and parse the String that you retrieve. You can use the Scanner class for this. The delimiter will be a colon. Then process the transaction; you do not need to check the type of transaction. Just add the amount of the transaction to the checkbook balance. Adding a negative transaction amount will decrease the balance as expected. Be sure to use try/catch blocks where appropriate.
After you have processed each transaction, call the animate method. This method belongs to the Accounting class, so you will call animate without using an object reference. The API if the animate method is the following
public void animate { String currentTransaction, double currentAmount, double currentBalance, }
As you can see, the animate method takes three arguments: currentTransaction is the transaction name ("Deposit," for example), currentAmount is the amount of the transaction (-45.00, for example), and currentBalance is the current balance of the checkbook. Assuming that you have a String variable called transactionName, a double variable called amount, and another double called balance, a call to animate will look like the following:
animate( transactionName, amount, balance);
When you call the animate, the window will display the current transaction geographically. It will also display the transaction amount (red, if negative, blue if positive), and the current checkbook balance (in black). By adding the previous checkbook balance to the current amount, you will be able to compute in your head what the current checkbook valance should be and determine if your program is working correctly.
When you reach the end of the file, print the final balance and write it to a file named balance.txt
So far, I have coded this much in the Accounting class:
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.Graphics;
import java.util.*;
import java.io.*;
public class Accounting extends JFrame
{
private BankAccount bankAccount;
public Accounting( )
{
bankAccount = new BankAccount( getBackground( ) );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 300, 300 );
setVisible( true );
}
public void balanceCheckBook( )
{
// ***** Write the body of this method *****
//
// Using a while loop, read the file transactions.txt
// The file transactions.txt contains
// transactions between you and your bank
//
// You will need to call the animate method inside
// the body of the loop that reads the file contents
//
// The animate method takes three arguments:
// a String, representing the type of transaction
// a double, representing the transaction money amount
// a double, representing the new checkbook balance
// So if these three variables are:
// transactionName, currentAmount, and balance,
// then the call to animate will be:
//
// animate( transactionName, currentAmount, balance );
//
// You should make that call in the body of your while
// loop, after you have updated the checkbook balance
//
double balance = 0.00;
double currentAmount;
String nextLine;
StringTokenizer st;
String transactionName;
}
public void animate( String currentTransaction, double currentAmount, double currentBalance )
{
if ( currentTransaction.startsWith( "Ch" ) )
bankAccount.setCurrentTransaction( new Check(currentAmount ) );
else if ( currentTransaction.startsWith( "With" ) )
bankAccount.setCurrentTransaction( new Withdrawal(currentAmount ) );
else if ( currentTransaction.startsWith( "Dep" ) )
bankAccount.setCurrentTransaction( new Deposit(currentAmount ) );
else
bankAccount.setCurrentTransaction( new UnknownTransaction(currentAmount ) );
bankAccount.updateBalance( currentBalance );
repaint( );
try
{
Thread.sleep( 3000 );
}
catch ( Exception e )
{
}
}
public void paint( Graphics g )
{
super.paint( g );
bankAccount.draw( g );
}
public static void main( String [] args )
{
Accounting app = new Accounting( );
app.balanceCheckBook( );
}
}
Upvotes: 0
Views: 835
Reputation: 17981
Using a scanner is the easiest way to read a file in java
import java.util.Scanner
then
Scanner myScanner = new Scanner(new File("/Your/File/Path/Here/transactions.txt");
then
String line;
while (myScanner.hasNextLine()) {
line = myScanner.nextLine();
//i think you'll call your animate function in here
}
Depending on your file structure, you could use myScanner.next() and myScanner.nextInt() to get tokens and ints respectively, a token is usually the a word delimited by whitespace
[edit]
op wanted an explanation on how to scan each line, here is a demo
while (myScanner.hasNextLine()) {
line = myScanner.nextLine();
Scanner lineReader = new Scanner(line);
String firstWord = lineReader.next();
String secondWord = lineReader.next();
double thirdWordValue = lineReader.nextDouble();
double fourthWordValue = lineReader.nextDouble();
animate(firstWord, thirdWordValue, fourthWordValue);
}
Upvotes: 1