File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes repeating code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "repeating code" Watch "repeating code" New topic
Author

repeating code

john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
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);

}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: repeating code