Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java coding help

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java coding help

    Please help with this question - 14. Internet Service provider, Part 2

    Modify the program you wrote for Programming Challenge 13 so it also calculates and displays the amount of money Package A customers would save if they purchased Package B or C, and the amount of money Package B customers would save if they purchased Package C. If there would be no savings, no message should be printed.

    This is the code I have so far, but I am missing how much we could've saved if we had chosen B or C in all cases.

     import java.util.Scanner;
     
    public class Ch3Challenge14 {
    public static void main( String [] args ) {
     
    // Package A base price stays constant at 9.95.
    final double PACKAGE_A_BASE_PRICE = 9.95;
    // Package B base price stays constant at 13.95.
    final double PACKAGE_B_BASE_PRICE = 13.95;
    // Package C base price stays constant at 19.95.
    final double PACKAGE_C_BASE_PRICE = 19.95;
    // Declare the 10 hour limit for package A.
    final int PACKAGE_A_HOUR_LIMIT = 10;
    // Declare the 20 hour limit for package B.
    final int PACKAGE_B_HOUR_LIMIT = 20;
    // Declare extra fee of $2.00 for every hour over the base price of package !.
    final double PACKAGE_A_EXTRA_FEE = 2;
    // Declare extra fee of $1.00 for every hour over the base price of package B.
    final double PACKAGE_B_EXTRA_FEE = 1;
    // Declare hours entered by user and store as integer.
    int userInputHours;
    // Declare variable for how many hours over the base / initially zero hours exceeded to start.
    int hoursExceeded = 0;
    // Declare new variable to not override values given for package A.
    int hoursExceededCompare = 0;
    // Declare variable for extra charge over the hour limit / initially zero charge added..
    double extraCharge = 0;
    // Declare new variable to not override package A value.
    double extraChargeCompare = 0;
    // Declare variable to store the total bill value / initially total bill =0..
    double totalBill = 0;
    // Declare variable for how much they would've paid with package B.
    double packageBPriceCompare = 0;
    // Declare variable for how much money they would've saved if they had chosen package B.
    double packageBSavings = 0;
    // Declare variable for how much money they would've saved if they had chosen package C.
    double packageCSavings = 0;
     
    Scanner scanner = new Scanner( System.in );
     
    // Create string variable to hold what the user enters.
    String userInputPackage;
     
    // Ask the user to enter which package that they have - A, B or C.
    System.out.println( "\nWhat package do you have - A, B, or C?" );
    // We have what package the user types stored - A, B, or C.
    userInputPackage = scanner.nextLine();
     
    // Ask the user how many hours that they used.
    System.out.println( "How many hours did you use?" );
    // We will have how many hours the user typed stored.
    userInputHours = scanner.nextInt();
     
    // Calculate how much he/she will pay for package A.
    if ( userInputPackage.equalsIgnoreCase( "a" ) ) {
    // How much user has to pay depending on how many hours over base price (9.95).
    if( userInputHours > PACKAGE_A_HOUR_LIMIT ) {
    // Calculate the hours exceeded over the 10 hour limit for package A.
    hoursExceeded = userInputHours - PACKAGE_A_HOUR_LIMIT;
    // Calculate the extra charge for every extra hour exceeded.
    extraCharge = hoursExceeded * PACKAGE_A_EXTRA_FEE;
    }
    // Display the total bill which is equal to the base price plus the calculated extra charge.
    totalBill = PACKAGE_A_BASE_PRICE + extraCharge;
     
    // Calculate how much a package A user would pay for package B.
    if( userInputHours > PACKAGE_B_HOUR_LIMIT ) {
    hoursExceededCompare = userInputHours - PACKAGE_B_HOUR_LIMIT;
    extraChargeCompare = hoursExceededCompare * PACKAGE_B_EXTRA_FEE;
    }
    packageBPriceCompare = PACKAGE_B_BASE_PRICE + extraChargeCompare;
    if( packageBPriceCompare < totalBill ){
    packageBSavings = totalBill - packageBPriceCompare;
    // Display how much money a package A user would have saved if he/she switched to package B.
    System.out.printf( "You will save $%,.2f if you switch to Package B\n", packageBSavings );
    }
     
    // Calculate how much a package A user would pay for package C.
    if( PACKAGE_C_BASE_PRICE < totalBill ) {
    packageCSavings = totalBill - PACKAGE_C_BASE_PRICE;
    // Display how much money a package A user would have saved if he/she had chosen package C.
    System.out.printf( "You will save $%,.2f if you switch to Package C\n", packageCSavings );
    }
     
    } else if( userInputPackage.equalsIgnoreCase("b") ) {
    // How much user has to pay depending on how many hours over base price (13.95).
    if ( userInputHours > PACKAGE_B_HOUR_LIMIT ) {
    // Calculate the hours exceeded over the 20 hour limit for package B.
    hoursExceeded = userInputHours - PACKAGE_B_HOUR_LIMIT;
    // Calculate the extra charge for every extra hour exceeded.
    extraCharge = hoursExceeded * PACKAGE_B_EXTRA_FEE;
    }
    // Display the total bill which is equal to the base price plus the calculated extra charge.
    totalBill = PACKAGE_B_BASE_PRICE + extraCharge;
     
    if( PACKAGE_C_BASE_PRICE < totalBill) {
    // Calculate how much user would save if they had chosen package C.
    packageCSavings = totalBill - PACKAGE_C_BASE_PRICE;
    // Display to the user how much they would have saved if they had chosen package C.
    System.out.printf( "You will save $%,.2f if you switch to Package C\n", packageCSavings );
    }
     
     
    } else if( userInputPackage.equalsIgnoreCase("c") ) {
    // Display the total bill which is equal to the base price without any extra charge.
    totalBill = PACKAGE_C_BASE_PRICE;
    }
    // Store what user enters: userInputPackage in %s, hoursExceeded in %d, extraCharge in %f).
    System.out.printf("You chose Package %s\nHours Exceeded: %d\n Extra charge: %f\nTotal charge: %f",
    userInputPackage, hoursExceeded, extraCharge, totalBill );

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Java coding help

    The posted code has lost all its indentations making it hard to read an understand.
    Logically nested statements should be indented at least 3 spaces.

    Please post the code with proper indentations.


    What problems are you having with the code?
    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java coding help
    By limesh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2014, 09:53 AM
  2. Need help with Java Coding
    By BionicBunny in forum Java Theory & Questions
    Replies: 5
    Last Post: November 30th, 2012, 10:08 AM
  3. java coding
    By deq wan in forum Member Introductions
    Replies: 1
    Last Post: April 14th, 2012, 08:48 AM
  4. Replies: 2
    Last Post: February 19th, 2012, 06:36 AM
  5. Help me in java coding
    By smallmac in forum Java Theory & Questions
    Replies: 5
    Last Post: August 2nd, 2010, 09:50 AM