#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Need to create a java program that includes if and case statements.


    Here is what I have so far. I need to include if statements, cases and discounts. I'm confused as to where and how to put the discounts. Any help is appreciated.

    Scanner keyboard = new Scanner(System.in);

    String accountNumber; //customer's account number
    String name; //Customers first and last name
    String address; // customers address
    double kwh; //customers KWH-whole number

    System.out.print("Please enter your 14 digit account number");
    accountNumber = keyboard.nextLine();

    System.out.print("Please enter your first and last name");
    name = keyboard.nextLine();

    System.out.print("Please enter your address-street, city, state and zip code");
    address = keyboard.nextLine();

    System.out.print ("Please enter your discount code -" +
    " enter SR for senior citizen," +
    " LI for low income, NA for no discount");

    System.out.print("Is your payment late? Y/N");


    System.out.print("Please enter your KWH-usage of Kilowatt Hours-"
    + " as a whole number");
    kwh = keyboard.nextDouble();
    double price = (kwh *.0624);
    if (SR = double price * .10);
    if (LI = double price * .25);
    if (NA = double price null);
    if (Y = double price * .15 );
    }
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by jjava
    Here is what I have so far. I need to include if statements, cases and discounts. I'm confused as to where and how to put the discounts. Any help is appreciated.

    Scanner keyboard = new Scanner(System.in);

    String accountNumber; //customer's account number
    String name; //Customers first and last name
    String address; // customers address
    double kwh; //customers KWH-whole number

    System.out.print("Please enter your 14 digit account number");
    accountNumber = keyboard.nextLine();

    System.out.print("Please enter your first and last name");
    name = keyboard.nextLine();

    System.out.print("Please enter your address-street, city, state and zip code");
    address = keyboard.nextLine();

    System.out.print ("Please enter your discount code -" +
    " enter SR for senior citizen," +
    " LI for low income, NA for no discount");

    System.out.print("Is your payment late? Y/N");


    System.out.print("Please enter your KWH-usage of Kilowatt Hours-"
    + " as a whole number");
    kwh = keyboard.nextDouble();
    double price = (kwh *.0624);
    if (SR = double price * .10);
    if (LI = double price * .25);
    if (NA = double price null);
    if (Y = double price * .15 );
    }
    }
    The way you have your if statements are incorrect. What you should have done was declare a variable , lets say:

    Code:
    String discountCode;
    What you would do then, is do an if statement to check what discount code the user entered, and if it is valid. Below is an example.

    Code:
    If(discountCode == "SR")
    { price = price + (price * .10);
    }
    
    else if(discountCode =="LI")
    
    { 
       //calculations here
    }
    
    else
    {
        //display standard price, as discount does not apply
    }
    What this does is checks to see IF the user entered SR, if the user did, it calculates the code in the statement. 'Else if' comes in to play if "SR" was not found. The same applies for 'else if' as the 'if'. If neither are found, the 'else' executes and the standard price applies as no valid discount code was found.

    On another note, you may want to declare a separate variable for late payment AFTER the discount code section. Who is to say someone can't be a senior citizen AND be late on payment. You could use conditional operators in the selection statements to do it all at once , or just do it at the end like I mentioned first.

    If you require anymore help, just ask. Happy to help with anything I can. I know how it feels to be stuck on something.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Thank you so much! I kept reading the book but I was getting more confused!

IMN logo majestic logo threadwatch logo seochat tools logo