Answered! /* Accounting class Anderson, Franceschi */ import javax.swing.JOptionPane;…

/* Accounting class
Anderson, Franceschi
*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.FileNotFoundException;

Don't use plagiarized sources. Get Your Custom Essay on
Answered! /* Accounting class Anderson, Franceschi */ import javax.swing.JOptionPane;…
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

public class Accounting extends JFrame
{
private BankAccount bankAccount;

public Accounting()
{
super(“Reading objects from a file”);
bankAccount = new BankAccount(getBackground());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}

public void balanceCheckBook()
{
//
// ***** Student writes the body of this method *****
//
// Using a while loop, read the file transactions.obj
// The file transactions.obj contains transaction objects
//
// You will need to call the animate method inside
// the body of the loop that reads the objects
//
// The animate method takes two arguments:
// a Transaction object, representing the transaction
// a double, representing the new checkbook balance
// So if these two variables are transaction and balance,
// then the call to animate will be:
//
// animate(transaction, balance);
//
// You should make that call in the body of your while
// loop, after you have updated the checkbook balance
//
// Student code starts here:

// Student code ends here.
}

public void writeTransactions(ArrayList<Transaction> transactionList)
{
//
// writing to file transactions.obj
//

try
{
FileOutputStream fos = new FileOutputStream(“transactions.obj”);
ObjectOutputStream oos = new ObjectOutputStream(fos);

Transaction tempTransaction;
for (int i = 0; i < transactionList.size(); i++)
{
tempTransaction = (Transaction) transactionList.get(i);
oos.writeObject(tempTransaction);
}
oos.close();
}
catch (IOException ioe)
{
System.out.println(ioe.toString());
System.out.println(ioe.getMessage());
}
}

public void animate(Transaction currentTransaction, double currentBalance)
{
// set the currentTransaction data member in the bankAccount object
bankAccount.setCurrentTransaction(currentTransaction);

// set the currentBalance data member in the bankAccount object
bankAccount.updateBalance(currentBalance);

repaint();
try
{
Thread.sleep(3000); // wait for the animation to finish
}
catch (Exception e)
{
}
}

public void paint(Graphics g)
{
super.paint(g);
if (bankAccount != null)
{
bankAccount.draw(g);
}
}

public static void main(String[] args)
{
Accounting app = new Accounting();

// Generate an ArrayList of Transaction objects to write to file Transaction.obj
ArrayList<Transaction> transactionList = new ArrayList<Transaction>();
Check c1 = new Check(-500.00);
transactionList.add(c1);
Deposit d1 = new Deposit(3000.00);
transactionList.add(d1);
Withdrawal w1 = new Withdrawal(-400.00);
transactionList.add(w1);
c1 = new Check(-300.00);
transactionList.add(c1);

// write transactions as objects to file transaction.obj
app.writeTransactions(transactionList);

// read transaction.obj, balance the checkbook
app.balanceCheckBook();
}
}

/* BankAccount class
Anderson, Franceschi
*/
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;

public class BankAccount
{
private Transaction currentTransaction;
private double currentBalance;
private Color background;

public BankAccount( Color newBackground )
{
currentBalance = 0.0;
currentTransaction = null;
background = newBackground;
}

public void setCurrentTransaction( Transaction ca )
{
currentTransaction = ca;
}

public Transaction getCurrentTransaction( )
{
return currentTransaction;
}

public void updateBalance( double newCurrentBalance )
{
currentBalance = newCurrentBalance;
currentTransaction.updateBalance( currentBalance );
}

public void draw( Graphics g )
{
if (currentTransaction != null)
currentTransaction .draw( g, 50, 200, 175, background );
}

}

