I am trying to write code to: Have a user input their desired salary and the pro
ID: 3638302 • Letter: I
Question
I am trying to write code to:
Have a user input their desired salary and the program output the sales amount needed to earn the commissions to meet that salary. I am getting so confused!?!?!
package salescommissionspart2;
import javax.swing.JOptionPane;
public class SalesCommissionsPart2
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// Commissions needed
final double COMMISSIONS_NEEDED = 30000.00;
double commission_target = 0;
String input = (String)JOptionPane.showInputDialog(null,
"Input your target amount $$: ",
"Input amount",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"100");
input = input.trim();
// Remove the white spaces
if(input != null && input.length() > 0)
commission_target = Double.parseDouble(input);
else
commission_target = 30000.00;
// Declare the commissions
double commissions = 0;
// Declare salesAmount
double salesAmount = 1;
// begin a while loop
while (commissions < commission_target)
{
// Calculate commissions
if (salesAmount >= 10001.00)
// Calculate te commissions if sales are less than 10000.00
{
commissions = 5000.00 * 0.08 + 5000.00 *0.10 +
(salesAmount - 10000.00) * 0.12;
// Use an else if statement
else if (salesAmount >=5001.00)
// Calculate commissions if sales are greater than 5000.00
{
commissions = 5000.00 * 0.08 +
(salesAmount - 5000.00) * 0.10;
}
else
// Use an else statement and calculate the commissions?
commissions = salesAmount * 0.08;
}
// With the salesAmount increasing
salesAmount++;
}
// End the while loop
// Display the sales amount
String output = "A sales amount" + salesAmount + " is needed to" +
"make a commission of $" + commission_target;
JOptionPane.showMessageDialog(null, output, "SALES",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
Explanation / Answer
Hi :)
I just looked at your code and your main problem was messy bracing. You open one of these { and then forget to close it } or close it in the wrong level and mess up your logic, or have one too many in the wrong place and cut your main method scope too early, or not have enough at the end of the program to close your class with... :)))
Here's a corrected version that executes just fine now. Take a deep breath and trace your code slowly next time.
Tip 1: whenever you open { immediately follow up with } then go in between them and write what you want. It will save you hours of headaches later one, believe me, I know :)
Tip 2: if a { } is too long or contains too much stuff , write a comment right after the closing brace } // end of while-loop or end of method X or end of if-else just so you know what you're closing when you get there
Tip 3: proper indentation does wonders for code readability (awkward word I know) so make it a habit, no, a reflex!
Here it is, give it a read and double check with your original code :)
I hope it helps, good luck. Please remember to rate
import javax.swing.JOptionPane;
public class SalesCommissionsPart2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// Commissions needed
final double COMMISSIONS_NEEDED = 30000.00;
double commission_target = 0;
String input = (String) JOptionPane.showInputDialog(null,
"Input your target amount $$: ",
"Input amount",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"100");
// Remove the white spaces
input = input.trim();
if (input != null && input.length() > 0) {
commission_target = Double.parseDouble(input);
} else {
commission_target = 30000.00;
}
// Declare the commissions
double commissions = 0;
// Declare salesAmount
double salesAmount = 1;
// begin a while loop
while (commissions < commission_target) {
// Calculate commissions
if (salesAmount >= 10001.00) // Calculate te commissions if sales are less than 10000.00
{
commissions = 5000.00 * 0.08 + 5000.00 * 0.10
+ (salesAmount - 10000.00) * 0.12;
}
// Use an else if statement
else if (salesAmount >= 5001.00) // Calculate commissions if sales are greater than 5000.00
{
commissions = 5000.00 * 0.08
+ (salesAmount - 5000.00) * 0.10;
} else // Use an else statement and calculate the commissions?
{
commissions = salesAmount * 0.08;
}
// With the salesAmount increasing
salesAmount++;
} // End the while loop
// Display the sales amount
String output = "A sales amount of " + salesAmount +
" is needed to make a commission of $" + commission_target;
JOptionPane.showMessageDialog ( null, output, "SALES",
JOptionPane.INFORMATION_MESSAGE);
System.exit (0);
} // end of main method
} // end of SalesCommissionsPart2 class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.