Created
September 13, 2016 02:05
-
-
Save rooksoto/9bfa15d997af56b13452a4f90875f969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nyc.c4q.abassawo; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class LameParkingGarage { | |
private Map<Integer, AbstractCar> map; | |
public LameParkingGarage(int numberOfFloors) { | |
this.map = new HashMap<>(numberOfFloors); | |
} | |
public void parkCar(int floor, AbstractCar car) { | |
map.put(floor, car); | |
} | |
public AbstractCar getCar(int floor) { | |
AbstractCar car = map.get(floor); | |
return car; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nyc.c4q.abassawo; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class Main { | |
//I GAVE UP! :( | |
public static void main(String[] args) { | |
// write your code here | |
ParkingGarage garage = new ParkingGarage(4); | |
System.out.println(garage); | |
AbstractCar car = new AbstractCar() { | |
@Override | |
public void airConditioning() { | |
} | |
}; | |
Map<String, AbstractCar> rides = new HashMap<>(); | |
rides.put("Abass' ride", new Jetta()); | |
rides.put("Jonathan's ride", new JettaTDI()); | |
rides.put("Rafael's ride", new A4()); | |
for (String key : rides.keySet()) { | |
System.out.println(key + ": " + rides.get(key)); | |
} | |
garage.parkCar(0, car); | |
garage.toString(); | |
for (int i = 0; i < rides.size(); i++) { | |
garage.parkCar(i, car); | |
} | |
garage.toString(); | |
System.out.println(garage); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nyc.c4q.abassawo; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by Abass | |
* | |
* ParkingGarage class implemented with an internal HashMap that maps floor numbers to a List of | |
* AbstractCar and subclasses of AbstractCar. | |
*/ | |
public class ParkingGarage { | |
private Map<Integer, List<AbstractCar>> internalMap; | |
public ParkingGarage(int numberOfFloors){ | |
makeMap(numberOfFloors); | |
List list = internalMap.get(0); | |
} | |
public void makeMap(int size){ | |
internalMap = new HashMap<>(size); | |
for (int i = 0; i < size ; i++) { | |
internalMap.put(i, new ArrayList<AbstractCar>()); | |
} | |
} | |
@Override | |
public String toString() { | |
/*TODO - Modify the toString method to print every car in your Parking Garage*/ | |
for (int i = 0; i < internalMap.size(); i++) { | |
for (int j = 0; j < internalMap.size(); j++) { | |
System.out.println("There are " + internalMap.get(j) + " cars on floor " i); | |
} | |
} | |
return "Levels : " + internalMap.size() + " & " + internalMap.get(0).size() + " cars "; | |
} | |
public void parkCar(int number, AbstractCar car){ | |
/*TODO - Implement this method by retrieving the List of Cars, | |
and adding the parameterizer car to it.*/ | |
List<AbstractCar> cars = internalMap.get(number); | |
if(cars == null){ | |
cars = new ArrayList<>(); | |
} | |
cars.add(car); | |
internalMap.put(number, cars); | |
} | |
public void removeCar(int number, AbstractCar car){ | |
/*TODO - Implement this method by retrieving the List of Cars, | |
and removing the parameterizer car from it.*/ | |
List<AbstractCar> cars = internalMap.get(number); | |
if(cars != null){ | |
cars.remove(car); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment