Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could someone help me finish this program please? I am stuck and just at a roadb

ID: 3770121 • Letter: C

Question

Could someone help me finish this program please? I am stuck and just at a roadblock.

You are implementing a system that manages parking lot using object-oriented principles. -­ The parking lot has multiple levels. Each level has a multiple rows of spots. -­ Motorcycles, cars and buses can park. It has motorcycle spot, compact spot and large spot. -­ A motorcycle can park in any spot. -­ A car can park in either a single compact spot or a single large spot. -­ A bus can park in five large spots that are consecutive and with in the same row. It cannot park in small spot.

public class parkingLot {

    private static final int NUMBER_OF_SMALL_SLOTS = 10;
    private static final int NUMBER_OF_COMPACT_SLOTS = 10;
    private static final int NUMBER_OF_LARGE_SLOTS = 10;
    public Map occupiedSlots;
    private List smallSlots;
    private List compactSlots;
    private List largeSlots;

    public parkingLot() {
        smallSlots = new ArrayList<>(NUMBER_OF_SMALL_SLOTS);
        compactSlots = new ArrayList<>(NUMBER_OF_COMPACT_SLOTS);
        largeSlots = new ArrayList<>(NUMBER_OF_LARGE_SLOTS);
        createSlots();
        occupiedSlots = new HashMap<>();
    }

    private void createSlots() {

        for (int i = 1; i <= NUMBER_OF_SMALL_SLOTS; i++) {
            smallSlots.add(new SmallSlotS(i));
        }
        for (int i = 1; i <= NUMBER_OF_COMPACT_SLOTS; i++) {
            compactSlots.add(new CompactSlotS(i));
        }
        for (int i = 1; i <= NUMBER_OF_LARGE_SLOTS; i++) {
            largeSlots.add(new LargeSlot(i));
        }

    }

    public long park(Vehicle vehicle) {

        Slots slot;
        long uniqueToken = -1;

        if (vehicle instanceof Motorcycle) {
            if ((slot = getFirstEmptySlot(smallSlots)) != null) {
                uniqueToken = parkHelper(slot, vehicle);
            } else if ((slot = getFirstEmptySlot(compactSlots)) != null) {
                uniqueToken = parkHelper(slot, vehicle);
            } else if ((slot = getFirstEmptySlot(largeSlots)) != null) {
                uniqueToken = parkHelper(slot, vehicle);
            }
        } else if (vehicle instanceof Car) {
            if ((slot = getFirstEmptySlot(compactSlots)) != null) {
                uniqueToken = parkHelper(slot, vehicle);
            } else if ((slot = getFirstEmptySlot(largeSlots)) != null) {
                uniqueToken = parkHelper(slot, vehicle);
            }
        } else {
            if ((slot = getFirstEmptySlot(largeSlots)) != null) {
                uniqueToken = parkHelper(slot, vehicle);
            }
        }
        return uniqueToken;
    }

    public void unPark(long uniqueToken) {
        occupiedSlots.get(uniqueToken).unPark();
        occupiedSlots.remove(uniqueToken);
    }

    private Slots getFirstEmptySlot(List slots) {
        Iterator slotIterator = slots.iterator();
        boolean isSlotFound = false;
        Slots emptySlot = null;

        while (slotIterator.hasNext() && !isSlotFound) {
            emptySlot = slotIterator.next();
            if (!emptySlot.isOccupied()) {
                isSlotFound = true;
            }
        }
        return emptySlot;
    }

    private long parkHelper(Slots slot, Vehicle vehicle) {
        slot.park();
        long uniqueToken = vehicle.hashCode() * 43;
        occupiedSlots.put(uniqueToken, slot);
        return uniqueToken;
    }
}

public abstract class Vehicle {
  protected ArrayList parkingSpots = new ArrayList();
  protected String licensePlate;
  protected int spotsNeeded;
  protected VehicleSize size;


  public int getSpotsNeeded() {
   return spotsNeeded;
  }


  public VehicleSize getSize() {
   return size;
  }


  // park vehicle in this spot(among others, potentially)
  public void parkInSpot(ParkingSpot s) {
   parkingSpots.add(s);
  }


  // remove car from spot, and notify spot that it is gone
  public void clearSpots() {
  }


  // checks if the spot is big enough for the vechile(and is available)
  // this compares the SIZE only. it does not check if
  // has enough spots
  public abstract boolean canFitInSpot(ParkingSpot spot);

    int hashCodeA() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


}



enum VehicleSize {
  Motorcycle, Compact, Large
}
public abstract class Motorcycle extends Vehicle {
  public Motorcycle() {
   spotsNeeded = 1;
   size = VehicleSize.Motorcycle;
}