/* Check class
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;

public class Check extends Transaction
{
public Check( double p )
{
super( p );
}

public void draw( Graphics g, int startX, int endX, int y, Color background )
{
String display1 = “Check: Amount = ” + money.format( amount );
// set color to red
g.setColor( Color.RED );
g.drawString( display1, 20, 50 );
// set color to black
g.setColor( Color.BLACK );
g.drawString( display2, 20, 75 );

// set color to blue
g.setColor( Color.BLUE );
g.drawString( “Check”, startX + 3, y – 44 );

// draw check
g.drawRect( startX, y, 200, 100 );
g.drawString( “ABC Bank”, startX + 20, y + 20 );

for ( int x = startX + 100; x < endX + 20; x += 1 )
{
// sign the check
g.setColor( Color.BLACK );
g.drawLine( x, ( int ) ( y + 60 + 10 * Math.sin( x ) ),
x + 1, ( int ) ( y + 60 + 10 * Math.sin( x + 1 ) ) );

try {
Thread.sleep( ( int )( 30 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
}
}
}

/* Deposit class
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;

public class Deposit extends Transaction
{
public Deposit( double p )
{
super( p );
}

public void draw( Graphics g, int startX, int endX, int y, Color background )
{
String display1 = “Deposit: Amount = ” + money.format( amount );
// set color to blue
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );

// set color to black
g.setColor( Color.BLACK );
g.drawString( display2, 20, 75 );

// set color to blue
g.setColor( Color.BLUE );
g.drawString( “Deposit”, startX + 3, y – 44 );

// draw Work, Internet, Bank
g.drawLine( startX, y – 20, endX, y – 20 );
g.drawLine( startX, y + 40, endX, y + 40 );
g.drawString( “Work”, startX – 40, y + 20 );
g.drawString( “ABC Bank”, endX + 10, y + 20 );

// Animate a dollar bill
int x;
for ( x = startX; x < endX – 60; x += 5 )
{
g.setColor ( new Color( 0, 255, 0 ) );// green
g.fillRect( x, y, 60, 20 );
g.setColor ( new Color( 0, 0, 0 ) );
g.drawString( “$$$”, x + 20, y + 15 );

try {
Thread.sleep( ( int )( 100 ) );
}
catch (InterruptedException e )
{
e.printStackTrace( );
}
g.setColor( background ); // background
g.fillRect( x, y, 60, 20 ); //erase
}
g.setColor ( new Color( 0, 255, 0 ) ); // green
g.fillRect( x, y, 60, 20 );
g.setColor ( new Color( 0, 0, 0 ) );
g.drawString( “$$$”, x + 20, y + 15 );
}
}

/* Transaction class
Anderson, Franceschi
*/

import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;
import java.io.Serializable;

