• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

displaying total amounts

Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've created a program that computes real estate commissions. The program prompts the user if they want to do a transaction. It then prompts them for the sale price and the property type (the property type corresponds with a certain rate). The program then calculates the commission and displays it. After the commission is displayed the program asks if you would like to do another transaction and continues to do so until the user selects no.
What I want to do is when the user selects no I want the program to display the total amount of the properties entered, and the total amount of the commissions calculated (both formatted as currency).

Also, if you have time. When the user enters an incorrect property type I want the program to say that it is an incorrect property type and to enter another one. I know how to display the message I just don't know how to go back to ask for the property type again.

Here is what i have done so far:

// RealEstateCommission.java: Calculates real estate commissions
import javax.swing.JOptionPane;

public class Calculator {
/** Main method */
public static void main(String[] args) {
double salePrice;
double commissionRate = 0;

while ( JOptionPane.showConfirmDialog(
"Perform a transaction?",
JOptionPane.YES_OPTION ) {

// Enter sale price
String salePriceString = JOptionPane.showInputDialog (null,
"Enter sale price",
"Calculator Input", JOptionPane.QUESTION_MESSAGE);

salePrice = Double.parseDouble(salePriceString);

// Enter property code (residential, multidwelling or commercial)
String propertyTypeString = JOptionPane.showInputDialog (null,
"Enter property code (residential (R), multidwelling (M) or commercial(C))",
"Calculator Input", JOptionPane.QUESTION_MESSAGE);

if (propertyTypeString.equals ("R"))
commissionRate = 0.070;
else if (propertyTypeString.equals ("M"))
commissionRate = 0.060;
else if (propertyTypeString.equals ("C"))
commissionRate = 0.035;
JOptionPane.showMessageDialog (null,
"Error: Wrong property code, please try again",
"Calculator Output", JOptionPane.INFORMATION_MESSAGE);



// Calculate commission
double commission = salePrice * commissionRate;

// Format to keep two digits after the decimal point
commission = (int)(commission * 100) / 100.0;

// Show results
"the commission is" + commission,
"Calculator Output",


author and iconoclast
Posts: 24207
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outside of your big while loop, declare variables to hold the two totals and initialize them to zero. Inside your while, at the end, add the numbers for that iteration to the totals. Then after your loop, display those totals (look at the java.text.DecimalFormat class for some help with formatting numbers.)

Regarding the "ask again": Make another while loop, that encloses the prompt for property type and the if-else chain. Each branch should use "break" to terminate this while, except for the last one which prints the error message. This one should allow the loop to repeat and prompt again.
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to quote Jeff Bosch from what looks like your previous post,

...could you please use the CODE UBB tags to enclose any code in future posts? Doing so maintains the formatting and makes reading it a whole lot easier.

Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic