Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The framework source file you are to modify is the Accounting.java file. There is only 1 part to change. As usual, it is indicated by

The framework source file you are to modify is the "Accounting.java" file. There is only 1 part to change. As usual, it is indicated by a comment block that starts with 5 asterisks (*****). As explained there, and in your textbook, you are to provide the entire body of the "balanceCheckBook()" method. Starter code ------------ Use the try-with-resources form of the "try" statement as shown in this week's outline. To get you started on the right foot, here is the code for the local variables and the beginning of the "try" block: double balance = 0.00; double currentAmount; Transaction transaction; try (FileInputStream fis = new FileInputStream("transactions.obj"); ObjectInputStream ois = new ObjectInputStream(fis);) // fis and cis are resources that will be automatically closed. { "while" loop ------------ Within the "try" block, create a "while" loop to process the object file. The "while" loop's condition is (true). For each iteration of the "while" loop, get one object from the object file, assigning it to the "transaction" variable. To do this, use the "ois" object and its "readObject()" function. In this assignment statement, explicitly cast the returned "Object" type to a "Transaction" object type (on the right hand side of the assignment operator). After obtaining the "transaction" object, use its "getAmount()" function to assign its amount to "currentAmount". Then use the new value of "currentAmount" to update "balance". Next, call "animate" as detailed in the framework comments. Since, we are using the try-with-resources form of the "try" statement, there is no need to provide a statement to close the object file, because that will be done automatically. This also means, only a single "try" statement is needed. There is no need for another (nested) "try" statement. Since the "while" loop condition should be "while (true)", it will just keep reading objects from the file until an EOFException causes it to cease, at which point control will be transferred to the EOFException catch block. "catch" blocks -------------- The "try" block should be followed by 4 "catch" blocks for the following exceptions in the order shown: a) FileNotFoundException In this catch block, display the following message to the system console: "The file transactions.obj was not found." b) ClassNotFoundException In this catch block, display the following message to the system console: "A ClassNotFoundException was encountered." Normally, the "ClassNotFoundException" exception could be used to flag and ignore individual corrupted objects in the input object file. This would require using a second (nested) "try" block inside the "while" loop. However, since the provided input object file, transactions.obj", has no corrupted objects that could cause this exception, and to avoid making things overly complicated for this activity, do not take that approach. c) EOFException In this catch block, display the following message to the system console: "The end of the file was reached." d) IOException In this catch block, display the following message to the system console: "An IOException was encountered." End-of-file exception --------------------- Here is the code for the end of file "catch" block: // An EOF exception is part of normal processing: catch (EOFException eofe) { System.out.println("The end of the file was reached."); } This "catch" block is executed when the code in the "try" block encounters the end of the input file. After this "catch" block executes, the program flow continues with the code after the last "catch" block. This trailing code is where you display messages to the user on the system console and in a pop-up window. Because we are using the "try-with-resources" form of the "try" statement, there is no need for a "finally" block. Output messages --------------- After the "catch" blocks, display both the console and the pop-up dialog messages as shown in the "Example Programming Activity 11-2 Output" document on our course web page. Skip optional part ------------------ Skip the optional part of the textbook instructions that is labeled "If you have time...". Watch the related video ----------------------- This assignment involves some complex concepts. However, given all that has been provided, it really is an exercise in following instructions, using what you have been given, and doing things step-by-step in the correct ORDER and syntax. Don't forget that there is also a video provided about reading objects from a file. Your solution should be short ----------------------------- The only code in the "try" block is the "while" block mentioned in the instructions. Not counting comments, blank lines, or the "animate" call, the "while" block can easily be coded with 3 simple lines of code--each mentioned in the instructions. There is 1 line of code inside each of the catch blocks. Each of these writes a message to the system console as detailed in the instructions. Make sure you put your catch blocks in the correct ORDER, as given in the instructions. The code following the last catch block needs only 3 statements to display the ending balance to the system console and in a pop-up window.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 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;
 
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 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 transactionList = new ArrayList();
 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 ); } 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Georgia Military College: Log in to the site

Skip to main content

Georgia Military College

Username Password

Remember username

Log in

Forgotten your username or password?

Cookies must be enabled in your browser

You are not logged in.

Home

Switch to the standard theme

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* 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 ); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

1. What are your creative strengths?

Answered: 1 week ago