-3

The variables charge and deduction1 cannot be resolved as variables. I have declared the type and assigned values to them however they cant be resolved.

Any help is appreciated.

Comments are written next to the unresolved variables

This program calculates a parking charge for an ` individual while taking into account pensioners, time and disablities

class parking 
{
    public static void main (String [] a)
    {
        all(); 
        System.exit(0);
    }
    public static double local_l(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Do you live locally ? (Yes or No)");
               
        String local;
        double deduction1;
        local = scanner.nextLine();
        if (local.equals("Yes")){
            deduction1 = 1.0;
            return deduction1;
        }
               
        if (local.equals("No")){
             deduction1 = 0;
             return deduction1;
        } else{
             System.out.println("Invalid answer, please answer : Yes or No (exactly as written)");
                  
        }
        return 0.0;
   }
               
   public static void calculation(){
       Scanner scanner = new Scanner(System.in);
       int hours;
       double charge;
       String disabled;
       disabled = scanner.nextLine();
       if (disabled.equals("Yes")){
           System.out.println("Parking is free");   
       }
                    
       if (disabled.equals("No")){
           hours = scanner.nextInt();
           if (hours == 1){
               charge = 1.00;
           } 
           if (hours >=2 && hours <= 4){
               charge = 4.00;
           }
       
           if (hours >=5 && hours <=6){
               charge = 4.50;
           }
       
           if (hours >= 7 && hours <=8){
               charge = 5.50;
           } 
       }
                   
   }
       
   public static double old_age_pensioner (double charge, double deduction1,double deduction2){
       Scanner scanner = new Scanner(System.in);
       String oap;
       System.out.println("Are you an old age pensioner? ");
       oap = scanner.nextLine();
       if(oap.equals("Yes")){
           deduction2 = 2.0;
           return deduction2;
       }
       if (oap.equals("No")){
           deduction2 = 0; 
           return deduction2;
       } else {
           System.out.println("Invalid answer, please answer : Yes or No (exactly as written");
                  
       }
        
       double result;
       result = charge - deduction1 - deduction2; **charge cannot be resolved to a 
                                                        variable as well as deduction 1**
       System.out.println("The parking charge is" + result);
       return 0;
   }
       
   public static void all(){
       calculation();
       local_l();
       old_age_pensioner( charge, deduction1);
   }  
}
gkatiforis
  • 1,588
  • 9
  • 19
  • 1
    You need to read about the scope of variables in Java, the charge variable is only visible in the calculation function. There's no way you can access it outside the function. If you want to access it outside make it a class variable – b.GHILAS Oct 29 '21 at 15:53
  • See: [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) – OH GOD SPIDERS Oct 29 '21 at 15:56
  • Is there no other way other than using global variables ? – yeppidyyep35 Oct 29 '21 at 16:23
  • @patres2002 of course there's, for example you can make calculation function returns the charge and in your all function use: double charge = calculation() – b.GHILAS Oct 29 '21 at 16:30
  • what about deduction1? – yeppidyyep35 Oct 29 '21 at 16:36
  • In the all() method you invoke old_age_pensioner() with only two arguments. This won't compile as that method takes three arguments. – vsfDawg Oct 29 '21 at 19:06

1 Answers1

0

One way to do this is like follows (I tried to not change a lot of code, and you should add print statement for your user to know what to enter):

public static void main (String [] a) {
    double charge = calculation();
    double deduction1 = local_l();
    double result = old_age_pensioner(charge, deduction1);
    System.out.println("The result: " + result);
}

public static double local_l(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Do you live locally ? (Yes or No)");

    String local;
    double deduction1 = 0.0;
    local = scanner.nextLine();
    if (local.equals("Yes")){
        deduction1 = 1.0;
    } else if (local.equals("No")){
        deduction1 = 0;
    } else {
        System.out.println("Invalid answer, please answer : Yes or No (exactly as written)");
    }

    return deduction1;
}

public static double calculation(){
    Scanner scanner = new Scanner(System.in);
    int hours;
    double charge = 0.0;
    String disabled;
    disabled = scanner.nextLine();
    if (disabled.equals("Yes")){
        System.out.println("Parking is free");
    } else if (disabled.equals("No")){
        hours = scanner.nextInt();
        if (hours == 1){
            charge = 1.00;
        } else if (hours >=2 && hours <= 4){
            charge = 4.00;
        } else if (hours >=5 && hours <=6){
            charge = 4.50;
        } else if (hours >= 7 && hours <=8){
            charge = 5.50;
        }
    }

    return charge;
}

public static double old_age_pensioner (double charge, double deduction1){
    Scanner scanner = new Scanner(System.in);
    String oap;
    double deduction2 = 0.0;
    System.out.println("Are you an old age pensioner? ");
    oap = scanner.nextLine();
    if(oap.equals("Yes")){
        deduction2 = 2.0;
    } else if (oap.equals("No")){
        deduction2 = 0;
    } else {
        System.out.println("Invalid answer, please answer : Yes or No (exactly as written");
    }

    double result = charge - deduction1 - deduction2;
    return result;
}
b.GHILAS
  • 2,273
  • 1
  • 8
  • 16