# Finding sales amount with loops or do while program

Printable View

• 12-03-2012, 03:46 AM
Nazarius
Finding sales amount with loops or do while program
Hello everyone,
I stucked by an excercise as follows:
(Financial application: finding the sales amount)
You have just started a sales job in a department store. Your pay consists of a base salary and a commission.
The base salary is \$5,000. The scheme shown below is used to determine the commission rate.
Sales Amount Commission Rate
=======================================
\$0.01 - \$5000 8 percent
\$5000-\$10000 10 percent
\$10000.01 and above 12 percent
Your goal is to earn \$30,000 a year. Write a program that finds out the minimum
amount of sales you have to generate in order to make \$30,000.
My java code is as follows:
public class excercise4_39 {
public static void main (String[]Args){
String commissionS = JOptionPane.showInputDialog("Enter the amount of the commission you would like to have:");
int desiredCommission = Integer.parseInt(commissionS)+1;
int salesAmount = 0;
double commissionRate = 0;
double commissionAmount = 0;
System.out.println("Sales Amount" + "\t\t" + "Commission Rate" + "\t\t" + "Commission Amount");
while(commissionAmount <= desiredCommission ){
for (salesAmount=1; salesAmount <= 500000;salesAmount++){
if(salesAmount <= 5000){
commissionRate = 0.08;
}
if(salesAmount >= 5001 && salesAmount <= 10000){
commissionRate = 0.10;
}
if(salesAmount >= 10000){
commissionRate = 0.12;
}

salesAmount += 4999;
commissionAmount = salesAmount * commissionRate;

System.out.println(salesAmount + "\t\t\t" + commissionRate + "\t\t\t" + commissionAmount);
}
}
}
}
But eventough I have written the while loop code for it, the sales amount won't stop by the desired commission of 30.000!
Could anyone please help me?
TYVM!
• 12-03-2012, 07:43 AM
wsaryada
Re: Finding sales amount with loops or do while program
First, please reformat your code post using the [code][/code] tag.

You should debug your program so that you can understand how it run. Why it doesn't stop at 30000 because your for-loop tell the computer to do a loop from 1 to 500000. If you want to stop at 30000 you should break the loop. And you don't need the while-loop for this. Just add a condition check in the for-loop and break when the required condition is reached.
• 12-04-2012, 12:07 AM
Nazarius
Re: Finding sales amount with loops or do while program
Hallo wsaryada,

I've solved it! finally, thank you for the help, but I have made mistake by adding +1 in my variabel desiredCommission :(blush):
Keep contact! My final code:

Code:

```import javax.swing.JOptionPane; public class excercise4_39 {         public static void main (String[]Args){                 String commissionS = JOptionPane.showInputDialog("Enter the amount of the commission you would like to have:");                 int desiredCommission = Integer.parseInt(commissionS);                 int salesAmount = 0;                 double commissionRate = 0;                 double commissionAmount = 0;                 System.out.println("Sales Amount" + "\t\t" + "Commission Rate" + "\t\t" + "Commission Amount");                                 for (salesAmount=1; salesAmount <= 500000;salesAmount++){                 if(salesAmount <= 5000){                         commissionRate =  0.08;                 }                 if(salesAmount >= 5001 &&  salesAmount <= 10000){                         commissionRate = 0.10;                 }                 if(salesAmount >= 10000){                         commissionRate = 0.12;                        }                        salesAmount += 4999;                 commissionAmount = salesAmount * commissionRate;                 System.out.println(salesAmount + "\t\t\t" + commissionRate + "\t\t\t" + commissionAmount);                 if(commissionAmount==desiredCommission){                         break;}                 }                 JOptionPane.showMessageDialog(null, "To earn a commission amount of €" + desiredCommission + " , Your sale amount has to reach €" + salesAmount);         } }```
• 12-04-2012, 12:41 AM
Nazarius
Re: Finding sales amount with loops or do while program
Sorry, this is the more perfect code:

Code:

```import javax.swing.JOptionPane; public class excercise4_39 {     public static void main (String[]Args){         String commissionS = JOptionPane.showInputDialog("Enter the amount of the commission you would like to have:");         int desiredCommission = Integer.parseInt(commissionS);         int salesAmount = 0;         double commissionRate = 0;         double commissionAmount = 0;         System.out.println("Sales Amount" + "\t\t" + "Commission Rate" + "\t\t" + "Commission Amount");           for (salesAmount=1; salesAmount >= commissionAmount;salesAmount++){         if(salesAmount <= 5000){             commissionRate =  0.08;         }         if(salesAmount >= 5001 &&  salesAmount <= 10000){             commissionRate = 0.10;         }         if(salesAmount >= 10000){             commissionRate = 0.12;            }            salesAmount += 999;         commissionAmount = salesAmount * commissionRate;         System.out.println(salesAmount + "\t\t\t" + commissionRate + "\t\t\t" + commissionAmount);         if(commissionAmount>=desiredCommission){             break;}         }         JOptionPane.showMessageDialog(null, "To earn a commission amount of €"  + desiredCommission + " , Your sale amount has to reach about €" +  salesAmount);     } }```
I hope it will bring benefit to someone :)-: