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
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."); } } } }