repeating program code  
Author Message
reimerchris





PostPosted: 2005-5-22 15:22:00 Top

java-programmer, repeating program code 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;

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

}
}

 
Andrew Thompson





PostPosted: 2005-5-22 16:12:00 Top

java-programmer >> repeating program code On Sun, 22 May 2005 03:22:14 -0400, reimerchris wrote:

> Should i use a while loop?

It can work for you. See the reworked example below..

BTW - Please keep the line lengths shorter when posting to usenet,
and examine the comments carefully.

<sscce>
// RealEstateCommission.java: Calculates real estate commissions
/* This is wrong for a start!The class is called 'Calculator'
and either the comment or the class name should be changed. */
import javax.swing.JOptionPane;

public class Calculator {

/** Main method */
/* please indent code with 2 or 3 spaces */
public static void main(String[] args) {
double salePrice;
double commissionRate = 0;

/** A 'confirmation' dialog is better for this */
while ( JOptionPane.showConfirmDialog(
null,
"Perform a calculation?",
"Continue",
JOptionPane.YES_NO_OPTION)==
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);

// typing capitals is a drag..
if (propertyTypeString.equalsIgnoreCase ("R"))
commissionRate = 0.070;
else if (propertyTypeString.equalsIgnoreCase ("M"))
commissionRate = 0.060;
else if (propertyTypeString.equalsIgnoreCase ("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;
/* This does not work you often get a single digit -
see this example.. */
for (double ii = 0; ii<1000; ii+=0.333) {
System.out.println(
"rate 1: " +
((int)(ii*0.70 * 100) / 100.0) +
" \t2: " +
((int)(ii*0.60 * 100) / 100.0) +
" \t3: " +
((int)(ii*0.35 * 100) / 100.0)
);
}
/* See 'NumberFormat' instead..
http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html */

// Show results
JOptionPane.showMessageDialog(null,
"the commission is" + commission,
"Cakculator Output",
JOptionPane.INFORMATION_MESSAGE);
}
System.exit(0);
}
}
</sscce>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane