Im making an app where the Observers(Clients) should get notified by the Observable(Orders). My app works fine till they need to be notified, and I don't understand why.
My main calls the following methods:
service.addOrder(John, orderForJohn);
service.processOrder();
service.processOder() does the following:
public void processOrder(){
System.out.println("Restaurant Service: Processing " + orders.size() + " order(s)");
for(Order order : orders) {
order.notifyObservers(foodFactory.createFood(order));
}
}
notifyObservers(Food food) does the following:
public void notifyObservers(Food food) {
System.out.println("Notifying Observers of: " + food.toString());
for(Observer observer : observers){
observer.update(food);
}
}
Update does the following:
public void update(Food food) {
System.out.println("Updating: " + food.toString());
}
However the last method update(Food food) does nothing.
I need to use Generics parameters in this exercise. I think the problem will be with one of the following classes.
Client class:
public class Client implements Observer<Food> {
private String name;
private double happiness;
public Client(String name, double happiness) {
this.name = name;
this.happiness = happiness;
}
@Override
public void update(Food food) {
System.out.println("Updating: " + food.toString());
}
public void consume(Food food){
}
public double getHappiness() {
return happiness;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Client \[" +
"name=" + name +
", happiness=" + happiness +
'\]';
}
}
Observer interface:
public interface Observer<Food> {
public void update(Food food);
}
Order Class:
public class Order extends Observable<Food> {
private FoodName food;
private List<FoodName> extras;
private List<Observer<Client>> observers = new ArrayList<>();
public Order(FoodName food, List<FoodName> extras) {
this.food = food;
this.extras = extras;
}
public FoodName getFood() {
return food;
}
public List<FoodName> getExtras() {
return extras;
}
public void notifyObservers(Food food) {
System.out.println("Notifying Observers of: " + food.toString());
for(Observer observer : observers){
observer.update(food);
}
}
@Override
public String toString() {
return "Order[" +
"food=" + food +
", extras=" + extras +
']';
}
}
Observable abstract class:
public abstract class Observable<E>{
List\<Observer\<E\>\> observers = new ArrayList\<\>();
public void addObserver(Observer \<E\> observer){
observers.add(observer);
}
public void notifyObservers(E e){
for(Observer\<E\> observer : observers){
observer.update(e);
}
}
}
The console prints out the following(as you can see, it doesnt show update:
Restaurant Service: Order registered, client: Client [name=John, happiness=250.0], order Order[food=HOTDOG, extras=[KETCHUP, MUSTARD]]
Restaurant Service: Processing 1 order(s)
Food Factory: Preparing food, order Order[food=HOTDOG, extras=[KETCHUP, MUSTARD]]
Food Factory: Food Prepared, food: [KETCHUP, MUSTARD]HOTDOG
Notifying Observers of: Order{food=HOTDOG, extras=[KETCHUP]}
Process finished with exit code 0
I tried renaming the Type parameters, didn't work.