wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes calculating commission Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calculating commission" Watch "calculating commission" New topic
Author

calculating commission

john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
I'm trying to create a program that will calculate real estate commission. The user must enter a sale price and a property code. There are 3 property codes (R,M,C) and each one represents a certain commission rate. I've been trying to figure it out, but i must be missing something. Take a look and let me know:

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

public class Calculator{
/** Main method */
public static void main(String[] args) {
double salePrice;
char propertyType = ('R', 'M', 'C');
double commissionRate;


// 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 (propertyType == 'R')
commissionRate = 0.070;
else if (propertyType == 'M')
commissionRate = 0.060;
else if (propertyType == 'C')
commissionRate = 0.035;
else{
JOptionPane.showMessageDialog (null,
"Error: Wrong property code, please try again",
"Calculator", 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);

}

}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

A String and a char are different types; comparing a String and a char using "==" will give you a compilation error about "incompatible types."

Your choices are to either convert the input String to a char, and then use == for the comparison, or to use double-quoted Strings for the rate codes instead, and use the equals() method of String for the comparison. I'd recommend the latter, in case there's ever a multicharacter rate code.



[Jess in Action][AskingGoodQuestions]
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
Thanks ernest for the help, I did what you said, but it still doesn't work. Here is what it looks like now and i've also included the error message at the end.


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

public class Calculator{
/** Main method */
public static void main(String[] args) {
double salePrice;
char propertyType = ('R', 'M', 'C');
double commissionRate;


// 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", JOptionPane.QUESTION_MESSAGE);




if (propertyType.equals ("R"))
commissionRate = 0.070;
else if (propertyType.equals ("M"))
commissionRate = 0.060;
else if (propertyType.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);

}

}


C:\JAVA\Assignment2_1tests.java:8: ')' expected
char propertyType = ('R', 'M', 'C');
^
1 error

Tool completed with exit code 1
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by john corsic:
I'm trying to create a program that will calculate real estate commission. The user must enter a sale price and a property code. There are 3 property codes (R,M,C) and each one represents a certain commission rate. I've been trying to figure it out, but i must be missing something. Take a look and let me know:
...

So what problems did you encounter with this code? Did it compile? It looks like EFH took some time to find out that it doesn't. Once you've followed his suggestions above, please post any other compiler errors you get. If it compiles fine, then you should explain what happens when you run the program, especially if it does something unexpected. The more details like this that you can provide, the more quickly we will be able to answer your questions.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

C:\JAVA\Assignment2_1tests.java:8: ')' expected
char propertyType = ('R', 'M', 'C');
^
1 error


The propertyType variable is a character variable. To assign it, you need something that is a character -- which the expression on the right side of the equals sign clearly is not.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
What wuuld you suggest?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I would suggest eliminating the propertyType variable altogether since I don't think you use it for anything; the tests you're running on "propertyType," you actually really intend to run on "propertyTypeString," right?
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
Yes, that's what i intended.
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
how would i go about doing this?
Sorry for all the questinons, I've just gotten into java and am a little confused
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Just delete the declaration of "propertyType", then change "propertyType" to propertyTypeString everywhere. It's likely there are more compiler errors to be found after this one is fixed: try to attack them one at a time, reading the messages carefully.

Here's a suggestion for the next program: write a few lines. Compile it. If it doesn't compile, fix it. Then test it, somehow or other, and make sure it does what you expect.

Then when it works, add a few more lines, and repeat the cycle. This kind of iterative approach is much easier than the "build it, push it off a cliff, see if it flies" approach.
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
C:\JAVA\Calculator.java:42: variable commissionRate might not have been initialized
double commission = salePrice * commissionRate;
^
1 error

Tool completed with exit code 1

I did that and this is the error i get.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, so, as I said, read the error message. It says that "commissionRate" might not have been initialized. That means that at this line of the program, it's possible that commisionRate has never had a value assigned to it. In Java, it's illegal to use a variable that's never been assigned to. So look at the code, and make sure that, whatever happens, commissionRate gets a value before this line of code runs.

Note that the Java compiler doesn't know that System.exit() never returns.
john corsic
Greenhorn

Joined: May 14, 2005
Posts: 13
Thank you very much Ernest for all the help. I finally figured it out thanks to your guideance.

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calculating commission