Results 1 to 9 of 9
  1. #1
    sjan is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default Percentages in Switch Case Statement

    This is some of the code I am working on. My main problem is that even if I enter a number that is higher than 10,000.00 it still will give the commissionRate as 5.0% instead of 7.5%. Can anyone help me? Thanks



    System.out.printf("Greetings %s! What is your current status as an employee?%n", userName);
    System.out.println("Enter 'P' for part-time and 'F' for full-time.");
    status = input.nextLine();
    status = status.toUpperCase();


    switch (status){
    case "P":
    if (totalSales >= 0.0);
    commissionRate = 3.5;
    break;

    case "F":
    if (10000.00 > totalSales)
    commissionRate = 5.0;

    else
    commissionRate = 7.5;
    break;
    }

    Excerpt from console run:
    (Your total sales profit for this month's payment period is 120000.0.
    You said that you status was F.
    Based upon this, your commission rate is 5.0%. {Should be 7.5!}
    Using this rate, you total commission earned is 6000.0.)

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Percentages in Switch Case Statement

    Two comments on this piece of code, neither of which solve your problem, but I thought I'd mention it:
    1) The first if (in case "P") is not doing what you think it does. Statement commisionRate=3.5 will ALWAYS be executed, due to the semi-colon behind the if statement. The if block is empty. Make it a habit to enclose if and else parts in curly braces, so you want fall in this trap.
    2) A comparison like this: 10 > variabele is making my brain read that twice. Don't make my brain work. It would be easier to read and less error prone to say variable < 10. Other coders might disagree, just sayin'...

    Java Code:
    System.out.printf("Greetings %s! What is your current status as an employee?%n", userName);
    System.out.println("Enter 'P' for part-time and 'F' for full-time.");
    status = input.nextLine();
    status = status.toUpperCase();
    
    switch (status) {
        case "P":
            if (totalSales >= 0.0) {
                commissionRate = 3.5;
            }
            break;
        case "F":
            if (totalSales < 10000) {
                commissionRate = 5.0;
            }
            else {
                commissionRate = 7.5;
            }
            break;
    }
    As for your issue, it doesn't appear to be in the switch part. Can you post the entire piece of code so we can run and test it?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Percentages in Switch Case Statement

    Quote Originally Posted by SurfMan View Post
    The if block is empty. Make it a habit to enclose if and else parts in curly braces, so you want fall in this trap.
    That doesn't guarantee anything :)
    Java Code:
    if (condition); {
        doSomething();
    }
    Compiles, runs without error, just doesn't do what was evidently intended.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Percentages in Switch Case Statement

    Quote Originally Posted by DarrylBurke View Post
    That doesn't guarantee anything :)
    Java Code:
    if (condition); {
        doSomething();
    }
    Compiles, runs without error, just doesn't do what was evidently intended.

    db
    You are correct. Dammit. Let's rephrase that into: semi-colons on the same line where an if statement is, is a code-smell. :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Percentages in Switch Case Statement

    Of course, for part-time work, why even test for total sales? How can you have negative sales?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    sjan is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default Re: Percentages in Switch Case Statement

    /**
    * FILE : SalesCommissionPayment
    * PURP : Calculate how much commission a salesman earns
    * @author Sarah Janaskie
    *
    */

    package edi.janaskie;

    import java.util.Scanner;


    public class SalesmenCommissionPayment {

    public static void main(String[] args) {

    Scanner input = new Scanner (System.in);

    //USER INFORMATION
    String userName = "";
    double totalSales = 0.0;
    String status = "";

    //COMMISSION INFORMATION
    double commissionRate = 0.0;
    double commissionEarned = 0.0;
    double companyProfit = 0.0;



    System.out.println("Welcome valued employee! Here at ACME Hammers Inc., it is our goal to not only take care of our customers but also you.");
    System.out.println("Without you we would have no customers! This is why we offer you a commission.");
    System.out.println("Within just a couple of questions, we will acurately be able to calculate this for you.");

    System.out.println("To get started, please enter your name (first and last) so that your commission can be successfully transferred to your bank account.");
    userName = input.nextLine();

    System.out.printf("Greetings %s! What is your current status as an employee?%n", userName);
    System.out.println("Enter 'P' for part-time and 'F' for full-time.");
    status = input.nextLine();
    status = status.toUpperCase();


    switch (status){
    case "P":
    if (totalSales >= 0.0);
    commissionRate = 3.5;
    break;

    case "F":
    if (totalSales < 10000.00)
    commissionRate = 5.0;

    else if (totalSales >= 10000.00)
    commissionRate = 7.5;
    break;
    }

    System.out.println("Finally, what is the total sales for this months time period?");
    totalSales = input.nextDouble();

    commissionEarned = (commissionRate * totalSales) / 100;
    companyProfit = totalSales - commissionEarned;

    System.out.printf("Thank you for your answers %s. We have now successfully calculated your commission earned.%n", userName);
    System.out.println("See below:");

    System.out.printf("Your total sales profit for this month's payment period is %.1f.%n", totalSales);
    System.out.printf("You said that you status was %s.%n", status);
    System.out.printf("Based upon this, your commission rate is %.1f%%.%n", commissionRate);
    System.out.printf("Using this rate, you total commission earned is %.1f.%n", commissionEarned);

    System.out.printf("Thank you for you hard work %s! Your efforts have earned our company %.1f.", userName, companyProfit);
    System.out.println("This money will be put to good use to grow our company so that we can provide better service to our customers and to you!");





    }

    }

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Percentages in Switch Case Statement

    You reposted your code but didn't ask a question or fix the problem reported by others. And please place your code between code tags for proper formatting.
    Read about it here -> BB Code List - Java Programming Forum - Learn Java Programming

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    sjan is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default Re: Percentages in Switch Case Statement

    My apologies, one of the administrators had requested that i post my entire code so that they could run it and test it. Next time i will use the code tags.

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: Percentages in Switch Case Statement

    one major issue is nowhere your totalsales value your are receiving.its always initialized value 0.0.check once.
    Ramya:cool:

Similar Threads

  1. If-Else vs Switch-Case
    By R03 in forum New To Java
    Replies: 9
    Last Post: 04-29-2014, 01:56 AM
  2. switch case help! :(
    By domtay in forum New To Java
    Replies: 12
    Last Post: 12-18-2013, 08:43 AM
  3. switch case help! :(
    By domtay in forum Advanced Java
    Replies: 1
    Last Post: 12-17-2013, 11:34 AM
  4. Replies: 9
    Last Post: 02-05-2012, 11:02 PM
  5. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 01:18 PM

Tags for this Thread

Posting Permissions

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