Create a new method in the account called displaystatement


Polishing the Output
This assignment continues the bank account application from Bank Part 2. In this part, we will polish up the appearance of the monthly statement. To do this we will have to play with output formatting. Read the separate file called NotesOnFormattingInJava to learn about using the String.format method to produce a formatted string.
In Part 2, the output of displayTransactions looked like the following:
1 500.0 deposit
3 -8.99 Burger King
7 -67.43 Sprint Wireless
17 -40.0 ATM on Elm Street 383.58

In Part 3, we want the output of displayStatement to look like the following:

day begin

amount

$0.00

description opening balance

1

$500.00

deposit

3

-$8.99

Burger King

7

-$67.43

Sprint Wireless

17

-$40.00

ATM on Elm Street

end

$383.58

closing balance

To make these changes, take small steps. What should we do, and in what order? Start by looking at the differences between the current implementations in the Account and Transaction classes (the as-is) and the new output (the to-be). Identify the changes (the gap). The differences are the lines with the opening and closing balance, the line with column labels, handling of the $ sign and - signs for deposits and withdrawals, and the evenly aligned column output.

Step 1
Create a new method in the Account called displayStatement(). To start, just have it print the line "day amount description", and then copy the loop found in displayTransactions().

Step 2
Add an openingBalance property to the Account class. The opening balance needs to be set to the same value as the balance at the beginning. We could change the constructor to accept an opening balance. For now, it is sufficient to let the constructor initialize the balance to 0.0. Right after the balance has been set, add another line to set openingBalance = balance;
Add a line in displayStatement() to show the opening balance, with the text
" opening balance" afterwards. At this point, your output should look like the following:
day amount description begin 0.0 opening balance
1 500.0 deposit
3 -8.99 Burger King
7 -67.43 Sprint Wireless
17 -40.0 ATM on Elm Street end 383.58 closing balance

Step 3
The next step, start adding a little formatting to the output. To do this, create a new method in the Transaction class that looks like toString(), but name it toFormattedString(), and use that for printing Transactions in the Account's displayStatement method.
In this first little step, just make all the currency amounts appear with dollar signs and two decimal places. Since we have to do this several times, and not just in the toFormattedString method, you should put the code for formatting currency in a separate method that can be called when needed. Give the Transaction class a static method called formatCurrency() that takes the amount as an argument, and returns a String. You can use this method directly in the Transaction class's toFormattedString method. To use it from outside the Transaction class in the displayStatement method, include it's Class name prefix: Transaction.formatCurrency(openingBalance).
Before continuing with this step you should read the NotesOnFormattingInJava document to learn how to use the String.format() method. There is also a section that describes an alternative way of formatting currency that involves using a factory method in the Java NumberFormat class.

When you have this step working, your output should look like the following. The version on the left used String.format(). The version on the left used the currency feature of NumberFormat.

day amount description begin $0.00 opening balance 1 $500.00 deposit
3 $-8.99 Burger King
7 $-67.43 Sprint Wireless
17 $-40.00 ATM on Elm Street end $383.58 closing balance

day amount description begin $0.00 opening balance 1 $500.00 deposit
3 ($8.99) Burger King
7 ($67.43) Sprint Wireless
17 ($40.00) ATM on Elm Street end $383.58 closing balance

Step 4
The desired output for withdrawal (negative amount) transactions is not what we want. In this step you should add or change the code to produce output that is closer to the desired result. Use if and else conditions in your formatCurrency method in order to do something different for negative and positive amounts.
The output should now appear as follows:
day amount description begin $0.00 opening balance 1 $500.00 deposit
3 -$8.99 Burger King
7 -$67.43 Sprint Wireless
17 -$40.00 ATM on Elm Street end $383.58 closing balance

Step 5
In this step, address the issue of making the output appear in columns. Start by focusing on the toString() method of the Transaction class. Adjust the format string used with String.format() until the output looks like the following:
day amount description begin $0.00 opening balance
1 $500.00 deposit
3 -$8.99 Burger King
7 -$67.43 Sprint Wireless
17 -$40.00 ATM on Elm Street end $383.58 closing balance
Notice that the descriptions must be left justified (which is explained in the Notes document).

Step 6
The final step is to get the other lines to line up with the new column widths. For the column labels, you can use of String.format() or just put spaces in the string until they line up.
Now the output should look as follows:

day begin amount
$0.00 description opening balance
1 $500.00 deposit
3 -$8.99 Burger King
7 -$67.43 Sprint Wireless
17 -$40.00 ATM on Elm Street
end $383.58 closing balance
Pay attention to the details! Count the spaces. There are 2 spaces between the right justified amount and the left justified description, not just 1. When you work somewhere and they give you requirements, every detail matters, including things like the precise number of spaces.
If you wish, you may add further improvements, like adding the customer name and address at the top, or making the labels appear in all upper case. Then turn in this final assignment.

Conclusion
This exercise was intended to show you how to design a solution by dividing the problem into separate concerns, by forming separate classes with small methods, and by solving one small problem at a time. When we started, the idea of creating a bank account application with transactions and a monthly statement would have seemed hopelessly complicated. But we did it with much less complication, one concern at a time.

When you have a programming problem, do not try to solve the problem all at once.

Remember to think about dividing the problem into smaller concerns. Imagine an architecture of objects, using classes where each class takes care of its own concerns.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a new method in the account called displaystatement
Reference No:- TGS02381307

Now Priced at $15 (50% Discount)

Recommended (95%)

Rated (4.7/5)