• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

repeating code

 
Greenhorn
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 calculates real estate commission. The person inputs the sale amount and then enters a letter which represents a certain commission rate. It then calculates the commission and displays it. After the commission amount is displayed i want it to prompt the user for another transaction. When there are no more transactions i want the program to display the total amount of the properties entered, and the total amount of the commissions calcualted. I'm not too sure what to do. Should i use a while loop? 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;

// 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;
else{
JOptionPane.showMessageDialog (null,
"Error: Wrong property code, please try again",
"Calculator Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}



// Calculate commission
double commission = salePrice * commissionRate;

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


// Show results
JOptionPane.showMessageDialog(null,
"the commission is" + commission,
"Calculator Output",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

[ EJFH: Removed a lot of extra whitespace ]
[ May 20, 2005: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Posts: 24207
46
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
Yes, you should use a while loop enclosing more or less all the code you've got here. You could just use "while(true)", and then use a "break" statement to break out of the loop if (say), the sale price is entered as 0.

Now, to compute the running sums, you'll need some more variables, defined outside the loop, initialized to zero. Inside the loop, each time through, after you calculate results, add the calculated amounts to the appropriate sums. Then, after the loop, you can print your results.

I bet that once you get this running in a loop, you're going to find it rather irritating the way the separate option boxes pop up for each question. At that point, you'll want to consider moving your questions over to our Swing/AWT forum, and learning something about building a real GUI.

Good luck!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll assume what you have runs and gives correct results ...

I'd first move that code out of main() and into a regular method. That would look like:

This makes it possible for other classes to use the calculator. And yes your "while loop" idea is right on the money. So some new class might say:

Let me know if this is the first time you've had one class calling another. Did it make sense? How about the while loop?
 
john corsic
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually want the program to ask the person if they would like to do another transaction. If they type in YES then the program will repeat. This is what i've done so far, but i'm still a little confused as how to repeat the program.

// 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;


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

salePrice = Double.parseDouble(salePriceString);


while(salePrice == 0) continue;


// 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;

// Calculate commission
double commission = salePrice * commissionRate;

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


// Show results
JOptionPane.showMessageDialog(null,
"the commission is" + commission,
"Cakculator Output",
JOptionPane.INFORMATION_MESSAGE);

String transactionString = JOptionPane.showInputDialog (null,
"Would you like to do another transaction (YES or No)",
"Calculator Input", JOptionPane.QUESTION_MESSAGE);

while (transactionString.equals ("YES"))

System.exit(0);

}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic