Skip to content

Commit

Permalink
Revert "Revert "[Refactoring] Mediator pattern for ControlDesk.java""
Browse files Browse the repository at this point in the history
This reverts commit 552b2d2.
  • Loading branch information
jjunjji committed May 30, 2023
1 parent 552b2d2 commit 84a9d48
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 57 deletions.
34 changes: 34 additions & 0 deletions BowlerRegistrationManager.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
85 changes: 28 additions & 57 deletions ControlDesk.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@
class ControlDesk extends Thread {
private static final int SLEEPMS = 250;

/** The collection of Lanes */
private HashSet<Lane> 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<ControlDeskObserver> subscribers;

Expand All @@ -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();

}
Expand Down Expand Up @@ -106,44 +102,30 @@ 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<Lane> 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();
}

/**
*/

public void viewScores(Lane ln) {
// TODO: attach a LaneScoreView object to that lane
scoreManager.viewScores(ln);
}

/**
Expand All @@ -154,14 +136,7 @@ public void viewScores(Lane ln) {
*/

public void addPartyQueue(Vector<String> partyNicks) {
Vector<Bowler> 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);
}

/**
Expand All @@ -172,26 +147,18 @@ public void addPartyQueue(Vector<String> partyNicks) {
*/

public Vector<String> getPartyQueue() {
Vector<String> 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();
}

/**
Expand Down Expand Up @@ -224,12 +191,16 @@ public void publish(PartyQueueEvent event) {

/**
* Accessor method for lanes
*
*
* @return a HashSet of Lanes
*
*/

public HashSet<Lane> getLanes() {
return lanes;
return laneManager.getLanes();
}

public Queue getWaitQueue(){
return partyQueueManager.getWaitQueue();
}
}
63 changes: 63 additions & 0 deletions LaneManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.HashSet;
import java.util.Iterator;

public class LaneManager {
private ControlDesk controlDesk;

/** The collection of Lanes */
private HashSet<Lane> 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<Lane> 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<Lane> getLanes() {
return lanes;
}
}
54 changes: 54 additions & 0 deletions PartyQueueManager.java
Original file line number Diff line number Diff line change
@@ -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<String> partyNicks) {
Vector<Bowler> 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<String> getPartyQueue() {
Vector<String> 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;
}
}
11 changes: 11 additions & 0 deletions ScoreManager.java
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 84a9d48

Please sign in to comment.