Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

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

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 ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

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 ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

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. ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

  • 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