public abstract class Transaction implements Serializable
{
public static final DecimalFormat money = new DecimalFormat( “$#,##0.00” );

protected double amount;
protected double currentBalance;

protected String display2 = “”;

public Transaction( double p )
{
setAmount( p );
}

public void setAmount( double newAmount )
{
amount = newAmount;
// display1 = “Check: Amount = ” + money.format( amount );
}

public double getAmount( )
{
return amount;
}

public void updateBalance( double newCurrentBalance )
{
currentBalance = newCurrentBalance;
display2 = “Current account balance = ” + money.format( currentBalance );
}

public abstract void draw( Graphics g, int startX, int endX, int y , Color c );
}

/* UnknownTransaction class
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;

public class UnknownTransaction extends Transaction
{
public UnknownTransaction( double p )
{
super( p );
}

public void draw( Graphics g, int startX, int endX, int y, Color background )
{
String display1 = “Unknown Transaction: Amount = ” + money.format( amount );
if ( amount < 0 )
g.setColor( Color.RED );
else
g.setColor( Color.BLUE );
g.drawString( display1, 20, 50 );

// set color to black
g.setColor( Color.BLACK );
g.drawString( display2, 20, 75 );

// set color to blue
g.setColor( Color.BLUE );
g.drawString( “Unknown Transaction”, startX + 5, y – 45 );

int x;
for ( x = startX; x < endX; x += 5 )
{
g.setColor( Color.DARK_GRAY );
// Draw a question mark
g.drawArc( x, y, 20, 20, 0, 180 );
g.drawLine( x + 20, y + 10, x, y + 60 );
g.drawArc( x, y + 55, 10, 10, 180, 225 );
g.fillOval( x, y + 80, 15, 15 );

try {
Thread.sleep( ( int )( 70 ) );
}
catch (InterruptedException e )
{
e.printStackTrace( );
}
g.setColor( background ); // background
g.fillRect( x, y – 10, x + 50, y + 100 ); //erase
}

g.setColor( Color.DARK_GRAY );
// Draw a question mark
g.drawArc( x, y, 20, 20, 0, 180 );
g.drawLine( x + 20, y + 10, x, y + 60 );
g.drawArc( x, y + 55, 10, 10, 180, 225 );
g.fillOval( x, y + 80, 15, 15 );

}
}

/* Withdrawal class
Anderson, Franceschi
*/
import java.awt.Graphics;
import java.awt.Color;
import java.text.DecimalFormat;

public class Withdrawal extends Transaction
{
public Withdrawal( double p )
{
super( p );
}

public void draw( Graphics g, int startX, int endX, int begY, Color background )
{
String display1 = “Withdrawal: Amount = ” + money.format( amount );
// set color to red
g.setColor( Color.RED );
g.drawString( display1, 20, 50 );
// set color to black
g.setColor( Color.BLACK );
g.drawString( display2, 20, 75 );
// set color to blue
g.setColor( Color.BLUE );
g.drawString( “Withdrawal”, startX + 5, begY – 45 );

// draw an automated teller machine
g.drawRect( startX, begY, 200, 100 );

g.drawRect( startX + 10, begY + 10, 20, 20 );
g.drawRect( startX + 40, begY + 10, 20, 20 );
g.drawRect( startX + 70, begY + 10, 20, 20 );

g.drawRect( startX + 10, begY + 40, 20, 20 );
g.drawRect( startX + 40, begY + 40, 20, 20 );
g.drawRect( startX + 70, begY + 40, 20, 20 );

g.drawRect( startX + 10, begY + 70, 20, 20 );
g.drawRect( startX + 40, begY + 70, 20, 20 );
g.drawRect( startX + 70, begY + 70, 20, 20 );

g.drawString( “1”,startX + 17, begY + 25 );
g.drawString( “2”,startX + 47, begY + 25 );
g.drawString( “3”,startX + 77, begY + 25 );

g.drawString( “4”,startX + 17, begY + 55 );
g.drawString( “5”,startX + 47, begY + 55 );
g.drawString( “6”,startX + 77, begY + 55 );

g.drawString( “7”,startX + 17, begY + 85 );
g.drawString( “8”,startX + 47, begY + 85 );
g.drawString( “9”,startX + 77, begY + 85 );

// Animate a dollar bill
int y;
for ( y = begY + 5; y < begY + 75; y += 5 )
{
g.setColor ( new Color( 0, 255, 0 ) ); // green
g.fillRect( startX + 120, y, 60, 20 );
g.setColor ( new Color( 0, 0, 0 ) );
g.drawString( “$$$”, startX + 140, y + 15 );

try {
Thread.sleep( ( int )( 100 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
g.setColor( background ); // background
g.fillRect( startX + 120, y, 60, 20 ); //erase
}
g.setColor( new Color( 0, 255, 0 ) ); // green
g.fillRect( startX + 120, y, 60, 20 );
g.setColor( new Color( 0, 0, 0 ) );
g.drawString( “$$$”, startX + 140, y + 15 );
}

}

Do me a favor, only answer if you know the answer. I appreciate it.

Expert Answer

 Based on the question I can see changes need to be made only to one class named Accounting. The rest of the classes are supporting for carrying different types of transaction to be carried out.

-The only addition had to be made in balanceCheckBook() method in the Accounting class. The main functionality to be added was simply to read the ObjectFile that was created by the main method and to read through that file.
-The balanceCheckBook() is going to read through the file and fetch all the transactions objects one by one and is going to animate that into screen.
-Each different fetched transactions object would animate different graphics on screen.
-I’m adding only class that was modified, the rest of the code has to be used as it is.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.FileNotFoundException;

public class Accounting extends JFrame
{
private BankAccount bankAccount;

public Accounting()
{
super(“Reading objects from a file”);
bankAccount = new BankAccount(getBackground());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}

public void balanceCheckBook()
{
//
// ***** Student writes the body of this method *****
//
// Using a while loop, read the file transactions.obj
// The file transactions.obj contains transaction objects
//
// You will need to call the animate method inside
// the body of the loop that reads the objects
//
// The animate method takes two arguments:
// a Transaction object, representing the transaction
// a double, representing the new checkbook balance
// So if these two variables are transaction and balance,
// then the call to animate will be:
//
// animate(transaction, balance);
//
// You should make that call in the body of your while
// loop, after you have updated the checkbook balance
//
// Student code starts here:

// Student code ends here.
Transaction tempTransaction;
double balance=0; // A new double variable that keeps info about net change in balance based on transactions.
try{
FileInputStream input=new FileInputStream(“transactions.obj”);
ObjectInputStream oos = new ObjectInputStream(input);
while((tempTransaction=(Transaction)oos.readObject())!=null){
balance+=tempTransaction.getAmount();

animate(tempTransaction,balance);
}

}catch(Exception e){
System.out.println(“EOF reached”);
//e.printStackTrace();
}

}

public void writeTransactions(ArrayList<Transaction> transactionList)
{
//
// writing to file transactions.obj
//

try
{
FileOutputStream fos = new FileOutputStream(“transactions.obj”);
ObjectOutputStream oos = new ObjectOutputStream(fos);

Transaction tempTransaction;
for (int i = 0; i < transactionList.size(); i++)
{
tempTransaction = (Transaction) transactionList.get(i);
oos.writeObject(tempTransaction);
}
oos.close();
}
catch (IOException ioe)
{
System.out.println(ioe.toString());
System.out.println(ioe.getMessage());
}
}

public void animate(Transaction currentTransaction, double currentBalance)
{
// set the currentTransaction data member in the bankAccount object
bankAccount.setCurrentTransaction(currentTransaction);

// set the currentBalance data member in the bankAccount object
bankAccount.updateBalance(currentBalance);

repaint();
try
{
Thread.sleep(3000); // wait for the animation to finish
}
catch (Exception e)
{
}
}

public void paint(Graphics g)
{
super.paint(g);
if (bankAccount != null)
{
bankAccount.draw(g);
}
}

public static void main(String[] args)
{
Accounting app = new Accounting();

// Generate an ArrayList of Transaction objects to write to file Transaction.obj
ArrayList<Transaction> transactionList = new ArrayList<Transaction>();
Check c1 = new Check(-500.00);
transactionList.add(c1);
Deposit d1 = new Deposit(3000.00);
transactionList.add(d1);
Withdrawal w1 = new Withdrawal(-400.00);
transactionList.add(w1);
c1 = new Check(-300.00);
transactionList.add(c1);

// write transactions as objects to file transaction.obj
app.writeTransactions(transactionList);

// read transaction.obj, balance the checkbook
app.balanceCheckBook();
}
}

This is the output that was generated during the dry run.

Answered! /* Accounting class Anderson, Franceschi */ import javax.swing.JOptionPane;... 1

This is the list of classes that need to be there for proper running. If things do not work out I would give you all the codes for all the classes as it is which were even had any change, just to it run . Even after arranging all the classes to a seperate package still this program is running fine at my side.

Answered! /* Accounting class Anderson, Franceschi */ import javax.swing.JOptionPane;... 2

Answered! /* Accounting class Anderson, Franceschi */ import javax.swing.JOptionPane;... 3

Response to the question about ObjectWrite process:

All the transaction are first saved in a Transaction list object called transactionList which just an ArrayList<Transaction> object.
-Then the contents of this transactionList is passed as a parameter to a auxiliary function calledwriteTransactions().
– Inside the writeTransactions(), the list is wrapped out and each individual transaction is taken out, then it is written to an object file called transactions.obj, with the use of ObjectOutputStream object.
This is the code that do the actual writing inside writeTransactions() method
oos.writeObject(tempTransaction);

– Below is how the ObjectOutputStream object is initialized in the code.
FileOutputStream fos = new FileOutputStream(“transactions.obj”);
ObjectOutputStream oos = new ObjectOutputStream(fos);

All this is taking place in the code that was shared by me. Though there was no change in this part of the code. Since we were expected to just read from an already saved object file.

Still stressed from student homework?
Get quality assistance from academic writers!