package C1.c; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import java.util.ArrayList; public class Broker { public Broker() { topics = new ArrayList<>(); numSubscribedLabel = new Label("0"); rect = new Rectangle(0,0,0, 20); rect.setFill(Color.LIGHTBLUE); view = new StackPane(); view.getChildren().addAll(rect, numSubscribedLabel); } public Topic createTopic (String topicName){ Topic topic; if ((topic=findTopic(topicName))==null) { topic = new Topic(topicName); topics.add(topic); } return topic; } public boolean subscribe (Subscriber sub){ Topic topic; String topicName = sub.getTopicName(); if ((topic=findTopic(topicName))!=null) { topic.subscribe(sub); numSubscribedLabel.setText(topic.getNumSubscribed()+""); rect.setWidth(rect.getWidth()+20); return true; } else return false; // topic does not exist. } public StackPane getView(){ return view; } private Topic findTopic (String topicName) { for (Topic topic : topics) { if (topic.hasThisName(topicName)) { return topic; } } return null; } private StackPane view; private Rectangle rect; private Label numSubscribedLabel; private ArrayList topics; }