# Help with switch statements

• February 12th, 2011, 11:15 PM
suxen
Help with switch statements
Hello all,

I'm in a intro java class, and while the teacher is great, I am still having some issues with understanding how to use the switch method.

The instructions are to:
write a program that calculates the price per month that an ISP charges according to base plan + usage. There are three plans: A,B,C.

Each are targeted to different people, but if you use too many hours, a certain plan will work best for you and the program is supposed to point that out.

So basically,

Plan A: \$9.95/month for first 10 hours, \$2.00 extra for each additional hours
Plan B: \$13.85 first 20 hours, each addt. hour \$1
Plan C: \$19.95 and unlimited hours

I will need to ask the user for the plan type they are using, and then how many hours.

Then, calculate their bill.

Then if there is a plan that works better for them, I will need to use a switch statement to recommend them a better plan (if one exists) and then display the amount of money they will save by switching.

I wrote what I have so far, but I am having problems with switch statements since it doesn't let me use operators such as == != || && and it also doesn't allow me to use double variables.

I would appreciate any help. Thanks for reading.

p.s.

My code is really inefficient, since I need to write each case separately by the hour entered. I would like to use interval groups like "hours > 20" or similar. My program also isn't able to calculate what the best plan would be and to display the cost savings. I know my code is probably an eyesore, it's my first week with programming in general so please understand. Thank you

Code :

```import java.util.Scanner;   public class InternetPackages   { public static void main(String [] args) { String input; //hold a line of input char Package; //hold single character   int hours; //holds hour online double baseA = 9.95; double baseB = 13.95; double baseC = 19.95;       //Scanner to read input from user   Scanner keyboard = new Scanner(System.in);     System.out.println("Please enter your current Package Plan: \n" +"A \n" +"B \n" +"C \n");     { input = keyboard.nextLine(); //get a line of input Package = input.charAt(0); //get the first character //ring choice = input.next();     System.out.println("How many whole hours did you use the internet for this month?\n" + "(Round up to the nearest whole hour.)\n" + "If hours exceed 30, please enter 30.");   hours = keyboard.nextInt();     if (input.equals ("A")&&(hours > 10.0))   { double resultA = ((hours - 10.0) * 2)+(baseA); //first 10 hours free System.out.println("Amount due: " +resultA); }     if (input.equals ("A") && (hours <= 10.0)) System.out.println("Amount due: " +baseA);   else   if (input.equals ("B") && (hours > 20.0)) { double resultB = ((hours - 20.0) * 1)+ (baseB); System.out.println("Amount due: " +resultB); }   if (input.equals ("B") && (hours <= 20.0)) { System.out.println("Amount due: " +baseB); } else   if (input.equals ("C")) System.out.println("Amount due :" +baseC);         switch (hours) {   case (1): System.out.println("You would not save any money by switching to another plan."); break;   case (2): System.out.println("You would not save any money by switching to another plan."); break;   case (3): System.out.println("You would not save any money by switching to another plan."); break;   case (4): System.out.println("You would not save any money by switching to another plan."); break;   case (5): System.out.println("You would not save any money by switching to another plan."); break;   case (6): System.out.println("You would not save any money by switching to another plan."); break;   case (7): System.out.println("You would not save any money by switching to another plan."); break;   case (8): System.out.println("You would not save any money by switching to another plan."); break;   case (9): System.out.println("You would not save any money by switching to another plan."); break;   case (10): System.out.println("You would not save any money by switching to another plan."); break;   case (11): System.out.println("You would not save any money by switching to another plan."); break;   case (12): System.out.println("You would not save any money by switching to another plan."); break;   case (13): System.out.println("The package that would save you the most money is B"); break;   case (14): System.out.println("The package that would save you the most money is B"); break;   case (15): System.out.println("The package that would save you the most money is B"); break;   case (16): System.out.println("The package that would save you the most money is B"); break;   case (17): System.out.println("The package that would save you the most money is B"); break;   case (18): System.out.println("The package that would save you the most money is B"); break;   case (19): System.out.println("The package that would save you the most money is B"); break;   case (20): System.out.println("The package that would save you the most money is B"); break;   case (21): System.out.println("The package that would save you the most money is B"); break;   case (22): System.out.println("The package that would save you the most money is B"); break;   case (23): System.out.println("The package that would save you the most money is B"); break;   case (24): System.out.println("The package that would save you the most money is B"); break;   case (25): System.out.println("The package that would save you the most money is B"); break;   case (26): System.out.println("The package that would save you the most money is B"); break;   case (27): System.out.println("The package that would save you the most money is C"); break;   case (28): System.out.println("The package that would save you the most money is C"); break;   case (29): System.out.println("The package that would save you the most money is C"); break;   case (30): System.out.println("The package that would save you the most money is C"); break;   default: System.out.println("Please enter hours in whole values.");   } } } }```
• February 13th, 2011, 02:45 PM
codesmuggler
Re: Help with switch statements
Aw snap.
Code :

```if(hours => 1 && hours < 13) print no better plan else if(hours => 13 && hours < 27) print B etc```
Switch doesn't make any sense here.
• February 13th, 2011, 08:09 PM
suxen
Re: Help with switch statements
Quote:

Originally Posted by codesmuggler
Aw snap.
Code :

```if(hours => 1 && hours < 13) print no better plan else if(hours => 13 && hours < 27) print B etc```
Switch doesn't make any sense here.

Thanks for the help. That's exactly what I was thinking. However, our instructor told us to modify the statements to use switch statements.

Here is the directions:

Code :

```Step 1: Write a program that calculates a customer’s monthly bill. It should ask the user to enter the letter of the package the customer has purchased (A, B, C) and the number of hours that were used. It should then display the total charges. Step 2: modify the program to use switch statement Step 3: modify the program such that for package A/B customers, the money that they could save if they switch to B or C plans. If no money will be saved, display no message.```
• February 14th, 2011, 04:09 PM
codesmuggler
Re: Help with switch statements
So what's the problem, if you have already written it? :)
• February 15th, 2011, 03:55 AM
suxen
Re: Help with switch statements
I already submitted the assignment. Thanks