daily pastebin goal
43%
SHARE
TWEET

parkinglot

nicks707 Mar 8th, 2018 71 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. Problem : Given two types of cars (S/L) and a parking with two type of parking spots (S/L) write a class with two fuctions parkCar(Car) that will return the locaiton of the parked car and fetechCar(loc) that returns
  2. the car parked in that location;
  3.  
  4. class Car{
  5.   int plateNo;
  6.   boolean isLarge;
  7.  
  8.   public Car(int plateNo, boolean isLarge){
  9.     this.palteNo = plate;
  10.     this.isLarge = isLarge;
  11.   }
  12. }
  13.  
  14. class Spot{
  15.   int loc;
  16.   boolean isLarge;
  17.   Car car;
  18.  
  19.   public Spot(int loc, boolean isLarge){
  20.     this.isLarge = isLarge;
  21.     this.loc = loc;
  22.     this.car = null;
  23.   }
  24.  
  25. }
  26.  
  27. class Parking{
  28.   Stack<Spot> smallSpots;
  29.   Stack<Spot> largeSpots;
  30.   Map<Integer, Spot> taken;
  31.  
  32.   public Parking(){
  33.     smallSpots = new Stack<Spots>();
  34.     largeSpots = new Stack<Spots>();
  35.     taken = new HashMap<Integer, Spot>();
  36.   }
  37.  
  38.   public int parkCar(Car car){
  39.  
  40.     Spot parkingSpot = null;
  41.     int location = -1;
  42.     if(car.isLarge && largeSpots.size()!=0){
  43.         parkingSpot = largeSpots.pop();
  44.  
  45.     }else if(!car.isLarge && (largeSpots.size()!=0 || smallSpots.size()!=0)){  // he wanted to make these if checks smaller or simpler said my code works but its complicated
  46.         parkingSpot = smallSpots.size()!=0?smallSpots.pop():largeSpots.pop();
  47.     }
  48.  
  49.     if(parkingSpot==null){
  50.       Sysout("Parking full, cant park");
  51.       return location;
  52.     }
  53.  
  54.     parkingSpot.car = car;    // ispe ro rha tha banda ki direct access kar loge, So I made a getter setter afterwards
  55.     location = parkingSpot.loc;    // pehle ye line bhi if mai likhi thi maine bad mai jab usne simple karne ko bola toh niche likh di
  56.     taken.put(location, parkingSpot);
  57.     return location;
  58.   }
  59.  
  60.   public Car fetchCar(int n){
  61.  
  62.     if(taken.containsKey(n)){
  63.        Spot parkingSpot = taken.get(n);
  64.        Car fetechedCar = parkingSpot.car;
  65.        parkingSpot.car = null;
  66.        if(parkingSpot.isLarge){
  67.          largeSpots.push(parkingSpot);
  68.        }else{
  69.          smallSpots.push(parkingSpot);
  70.        }
  71.        taken.remove(n);
  72.        return fetechedCar;
  73.      }
  74.      return null;
  75.   }
  76.  
  77.  
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top