Friday, January 29, 2016

Java - Automated Car Parking


In a automated car parking facility we have 10 parking racks
"Rack-01", "Rack-02", "Rack-03", "Rack-04", "Rack-05", "Rack-06", "Rack-07", "Rack-08",

"Rack-09", "Rack-10"

need to maintain a record of which parking rack is free and which is occupied through Java


 package CollectionFramework;  
 import java.util.Arrays;  
 import java.util.Collection;  
 import java.util.HashMap;  
 import java.util.Iterator;  
 import java.util.List;  
 import java.util.Map;  
 import java.util.Map.Entry;  
 import java.util.Set;  
 public class CarParking {  
      static Map<String, String> rackMap;  
      static String rackNumber;  
      static List<String> rackList;  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           CarParking parking = new CarParking();  
           rackSpace();  
           parking.parking(new String[] { "Rack-01", "Rack-10" });  
           parkTheCar();  
           checkRackSpaceStatus();  
      }  
      public static void rackSpace() {  
           // Creating the racks  
           rackList = Arrays.asList("Rack-01", "Rack-02", "Rack-03", "Rack-04", "Rack-05", "Rack-06", "Rack-07", "Rack-08",  
                     "Rack-09", "Rack-10");  
           // adding racks in the map  
           rackMap = new HashMap<>();  
           // by default all parking racks are free  
           for (int i = 0; i < rackList.size(); i++) {  
                rackMap.put(rackList.get(i), "Availale For Parking");  
           }  
      }  
      public static void checkRackSpaceStatus() {  
           System.out.println("");  
 System.out.println("******< PARKING STATUS >******");  
           Set<Entry<String, String>> set = rackMap.entrySet();  
           Iterator<Entry<String, String>> iterator = set.iterator();  
           while (iterator.hasNext()) {  
                Entry<String, String> next = iterator.next();  
                System.out.println(next.getKey() + " :"+"\t" + next.getValue());  
                // System.out.println(iterator.next().getValue());  
           }  
      }  
      public static void parkTheCar() {  
      }  
      public void parking(String[] cars) {  
           for (int i = 0; i < cars.length; i++) {  
                rackNumber = cars[i];  
                // System.out.println(rackMap.get(cars[i]));  
                if (rackMap.get(rackNumber).equals("Availale For Parking")) {  
                     rackMap.remove(rackNumber);  
                     // System.out.println(rackMap.get(rackNumber));  
                      rackMap.putIfAbsent(rackNumber, "Occupied");  
 //                    rackMap.put(rackNumber, "Occupied");  
                     System.out.println("Parked car at " + rackNumber );  
                }  
           }  
      }  
 }  
OUTPUT
 Parked car at Rack-01  
 Parked car at Rack-10  
 ******< PARKING STATUS >******  
 Rack-03 :     Availale For Parking  
 Rack-02 :     Availale For Parking  
 Rack-05 :     Availale For Parking  
 Rack-04 :     Availale For Parking  
 Rack-07 :     Availale For Parking  
 Rack-06 :     Availale For Parking  
 Rack-09 :     Availale For Parking  
 Rack-08 :     Availale For Parking  
 Rack-10 :     Occupied  
 Rack-01 :     Occupied  

No comments:

Post a Comment