  public boolean canFitSpot(ParkingSpot spot) {
   return false;
  }
}
class Car extends Vehicle {
  public Car() {
   spotsNeeded = 1;
   size = VehicleSize.Compact;
  }


  // checks if the spot is a Compact or a large
         @Override
  public boolean canFitInSpot(ParkingSpot spt) {
   return false;
  }


}

public class Bus extends Vehicle {
    public Bus() {
   spotsNeeded = 5;
  size = VehicleSize.Large;
    }

    // checks if the spot is a large. does not check num of spots
    public boolean canFitInSpot(ParkingSpot spot) {
  return false;
    }
}

abstract class Slots {
    
    private boolean isOccupied;   
    private int slotNumber;
    
    
    Slots(int slotNumber) {
        isOccupied = false;
        this.slotNumber = slotNumber;   
    }
    
    boolean isOccupied() {
        return isOccupied;
    }
    
    int getSlotNumber() {
        return slotNumber;
    }
    
    void park() {
        isOccupied = true;
    }
    
    void unPark() {
       isOccupied = false;
    }
    
    @Override
    public boolean equals(Object o) {
        return (((Slots) o).slotNumber == this.slotNumber);
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 53 * hash + this.slotNumber;
        return hash;
    }
  
}
class SmallSlotS extends Slots {
     SmallSlotS(int slotNo){
         super(slotNo);
     }
}
class CompactSlotS extends Slots {
     CompactSlotS(int slotNo) {
         super(slotNo);
     }
}

class LargeSlot extends Slots {
     LargeSlot(int slotNo) {
         super(slotNo);
     }
}

Explanation / Answer


public class Vehicle {
   private final int size;
   private final int lisense;
   private boolean status;
   private Lot lot;

   public Vehicle(int size) {
       this.size = size;
       lisense = this.hashCode();
       lot = Lot.getInstance();
   }

   public void setStatus(boolean status) {
       this.status = status;
   }

   private Slot findSlot() {

       Slot slot;
       switch (this.size) {
       case 1:
           slot = lot.getSmallSlots().remove(0);
       case 2:
           slot = lot.getCompactSlots().remove(0);
       case 3:
           slot = lot.getLargeSlots().remove(0);
       default:
           slot = null;
       }
       return slot;
   }

   public void park() {
       Slot slot = findSlot();
       if (slot != null) {
           lot.occupiedSlots.put(this.lisense, slot);
           slot.occupy(this);
       }
   }

   public void leave() {
       Slot slot = lot.occupiedSlots.remove(this.lisense);
       slot.release();
       switch (this.size) {
       case 1:
           lot.getSmallSlots().add(slot);
       case 2:
           lot.getCompactSlots().add(slot);
       case 3:
           lot.getLargeSlots().add(slot);
       }
   }
}

public class Car extends Vehicle
{
   public Car(){
       super(1);      
   }
}
public class Truck extends Vehicle{
   public Truck(){
       super(2);      
   }
}


public class Lot {
   private static Lot lot = null;

   private static final int NUMBER_OF_SMALL_SLOTS = 10;
   private static final int NUMBER_OF_COMPACT_SLOTS = 10;
   private static final int NUMBER_OF_LARGE_SLOTS = 10;

   public Map<Integer, Slot> occupiedSlots;
   private List<Slot> smallSlots;
   private List<Slot> compactSlots;
   private List<Slot> largeSlots;

   private Lot() {
       smallSlots = new LinkedList<>();
       compactSlots = new LinkedList<>();
       largeSlots = new LinkedList<>();
       occupiedSlots = new HashMap<>();
       for (int i = 1; i <= NUMBER_OF_SMALL_SLOTS; i++)
           smallSlots.add(new Slot(i, 1));

       for (int i = 1; i <= NUMBER_OF_COMPACT_SLOTS; i++)
           compactSlots.add(new Slot(i, 2));

       for (int i = 1; i <= NUMBER_OF_LARGE_SLOTS; i++)
           largeSlots.add(new Slot(i, 3));

   }

   public List<Slot> getSmallSlots() {
       return smallSlots;
   }

   public List<Slot> getCompactSlots() {
       return compactSlots;
   }

   public List<Slot> getLargeSlots() {
       return largeSlots;
   }

   public static Lot getInstance() {
       if (lot == null)
           lot = new Lot();
       return lot;
   }
}

    Slot


public class Slot {
   private final int id;
   private final int size;
   private boolean available;
   private Vehicle vehicle;

   public Slot(int id, int size) {
       this.id = id;
       this.size = size;
       this.available = true;
   }

   public void occupy(Vehicle v) {
       this.vehicle = v;
       this.available = false;
   }

   public void release() {
       this.vehicle = null;
       this.available = true;
   }
}