Problem: I need to add code to my program allowing the user to enter their desir
ID: 3638123 • Letter: P
Question
Problem: I need to add code to my program allowing the user to enter their desired salary so my program outputs to them the amount of sales they need to earn that amount of commission. Can you enter the code in different color please, lol. Thanks community!!
// Use javax.swing.JoptionPane
import javax.swing.JOptionPane;
public class HWAssignment2SaleAmount {
// This is where the Main method begins
public static void main(String[] args) {
// Commisions needed
final double COMMISSIONS_NEEDED = 30000;
// Declare commissions
double commissions = 0;
// Declare salesAmount
double salesAmount = 1;
//start while loop
while (commissions < COMMISSIONS_NEEDED) {
//Calculate commissions
if (salesAmount >= 10001)
// calculate the commissions if sales are less than 10000
commissions = 5000 * 0.08 + 5000 * 0.10 + (salesAmount - 10000) * 0.12;
else if (salesAmount >= 5001)
// Calculate commissions if sales are greater than 5000
commissions = 5000 * 0.08 + (salesAmount - 5000) * 0.10;
else
// calculate the commissions
commissions = salesAmount * 0.08;
// with increasing salesAmount
salesAmount++;
} //end of while loop
// Display the sales amount
String output = "The sales amount " + salesAmount + " is needed to make a commission of $" + COMMISSIONS_NEEDED;
JOptionPane.showMessageDialog(null, output,"SALES", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
} // End the Main method
} // End the class
Explanation / Answer
Hi, I was a little confused by the way your question is phrased vs. what's written in the code (salary vs. commission...?), so I just used my own judgment and added another JOptionPane that prompts the user to imput their target commission amount then the sales are computed and the second JOptionPane displays how many sales are needed for them to reach that commission quota. In case the user inputs a blank or nothing at all, it reverts to the default $30000 commission computation - feel free to display an error message or do any other error handling as you see fit, I just went for simplicity there :)
If something is missing or not clear, just PM me and we can look at it again. I highlighted the changes/additions to the code.
I hope it's helpful, please remember to rate !
import javax.swing.JOptionPane;
public class HWAssignment2SaleAmount {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Commisions needed
//final double COMMISSIONS_NEEDED = 30000;
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 white spaces
if(input != null && input.length() > 0)
commission_target = Double.parseDouble(input);
else
commission_target = 30000;
// Declare commissions
double commissions = 0;
// Declare salesAmount
double salesAmount = 1;
//start while loop
while (commissions < commission_target) {
//Calculate commissions
if (salesAmount >= 10001) // calculate the commissions if sales are less than 10000
{
commissions = 5000 * 0.08 + 5000 * 0.10 + (salesAmount - 10000) * 0.12;
}
else if (salesAmount >= 5001) // Calculate commissions if sales are greater than 5000
{
commissions = 5000 * 0.08 + (salesAmount - 5000) * 0.10;
}
else // calculate the commissions
{
commissions = salesAmount * 0.08;
}
// with increasing salesAmount
salesAmount++;
} //end of while loop
// Display the sales amount
String output = "The sales amount " + salesAmount + " is needed to make a commission of $" + commission_target;
JOptionPane.showMessageDialog(null, output, "SALES", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.