javaif-statementmethods

How Do I Add Charges Based On Yes or No?


Working on a coding assignment and we have to incorporate methods and returning it. However, that is the beside the point. I am struggling on the price calculation portion. Okay, here's the gist of what I am stuck with. When the program asks, is your car and import. If the answer is yes, you will be charge with a 7% import tax, if no, the charge is negated. Now, the program asks for four services, depending on yes or no, the charge will be added based on their answer.

So, if the user wants an Oil Change and Tune Up and if their car is an import the services price will be added along with the import tax or if the car is not an import but want all services then the charges will be displayed without the import tax added, etc... The final outcome would display "Before taxes, it would be $# and after taxes, your total is..." An if statement is required but I do not know where to start because I am mainly struggling with the yes or no... Any help? My professor advised me to use an accumulator so I added one.. No avail, I am lost, any help would be greatly appreciated.

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

  Scanner keyboard = new Scanner(System.in);
  String car; 

  //capture car
  System.out.println("What is the make of your car"); 
  car = keyboard.nextLine();

  String answer;
  boolean yn;

  System.out.println("Is your car an import?");
  while (true)
  {
     answer = keyboard.nextLine();
     if (answer.equalsIgnoreCase("yes")) 
   {
     yn = true;
     break;
   } 
        else if (answer.equalsIgnoreCase("no"))
        {
           yn = false;
           break;
         } 
           else 
           {
           System.out.println("Sorry, I didn't catch that. Please answer yes or no");
           }

   } 

      String[] services = {"Oil Change", "Coolant Flush", "Brake Job", "Tune Up"};                      
      for(int i=0; i<services.length; i++)
         {
           System.out.println("Do you want a " +services[i]);
           answer = keyboard.next();    
          }  

     double amount = 0;
     double[] price = {39.99, 59.99, 119.99, 109.99};  
     for(int i =0; i<price.length; i++)
           {
              amount = (price[i] + price [i]);
           }

  // double total = 0;
     double c = 0;
     c = car(amount);
//   c = car(total);
     System.out.println("The price for your services for your" + " " + car + " " + "is" + " "+ c + ".");


 }

 public static double car(double amount)
 {
 return (double) ((amount-32)* 5/9);
 }      

}

Solution

  • Before I talk about your code, I recommend you read this article.

    It is a bit flippant, but there is a deep truth to it. And it maps neatly with a debugging technique that I was taught .... umm ... 40 years ago.


    First I draw your attention to this:

      System.out.println("Is your car an import?");
      while (true)
      {
         answer = keyboard.nextLine();
         if (answer.equalsIgnoreCase("yes")) 
       {
         yn = true;
         break;
       } 
            else if (answer.equalsIgnoreCase("no"))
            {
               yn = false;
               break;
             } 
               else 
               {
               System.out.println("Sorry, I didn't catch that. Please answer yes or no");
               }
    
       } 
    

    That code does a pretty good job of asking a "yes or no" question and getting the answer. It includes code to retry if the user responds with something that isn't recognizable as "yes" or "no".

    So I'm guessing that someone wrote that code for you ... or you found it somewhere. You need to read that code carefully, and make sure you understand exactly how it works.


    Next, I draw your attention to this:

      for(int i=0; i<services.length; i++)
         {
           System.out.println("Do you want a " +services[i]);
           answer = keyboard.next();    
          }  
    

    This code does not make sense. (Explain it to your rubber duck!)

    Your earlier code is a good example of how to ask a yes / no question. But this is nothing like that:

    • You are not testing for "yes" or "no".
    • You are not repeating if the user gives an unrecognizable answer
    • You are not even storing the answer .... for each distinct service.

    Then this:

     double amount = 0;
     double[] price = {39.99, 59.99, 119.99, 109.99};  
     for(int i =0; i<price.length; i++)
           {
              amount = (price[i] + price [i]);
           }
    

    It looks like this is trying to add the price of service items to the overall amount. But:

    • You are doing it for every service item, not just the items that the customer asked for.
    • You are actually doing the calculation incorrectly anyway. (Talk to your Rubber Duck about it!)

    How to fix this?

    • Some things should be obvious from what I said above.

    • The problem of remembering the answers from the first for loop and using them in the second for loop ... is actually a problem you can / should avoid. Instead, combine the two loops into one. Here's some pseudo code:

          for each service:
              ask the user if he wants this service
              if the user wants this service:
                  add the service cost to the total.
      

      Understand the logic of that, and translate it into Java code. (Yes I could write it for you, but that defeats the purpose!)