Complete the code to define the ParkingStrategy interface.
public interface ParkingStrategy {
boolean [1](Vehicle vehicle, ParkingLot lot);
}The method canPark checks if a vehicle can be parked in the lot according to the strategy.
Complete the code to implement a strategy that parks only motorcycles.
public class MotorcycleParkingStrategy implements ParkingStrategy { @Override public boolean [1](Vehicle vehicle, ParkingLot lot) { return vehicle.getType() == VehicleType.MOTORCYCLE; } }
The canPark method is overridden to allow parking only if the vehicle is a motorcycle.
Fix the error in the ParkingLot class method to use the strategy correctly.
public class ParkingLot { private ParkingStrategy strategy; public ParkingLot(ParkingStrategy strategy) { this.strategy = strategy; } public boolean [1](Vehicle vehicle) { if(strategy.canPark(vehicle, this)) { // park vehicle logic return true; } return false; } }
The method parkVehicle is the correct name for parking a vehicle in the lot using the strategy.
Fill both blanks to complete the code for setting and using a parking strategy.
public class ParkingLot { private ParkingStrategy [1]; public void setStrategy(ParkingStrategy [2]) { this.strategy = [2]; } }
The field is named strategy and the method parameter is newStrategy to clearly distinguish them.
Fill all three blanks to complete the code for applying different parking strategies dynamically.
public class ParkingManager { private ParkingLot lot; public ParkingManager(ParkingLot lot) { this.lot = lot; } public void applyStrategy(ParkingStrategy [1]) { lot.[2]([3]); } }
The parameter is named strategy, the method called on lot is setStrategy, and the argument passed is strategy.
