1

While I am design the parking system, I am struggling with increment and decrement value. For example, If I type rT, the large number will decrement once. After I choose that again, it's not decrementing anymore.

Can someone body explain it to me, thanks?

Here is my code

import java.util.Scanner;

public class challenge3 {

    private static int small = 9;
    private static int  medium = 24;
    private static int mediumHandi = 5;
    private static int  large = 10;


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        handiCar hc = new handiCar();
        rTruck rT = new rTruck();
        rCar rC = new rCar();
        handiTruck hT = new handiTruck();
        bike b = new bike();

        int p = 48;

        String parkagain = "";
        do {

            System.out.println("Type your vehicle type: " +
                    "handiCar hc " +
                    "rTruck rT " +
                    "rCar rC" +
                    "handiTruck hT " +
                    "bike b ");
            String s1 = scanner.nextLine();

           if (s1.equalsIgnoreCase("hc")) {
               mediumHandi--;
               System.out.println("Thank you.");
               if (mediumHandi == 0) {
                   System.out.println("Sorry the parking space has run out");

               }

           }
           else if (s1.equalsIgnoreCase("rT")) {
               large--;
               System.out.println("Thank you.");
               if (large == 0) {
                   System.out.println("Sorry the parking space has run out");

               }

           } else if (s1.equalsIgnoreCase("rC")) {
               medium--;
               System.out.println("Thank you.");
               if (medium == 0) {
                   System.out.println("Sorry the parking space has run out");

               }

           } else if (s1.equalsIgnoreCase("hT")) {
               large--;
               System.out.println("Thank you.");
               if (large == 0) {
                   System.out.println("Sorry the parking space has run out");

               }

           } else if (s1.equalsIgnoreCase("b")) {
               small--;
               System.out.println("Thank you.");
               if (small == 0) {
                   System.out.println("Sorry the parking space has run out");

               }

           }


            System.out.println("Number of small parking " + small);
            System.out.println();
            System.out.println("Number of large parking " + large);
            System.out.println();
            System.out.println("Number of medium parking "+medium);
            System.out.println();
            System.out.println("Number of handi meduim " + mediumHandi);
            System.out.println();

            System.out.println("Type your vehicle type: " +
                    "handiCar hc " +
                    "rTruck rT " +
                    "rCar rC" +
                    "handiTruck hT " +
                    "bike b or no ");
            parkagain = scanner.next();

       }while (
               (parkagain.equalsIgnoreCase("rT") ||
                parkagain.equalsIgnoreCase("hc")||
                parkagain.equalsIgnoreCase("rC")||
                parkagain.equalsIgnoreCase("hT")||
                parkagain.equalsIgnoreCase("b") ||
                       p <= 48 )

        );

        System.out.println("Thank you!");
        scanner.close();
    }

    public static class handiTruck {

    }

    public static class rTruck {


    }

    public static class rCar {


    }

    public static class handiCar {
    }

    public static class bike {
    }

}
Sunil Dabburi
  • 1,394
  • 10
  • 15

1 Answers1

2

You are displaying the same prompt message at the end of the loop and have scanner.next() which will not consider new line. Remove that and assign s1 to parkagain. it should work fine

Replace

System.out.println("Type your vehicle type: " +
                    "handiCar hc " +
                    "rTruck rT " +
                    "rCar rC" +
                    "handiTruck hT " +
                    "bike b or no ");
parkagain = scanner.next();

at the end of the loop with this

parkagain = s1
Sunil Dabburi
  • 1,394
  • 10
  • 15