From 84a9d48b66bc0ab2828c6f9d16ec7b9ce04e1c92 Mon Sep 17 00:00:00 2001 From: jjunjji Date: Wed, 31 May 2023 00:20:24 +0900 Subject: [PATCH] Revert "Revert "[Refactoring] Mediator pattern for ControlDesk.java"" This reverts commit 552b2d2c65a9649836af70788522e3a639214302. --- BowlerRegistrationManager.java | 34 ++++++++++++++ ControlDesk.java | 85 +++++++++++----------------------- LaneManager.java | 63 +++++++++++++++++++++++++ PartyQueueManager.java | 54 +++++++++++++++++++++ ScoreManager.java | 11 +++++ 5 files changed, 190 insertions(+), 57 deletions(-) create mode 100644 BowlerRegistrationManager.java create mode 100644 LaneManager.java create mode 100644 PartyQueueManager.java create mode 100644 ScoreManager.java diff --git a/BowlerRegistrationManager.java b/BowlerRegistrationManager.java new file mode 100644 index 0000000..40b1f00 --- /dev/null +++ b/BowlerRegistrationManager.java @@ -0,0 +1,34 @@ +import java.io.IOException; + +public class BowlerRegistrationManager { + private ControlDesk controlDesk; + + public BowlerRegistrationManager(ControlDesk controlDesk) { + this.controlDesk = controlDesk; + } + + /** + * Retrieves a matching Bowler from the bowler database. + * + * @param nickName The NickName of the Bowler + * + * @return a Bowler object. + * + */ + + public Bowler registerPatron(String nickName) { + Bowler patron = null; + + try { + // only one patron / nick.... no dupes, no checks + + patron = BowlerFile.getBowlerInfo(nickName); + + } catch (IOException e) { + System.err.println("Error..." + e); + } + + return patron; + } + +} diff --git a/ControlDesk.java b/ControlDesk.java index 9b85791..86dd262 100644 --- a/ControlDesk.java +++ b/ControlDesk.java @@ -46,15 +46,14 @@ class ControlDesk extends Thread { private static final int SLEEPMS = 250; - /** The collection of Lanes */ - private HashSet lanes; + private BowlerRegistrationManager registrationManager; - /** The party wait queue */ - private Queue partyQueue; + private PartyQueueManager partyQueueManager; + + private LaneManager laneManager; + + private ScoreManager scoreManager; - /** The number of lanes represented */ - private int numLanes; - /** The collection of subscribers */ private Vector subscribers; @@ -66,16 +65,13 @@ class ControlDesk extends Thread { */ public ControlDesk(int numLanes) { - this.numLanes = numLanes; - lanes = new HashSet<>(numLanes); - partyQueue = new Queue(); + registrationManager = new BowlerRegistrationManager(this); + partyQueueManager = new PartyQueueManager(this); + laneManager = new LaneManager(this, numLanes); + scoreManager = new ScoreManager(this); subscribers = new Vector<>(); - for (int i = 0; i < numLanes; i++) { - lanes.add(new Lane()); - } - this.start(); } @@ -106,37 +102,22 @@ public void run() { */ private Bowler registerPatron(String nickName) { - Bowler patron = null; - - try { - // only one patron / nick.... no dupes, no checks - - patron = BowlerFile.getBowlerInfo(nickName); - - } catch (IOException e) { - System.err.println("Error..." + e); - } + return registrationManager.registerPatron(nickName); + } - return patron; + //PartyQueueManager에서 사용하기 위한 public 메소드 + public Bowler registerBowler(String nickName) { + return registerPatron(nickName); } + /** * Iterate through the available lanes and assign the paties in the wait queue if lanes are available. * */ public void assignLane() { - Iterator it = lanes.iterator(); - - while (it.hasNext() && partyQueue.hasMoreElements()) { - Lane curLane = it.next(); - - if (!curLane.isPartyAssigned()) { - System.out.println("ok... assigning this party"); - curLane.assignParty(((Party) partyQueue.next())); - } - } - publish(new PartyQueueEvent(getPartyQueue())); + laneManager.assignLane(); } /** @@ -144,6 +125,7 @@ public void assignLane() { public void viewScores(Lane ln) { // TODO: attach a LaneScoreView object to that lane + scoreManager.viewScores(ln); } /** @@ -154,14 +136,7 @@ public void viewScores(Lane ln) { */ public void addPartyQueue(Vector partyNicks) { - Vector partyBowlers = new Vector<>(); - for (String partyNick : partyNicks) { - Bowler newBowler = registerPatron(partyNick); - partyBowlers.add(newBowler); - } - Party newParty = new Party(partyBowlers); - partyQueue.add(newParty); - publish(new PartyQueueEvent(getPartyQueue())); + partyQueueManager.addPartyQueue(partyNicks); } /** @@ -172,26 +147,18 @@ public void addPartyQueue(Vector partyNicks) { */ public Vector getPartyQueue() { - Vector displayPartyQueue = new Vector<>(); - for ( int i=0; i < ( partyQueue.asVector()).size(); i++ ) { - String nextParty = - ((Bowler) ( ((Party) partyQueue.asVector().get( i ) ).getMembers()) - .get(0)) - .getNickName() + "'s Party"; - displayPartyQueue.addElement(nextParty); - } - return displayPartyQueue; + return partyQueueManager.getPartyQueue(); } /** * Accessor for the number of lanes represented by the ControlDesk - * + * * @return an int containing the number of lanes represented * */ public int getNumLanes() { - return numLanes; + return laneManager.getNumLanes(); } /** @@ -224,12 +191,16 @@ public void publish(PartyQueueEvent event) { /** * Accessor method for lanes - * + * * @return a HashSet of Lanes * */ public HashSet getLanes() { - return lanes; + return laneManager.getLanes(); + } + + public Queue getWaitQueue(){ + return partyQueueManager.getWaitQueue(); } } diff --git a/LaneManager.java b/LaneManager.java new file mode 100644 index 0000000..dc68ab8 --- /dev/null +++ b/LaneManager.java @@ -0,0 +1,63 @@ +import java.util.HashSet; +import java.util.Iterator; + +public class LaneManager { + private ControlDesk controlDesk; + + /** The collection of Lanes */ + private HashSet lanes; + + /** The number of lanes represented */ + private int numLanes; + + public LaneManager(ControlDesk controlDesk, int numLanes) { + this.controlDesk = controlDesk; + this.lanes = new HashSet<>(numLanes); + this.numLanes = numLanes; + + for (int i = 0; i < numLanes; i++) { + lanes.add(new Lane()); + } + } + + /** + * Iterate through the available lanes and assign the paties in the wait queue if lanes are available. + * + */ + + public void assignLane() { + Iterator it = lanes.iterator(); + + while (it.hasNext() && controlDesk.getWaitQueue().hasMoreElements()) { + Lane curLane = it.next(); + + if (!curLane.isPartyAssigned()) { + System.out.println("ok... assigning this party"); + curLane.assignParty(((Party) controlDesk.getWaitQueue().next())); + } + } + controlDesk.publish(new PartyQueueEvent(controlDesk.getPartyQueue())); + } + + /** + * Accessor for the number of lanes represented by the ControlDesk + * + * @return an int containing the number of lanes represented + * + */ + + public int getNumLanes() { + return numLanes; + } + + /** + * Accessor method for lanes + * + * @return a HashSet of Lanes + * + */ + + public HashSet getLanes() { + return lanes; + } +} diff --git a/PartyQueueManager.java b/PartyQueueManager.java new file mode 100644 index 0000000..4d6e09d --- /dev/null +++ b/PartyQueueManager.java @@ -0,0 +1,54 @@ +import java.util.Vector; + +public class PartyQueueManager { + private ControlDesk controlDesk; + + /** The party wait queue */ + private Queue partyQueue; + + public PartyQueueManager(ControlDesk controlDesk) { + this.controlDesk = controlDesk; + partyQueue = new Queue(); + } + + /** + * Creates a party from a Vector of nickNAmes and adds them to the wait queue. + * + * @param partyNicks A Vector of NickNames + * + */ + + public void addPartyQueue(Vector partyNicks) { + Vector partyBowlers = new Vector<>(); + for (String partyNick : partyNicks) { + Bowler newBowler = controlDesk.registerBowler(partyNick); + partyBowlers.add(newBowler); + } + Party newParty = new Party(partyBowlers); + partyQueue.add(newParty); + controlDesk.publish(new PartyQueueEvent(getPartyQueue())); + } + + /** + * Returns a Vector of party names to be displayed in the GUI representation of the wait queue. + * + * @return a Vecotr of Strings + * + */ + + public Vector getPartyQueue() { + Vector displayPartyQueue = new Vector<>(); + for ( int i=0; i < ( partyQueue.asVector()).size(); i++ ) { + String nextParty = + ((Bowler) ( ((Party) partyQueue.asVector().get( i ) ).getMembers()) + .get(0)) + .getNickName() + "'s Party"; + displayPartyQueue.addElement(nextParty); + } + return displayPartyQueue; + } + + public Queue getWaitQueue(){ + return this.partyQueue; + } +} diff --git a/ScoreManager.java b/ScoreManager.java new file mode 100644 index 0000000..b288b95 --- /dev/null +++ b/ScoreManager.java @@ -0,0 +1,11 @@ +public class ScoreManager { + private ControlDesk controlDesk; + + public ScoreManager(ControlDesk controlDesk) { + this.controlDesk = controlDesk; + } + + public void viewScores(Lane ln) { + // TODO: attach a LaneScoreView object to that lane + } +}