Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Nazarius

Thread: Finding sales amount with loops or do while program

  1. #1
    Nazarius is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Post 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!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

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

  3. #3
    Nazarius is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default 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
    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. #4
    Nazarius is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default 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
    DarrylBurke likes this.

Similar Threads

  1. Replies: 12
    Last Post: 08-04-2012, 09:35 PM
  2. program ,deposit of an amount of money in a bank
    By aou in forum Advanced Java
    Replies: 1
    Last Post: 08-03-2012, 09:41 PM
  3. Replies: 1
    Last Post: 03-06-2011, 01:16 PM
  4. Replies: 4
    Last Post: 11-03-2010, 06:17 PM
  5. Adding numbers for sales reciept program(Biginner)
    By waklo99 in forum New To Java
    Replies: 10
    Last Post: 03-18-2010, 04:09 AM

Posting Permissions

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