# Help! Cant figure it out

• 09-12-2013, 04:08 AM
Anthony5347
Help! Cant figure it out
So I have a homework program almost finished but I cannot figure out for the life of why this condition will not compute. Basically if the hourUsage is over 10 but under 21 it should compute the following, but it doesnt:

else if (hourUsage > 10 && hourUsage < 21)
{
totalCost = packA + (additionalHours * 2.00);
savingsAB = totalCost - packB;

if (savingsAB > 0)
System.out.printf("You would save \$%,.2f if you" +
" purchase Package B.\n", savingsAB);
}

else
{
totalCost = packA + (additionalHours * 2.00);
savingsAC = totalCost - packC;
if (savingsAC > 0)
System.out.printf("You would save \$%,.2f if you" +
" purchase Package C.\n", savingsAC);

}

in case 'a' will not compute.

Heres the homework question:

Question: Internet Service Provider

An Internet service provider has three different subscription packages for its customers:

Package A: \$9.95 per month for 10 hours access. Additional hours are \$2.00 per hour

Package B: \$13.95 per month for 20 hours access. Additional hours are \$1.00 per hour

Package C: \$29.99 per month for unlimited access.

Write a program that calculates a customer's monthly bill. The program should ask the user to enter the letter of the package the customer purchased (A, or B, or C) and the number of hours that we used. It should calculate and display the total charges.

The program should also calculate and display 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 displayed.

And heres my code:
import java.util.Scanner;
public class InternetService
{
public static void main(String[] args)
{
//Variable Declaration
String input;
savingsAB, savingsAC, savingsBC;
char packLetter;
final double packA = 9.95;
final double packB = 13.95;
final double packC = 29.99;
Scanner userInput = new Scanner(System.in);

//Promt user for Input
System.out.print("Enter the letter of the package" +
"you have: ");
input = userInput.nextLine();
packLetter = input.charAt(0);

System.out.print("Enter the amount of hours used: ");
hourUsage = userInput.nextDouble();

//Case Structure
switch (packLetter)
{
case 'a':
case 'A':
if (hourUsage <= 10)
{
System.out.print("Your monthly bill is \$" + packA);
}
else if (hourUsage > 10 && hourUsage < 21)
{
totalCost = packA + (additionalHours * 2.00);
savingsAB = totalCost - packB;

if (savingsAB > 0)
System.out.printf("You would save \$%,.2f if you" +
" purchase Package B.\n", savingsAB);
}

else
{
totalCost = packA + (additionalHours * 2.00);
savingsAC = totalCost - packC;
if (savingsAC > 0)
System.out.printf("You would save \$%,.2f if you" +
" purchase Package C.\n", savingsAC);

}

break;

case 'b':
case 'B':
if (hourUsage <= 20)
{
System.out.print("Your monthly bill is \$" + packB);
}
else if (hourUsage > 20)
{
totalCost = packB + (additionalHours * 1.00);
savingsBC = totalCost - packC;

System.out.printf("Your monthly bill is \$%,.2f\n", totalCost);

if (savingsBC > 0)
System.out.printf("You would save \$%,.2f\n if you" +
" purchase package C.\n", savingsBC);
}
break;

case 'c':
case 'C':
System.out.print("Your monthly bill is \$" + packC);

break;

default:
System.out.println("Invalid choice.");

}

}
}
• 09-12-2013, 09:13 AM
gimbal2
Re: Help! Cant figure it out
Yes it does. So if it doesn't do what you expect it to do, your expectations are wrong. hourUsage is not between 10 and 21.

I am making assumptions here because I don't want to study that blurb of unformatted code. If you want better help, learn how to use this forum properly:

http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum - Learn Java Programming