Create a new method in the account

Assignment Help JAVA Programming
Reference no: EM131585764

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.

Reference no: EM131585764

Questions Cloud

Compare two resources available for a family of four : The Robinson family consists of a mother and her three children: a 14-year-old boy, an 11-year-old girl, and a 3-month-old infant.
What is the name of the election whereby employees decide : What is the name of the election whereby employees decide if a union will represent them in their dealings with management
How resources and capabilities influence competitive dynamic : 1. Explain how resources and capabilities influence competitive dynamics.
Develop tax research project based upon specific tax service : Develop a tax research project, based upon specific tax service situations and issues. Provide an annotated bibliography for your selection.
Create a new method in the account : 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
Strong brand and bring value to the target market : What marketing actions did CEO Gert Boyle take to make a strong brand and bring value to the target market?
What is the smallest amount of cable you can use : Back at the University of Universe City (see Example), you have been asked to network together the computer just inside the door of the Computer Science.
Describe the role of prices in market economies : What is a competitive market? Briefly describe a type of market that is not perfectly competitive. Describe the role of prices in market economies
Psychological and physical problems : Explain the importance of assessing both psychological and physical problems during the course of treating a client.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd