I have 2 classes: truck and sedan. When some action occurs, I want to add a sedan to the hashMap in Truck class. The map tells what Sedans are currently in the Truck's cargo. I don't want Truck and Sedan to know about each other so I have made a method in CarManager that Sedan can call which passes the id of the sedan and the id of the Truck that Sedan wants to be added to. Then CarManager will inform Truck that a Sedan wants to be added to the list. The issue is I don't know how CarManager will inform the Truck and what I should have in that addSedan method. I do have a HashMap in CarManager that has a collection of CarEntities. The Truck's addCar method cannot be accessed by CarManager since it isnt in the interface and I dont want to add it in the interface because not all CarEntity will use it. Can anyone help?
public interface CarEntity {
String getId();
double getSpeed();
void move();
}
public class CarManager {
private HashMap<String, CarEntity> hash = new HashMap<String, CarEntity>();
public void addSedan(String carId, String truckId) {
???
hash.get(truckId).addCarr(carId); //I don't think this will work
}
}
public class Truck implements CarEntity {
private HashMap<String, CarEntity> cargo = new HashMap<String, CarEntity>();
public void addCar(String id, CarEntity ce) {
cargo.put(id,ce);
}
public class Sedan implements CarEntity {
CarManager.addSedan("Car 1", "Truck 5");
}