Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M92394478
  • Price:- $15

Priced at Now at $15, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As