Mel
Mel

Reputation: 21

Simple Calculation in Java using Methods

I cannot find out where my mathematical error is. I just cannot see why I continue to receive no values back for my determineCableSrvs method.

private static Scanner input = new Scanner(System.in); //STORES CUSTOMER NAME STRING VALUE

public static void main(String[] args)
{//BEGIN main()

//Variables

int subscription = 0; //STORES SUBSCRIPTION INTEGER VALUE
int moviesOnDemand  = 0; //STORES MOVIES ON DEMAND INTEGER VALUE
double cableSrvs = 0; //STORES TOTAL CABLE SERVICE DECIMAL VALUE
double totalMovieCharges = 0; //STORES TOTAL MOVIE CHARGES DECIMAL VALUE
double total = 0; //STORES TOTAL DECIMAL VALUE
char confirm = 'Y'; //LOOP CONTROL VARIABLE SET TO Y
Calendar dateTime = Calendar.getInstance(); //STORES THE DATE VALUE
String customerName = "";


while(Character.toUpperCase(confirm) == 'Y') //START OF WHILE LOOP AUTOMATICALLY ENTERS
{
 String customer = setCustNm(customerName);


 double cable = setCablePkg(subscription);

 double type = determineCableSrv(subscription, cableSrvs);

 int movies = setMoviePurchase(moviesOnDemand);

 totalMovieCharges = movies * 7; //CALCULATING THE TOTAL MOVIE ON DEMAND CHARGE       
 total = totalMovieCharges + type; //CALCULATING THE OVERALL TOTAL  




  System.out.printf("%n%s %tD"
                      + "\nCustomer:  %S"
                      + "%n%nCable Service:  $%,21.2f"
                      + "%nMovies-On-Demand-HD:  %,20.2f"
                      + "\n\nTOTAL DUE: $%,21.2f\n", 
                    "SA CABLE CHARGES AS OF", dateTime, customer, type,
                    totalMovieCharges, total);

  input.nextLine();


  askNewSubscription(confirm);

  printThankYou(confirm);

}
} 

public static String setCustNm(String customerName)

 {

// 1st prompt
System.out.printf("%n%n%nWELCOME TO SA CABLE %n");  
System.out.printf("%nPlease enter your name:  "); //PROMPTING USER TO ENTER NAME
customerName = input.nextLine(); //CAPTURES USERS NAME

return customerName;
}          

public static int setCablePkg(int subscription)
{
 do{
// 2nd Prompt DISPLAYING THE DIFFERENT PACKAGES AVAILABLE FOR SUBSCRIPTION             
System.out.printf("%nSA CABLE - SUBSCRIPTION PACKAGES " +
                  "%n%n1. Basic:  Local & major TV network channels                           %s"      + 
                  "%n2. Deluxe:  Local, major TV, cable & 100 other channels                 %s" +
                  "%n3. Premium:  Deluxe package plus HEB, on-demand & 300 other channels   %s",
                  "$35.00", "75.00", "110.00") ;

  System.out.printf("%n%nSelect your cable subscription package:   "); 
  subscription = input.nextInt();//CAPTURING USER INPUT

   }while (subscription < 1 || subscription > 3);

   return subscription;   
   }

public static double determineCableSrv(int subscription, double cableSrvs)
{

 if(subscription == 1) // IF STATEMENT TO IDENTIFY THE PRICE OF THE TOTAL CABLE SERVICE            
{
  cableSrvs = 35; 
}
else if(subscription == 2)
{
  cableSrvs = 75;
}
else if(subscription == 3)
{
  cableSrvs = 110;  
}   
return cableSrvs;

 }     



public static int setMoviePurchase(int moviesOnDemand)
{
System.out.printf("%nSA CABLE - MOVIES " +
                  "%n%nEnter the number of Movies-On-Demand-HD purchases:  ");
moviesOnDemand = input.nextInt();
return moviesOnDemand;
 }


public static char askNewSubscription(char confirm)
{
System.out.printf("\nEnter 'Y' to continue with a new subscription or 'N' to exit:  ");
confirm = input.nextLine().charAt(0);

return confirm;

}


public static char printThankYou(char confirm)
{ 

 if(Character.toUpperCase(confirm) == 'Y')
 {
   confirm = 'Y';
 }
 if (Character.toUpperCase(confirm) != 'Y')
 {
   confirm = 'N';
   System.out.printf("Thank you for being a valued SA Cable customer!");

    System.exit(0);
  }

 return confirm;
 }

I'm having trouble with this part:

 public static double determineCableSrv(int subscription, double cableSrvs)
 {

 if(subscription == 1) // IF STATEMENT TO IDENTIFY THE PRICE OF THE TOTAL CABLE SERVICE            
{
  cableSrvs = 35; 
}
else if(subscription == 2)
{
  cableSrvs = 75;
}
else if(subscription == 3)
{
  cableSrvs = 110;  
}   
return cableSrvs;

 }     

Here is how I'm calling the method:

double type = determineCableSrv(subscription, cableSrvs);

I cannot seem to get the return value. I need the value for:

total = totalMovieCharges + type; //CALCULATING THE OVERALL TOTAL  

and this print out statement:

System.out.printf("%n%s %tD"
                      + "\nCustomer:  %S"
                      + "%n%nCable Service:  $%,21.2f"
                      + "%nMovies-On-Demand-HD:  %,20.2f"
                      + "\n\nTOTAL DUE: $%,21.2f\n", 
                    "SA CABLE CHARGES AS OF", dateTime, customer, type,
                    totalMovieCharges, total);

Upvotes: 1

Views: 233

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 406125

You set subscription equal to 0 and never change it before passing it in to the determineCableSrv method. You do the same with cableSrvs, so the return value from your method is going to be 0. The cableSrvs variable isn't read inside the method, so it probably shouldn't be in input parameter to begin with.

Upvotes: 2

Related Questions