0

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.

Mate25
  • 43
  • 5
  • 1
    Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your source code as a working [mcve], which can be compiled and tested by others. – Progman Apr 02 '22 at 16:19
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Apr 02 '22 at 18:45
  • Where are you populating your `observers` arraylist in your code? Try running through your code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems#:~:text=A%20debugger%20is%20a%20program,while%20your%20program%20is%20running.). – Alias Cartellano Apr 02 '22 at 19:32

0 Answers0