# Thread: Finding sales amount with loops or do while program

1. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## 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!
TYVM!

2. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
10

## Re: Finding sales amount with loops or do while program

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.
Last edited by wsaryada; 12-03-2012 at 07:47 AM.

3. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: Finding sales amount with loops or do while program

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

Java 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);
}
}```

4. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: Finding sales amount with loops or do while program

Sorry, this is the more perfect code:

Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•