mercredi 5 avril 2017

Need Help - Java program to determine savings for different ISP packages

My first assignment was to determine the monthly bill depending on what package you have for your ISP. This is the exact assignment.


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

  • Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
  • Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
  • Package C: For $19.95 per month unlimited access is provided.

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, or C) and the number of hours that were used. It should then display the total charges.


Here is my code for that program. I'm pretty sure it's inefficient but it's okay for a beginner. By the way, I know DrJava is old, but my school forces us to use it.

// Adam King

import java.util.*;
public class Bill{
  public static void main (String [] args){

  Scanner keyboard = new Scanner(System.in);
  System.out.println("Package A: For $9.95 per month 10 hours of access are provided.  Additional hours are $2.00 per hour.");
  System.out.println("Package B: For $13.95 per month 20 hours of access are provided.  Additional hours are $1.00 per hour.");
  System.out.println("Package C: For $19.95 per month unlimited access is provided.");
  System.out.println("Which package did you purchase? (A,B, or C)");
  String pkg = keyboard.nextLine();
  System.out.println("How many hours did you use?");
  double hrs = keyboard.nextDouble();
  if (hrs < 0){hrs = 0;}
  double ahrs = hrs - 10;
  double bhrs = hrs - 20;

  if (pkg.equals("A")){
    double prc = 9.95 + ((ahrs * 2));
       System.out.println("Your bill this month is $"+prc+".");
  }
  if (pkg.equals("B")){
    double prc = (13.95 + (bhrs));
       System.out.println("Your bill this month is $"+prc+".");
  }
   if (pkg.equals("C")){
    double prc = (19.95);
       System.out.println("Your bill this month is $"+prc+".");
  }
   keyboard.close();

 }
}

/*
Welcome to DrJava.  Working directory is F:\java
> run Bill
Package A: For $9.95 per month 10 hours of access are provided.  Additional hours are $2.00 per hour. 
Package B: For $13.95 per month 20 hours of access are provided.  Additional hours are $1.00 per hour. 
Package C: For $19.95 per month unlimited access is provided. 
Which package did you purchase? (A,B, or C) 
 [DrJava Input Box]
How many hours did you use? 
 [DrJava Input Box]
Your bill this month is $13.95. 
> 
*/


Now the real problem is the next assignment, which is based off of the last one. Here's the exact assignment.

Modify the program you wrote for the last program so it also calculates and displays the amount of money you would save by switching to a different package. If you don't save any money, tell the user that there is no savings. Only show the package with the most savings.


This is what I have so far, and I'm sure something isn't right. It compiles, and runs correctly, but I feel like the math is wrong.

// Adam King

import java.util.*;
public class Save{
  public static void main (String [] args){

  Scanner keyboard = new Scanner(System.in);
  System.out.println("Package A: For $9.95 per month 10 hours of access are provided.  Additional hours are $2.00 per hour.");
  System.out.println("Package B: For $13.95 per month 20 hours of access are provided.  Additional hours are $1.00 per hour.");
  System.out.println("Package C: For $19.95 per month unlimited access is provided.");
  System.out.println("Which package do you currently have? (A,B, or C)");
  String pkg = keyboard.nextLine();
  System.out.println("How many hours did you use this month?");
  double hrs = keyboard.nextDouble();
  if (hrs < 0){hrs = 0;}
  double ahrs = hrs - 10;
  double bhrs = hrs - 20;
  double aprc = 9.95 + ((ahrs * 2));
  double bprc = (13.95 + (bhrs));
  double cprc = (19.95);

  if (pkg.equals("A")){
    System.out.println("Your bill this month is $"+aprc+".");
       if (aprc >= cprc){
                    System.out.println("You can save $"+(aprc -cprc)+" by switching to Package C.");
       }
       else if (aprc >= bprc){
                    System.out.println("You can save $"+(aprc -bprc)+" by switching to Package B.");
       }
       else if (aprc < cprc && aprc < bprc){
                    System.out.println("Your current package is the cheapest package.");
       }
  }

  if (pkg.equals("B")){
    System.out.println("Your bill this month is $"+bprc+".");
       if (bprc >= cprc){
                    System.out.println("You can save $"+(bprc -cprc)+" by switching to Package C.");
       }
       else if (bprc >= aprc){
                    System.out.println("You can save $"+(bprc -aprc)+" by switching to Package A.");
       }
       else if (bprc < aprc && bprc < cprc){
                    System.out.println("Your current package is the cheapest package.");
       }
  }

   if (pkg.equals("C")){
    System.out.println("Your bill this month is $"+cprc+".");
      if (cprc > aprc){
                    System.out.println("You can save $"+(cprc -aprc)+" by switching to Package A.");
      }
      else if (cprc > bprc){
                    System.out.println("You can save $"+(cprc -bprc)+" by switching to Package B.");
      }
      else if (cprc < aprc && cprc < bprc){
                    System.out.println("Your current package is the cheapest package.");
      }
   }
   keyboard.close();
  }
}


/*
Welcome to DrJava.  Working directory is F:\java
> run Save
Package A: For $9.95 per month 10 hours of access are provided.  Additional hours are $2.00 per hour. 
Package B: For $13.95 per month 20 hours of access are provided.  Additional hours are $1.00 per hour. 
Package C: For $19.95 per month unlimited access is provided. 
Which package do you currently have? (A,B, or C) 
 [DrJava Input Box]
How many hours did you use this month? 
 [DrJava Input Box]
Your bill this month is $13.95. 
You can save $8.0 by switching to Package B. 
> 
*/


If anyone can check this over and tell me if anything is wrong I'd be ecstatic.

Aucun commentaire:

Enregistrer un commentaire