Results 1 to 6 of 6
  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default how do I make this code compare the result?

    Java Code:
    import java.util.Scanner; //import scanner statement
    
    public class InternetServiceProvider
    {
    	public static void main(String [] args)
    	{
    		Scanner keyboard = new Scanner (System.in); //scanner object
    		
    		String service; //service package choice 
    		double hours; //hours used
    		double total; //total amount due
    		
    		
    		System.out.println("Package A: For $9.95 per month 10 hours of access are provided." +
    			" Additional hours\nare $2.00 per hour.\n"); //print service package A rate and price
    		System.out.println("Package B: For $13.95 per month 20 hours of access are provided." +
    			" Additional hours are\n$1.00 per hour.\n"); //print service package B rate and price
    		System.out.println("Package C: For $19.95 per month unlimited access is provided.\n"); //print service package C rate and price
    		System.out.print("Please enter a package you want(A, B, C): "); //prompt service package choice
    		service = keyboard.nextLine(); //accept service package input
    		System.out.print("Please enter hours used: "); //prompt hours used
    		hours = keyboard.nextDouble(); //accept hours used input
          
          
    		
    		if (service.equals("A")) //if statement for service A package
    		{
    			if(hours <= 10)
    			{
    				total = 9.95; //total
                System.out.println("You are using " + service + " and you used " + hours +
    				" hours this month. Your total charge will be $" + total + "."); //print the result for 10 hours or less used
    			}
    			else
    			{
    				total = 9.95 + ((hours-10)*2); //calculate total
                System.out.println("You are using " + service + " and you used " + hours +
    				" hours this month. Your total charge will be $" + total + "."); //pritn the result for 10+ hours used
    			}
    		}
          
          
    		else if(service.equals("B")) //if statement for service B package
          {
             if(hours <= 20)
             {
                total = 13.95; //total
                System.out.println("You are using " + service + " and you used " + hours +
    				" hours this month. Your total charge will be $" + total + "."); //print the result for 20 hours or less used
             }
             else
             {
                total = 13.95 + ((hours-20)*1); //calculate total
                System.out.println("You are using " + service + " and you used " + hours +
    				" hours this month. Your total charge will be $" + total + "."); //print the result for 20+ hours used
             }
           }
           
           
          else if(service.equals("C")) //if statement for service C package
          {
             total = 19.95; //total
             System.out.println("You are using " + service + " and you used " + hours +
    				" hours this month. Your total charge will be $" + total + "."); //print the result
          }
        
    	}
    }

    ok. This is my initial code that suppose to prompt user to choose one of 3 internet service plans which are plan A, B, C and then how many hours user used it. It then prints out the total cost.
    Now, I am having problem. How do you alter this code to compare result from A to B and A to C. for example, user would get $29.95 if he choose plan A and use for 20 hours. But if he choose plan B and use for 20 hours, he would get $13.95. So how do I make this to compare those two prices and print out the difference? I was thinking about compareTo method but I only know how to do it with basic strings like comparing names. Can anyone help me out on this?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: how do I make this code compare the result?

    Calculate each plan, so you would have a totalPlanA, totalPlanB, totalPlanC. Then you can do your calculations.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: how do I make this code compare the result?

    I think you would have to override class Object's compareTo()... but I'm not sure if I get your quest

    When you say the difference between two prices do you mean
    price1 - price2 = difference
    or do you mean to print out the different prices(price1 & price2) side to side

    i assume something like
    Java Code:
    if
        (price1<price2) System.out.println("price1 is cheaper than price2 with the difference of " + price2-price1);
    else 
        if(price1>price2)
         System.out.println(price2 is cheaper than price1 with the difference of "+ price1-price2);
    
    else
          System.out.println("Both plans are the same");
    But I must say this isn't the best option to go about it... let's wait for other advices... I like your code it's easy to follow

  4. #4
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: how do I make this code compare the result?

    This is instruction on the problem. Modify the program you wrote for internet service charge provider 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 saviings, no message should be printed.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: how do I make this code compare the result?

    Quote Originally Posted by Kagiso View Post
    I think you would have to override class Object's compareTo().
    Object doesn't have a compareTo method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: how do I make this code compare the result?

    Quote Originally Posted by jim829 View Post
    Object doesn't have a compareTo method.

    Regards,
    Jim
    My Bad My Bad.... we learn to forget... I just zoomed at the api docs THANX...
    Sometimes I wonder if I'll ever know the entire library... it seems like while I learn some I forget some

    THANX AGAIN

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2012, 01:59 PM
  2. Replies: 3
    Last Post: 11-30-2010, 10:51 PM
  3. Replies: 3
    Last Post: 04-14-2009, 04:44 PM
  4. need a code to get the result of avgscan.exe file
    By Michael in forum New To Java
    Replies: 0
    Last Post: 06-11-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •