From 92a05d33f5c0b64d0b950c9295fff647a1bed43f Mon Sep 17 00:00:00 2001
From: dansl <dansl@LAPTOP-SIKRVJU7.ern.nps.edu>
Date: Sun, 7 May 2023 11:37:59 -0700
Subject: [PATCH] Commit Assignment 5 Sloan

---
 MV3302ClassCode/src/mv3302/Ship.java          |  2 +-
 .../src/mv3302/ShipArrivalProcess.java        |  2 +-
 .../src/mv3302/TwoCranesBerth.java            | 20 +++++-----
 .../mv3302/run/testShipArrivalProcess.java    | 38 ++++++++++++++-----
 4 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/MV3302ClassCode/src/mv3302/Ship.java b/MV3302ClassCode/src/mv3302/Ship.java
index 2098bd386f..23d4cf6b14 100644
--- a/MV3302ClassCode/src/mv3302/Ship.java
+++ b/MV3302ClassCode/src/mv3302/Ship.java
@@ -10,7 +10,7 @@ import simkit.Entity;
  *
  * @author dansl
  */
-public class Ship extends Entity {
+public final class Ship extends Entity {
 
     private double remainingUnloadingTime;
 
diff --git a/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java b/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java
index f714e555c7..251236a69c 100644
--- a/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java
+++ b/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java
@@ -10,7 +10,7 @@ import simkit.random.RandomVariate;
  *
  * @author dansl
  */
-public class ShipArrivalProcess extends ArrivalProcess {
+public final class ShipArrivalProcess extends ArrivalProcess {
 
     private RandomVariate interarrivalTimeGenerator;
 
diff --git a/MV3302ClassCode/src/mv3302/TwoCranesBerth.java b/MV3302ClassCode/src/mv3302/TwoCranesBerth.java
index 24286c70bd..9e60dbbbbe 100644
--- a/MV3302ClassCode/src/mv3302/TwoCranesBerth.java
+++ b/MV3302ClassCode/src/mv3302/TwoCranesBerth.java
@@ -41,8 +41,9 @@ public class TwoCranesBerth extends SimEntityBase {
 
     public void doArrival(Ship ship) {
         ship.stampTime();
+        SortedSet<Ship> oldQueue = getQueue();
         queue.add(ship);
-        firePropertyChange("queue", getQueue());
+        firePropertyChange("queue", oldQueue, getQueue());
         if (berths.size() == 1) {
             waitDelay("SwitchTo1Crane", 0.0);
         }
@@ -57,7 +58,6 @@ public class TwoCranesBerth extends SimEntityBase {
         ship.stampTime();
         interrupt("EndUnload2Cranes");
         waitDelay("StartUnload1Crane", 0.0);
-        System.out.println(ship.getRemainingUnloadingTime());
         waitDelay("EndUnload1Crane", ship.getRemainingUnloadingTime(), ship);
     }
     
@@ -95,7 +95,7 @@ public class TwoCranesBerth extends SimEntityBase {
         SortedSet<Ship> oldBerths = getBerths();
         berths.remove(ship);
         firePropertyChange("berths", oldBerths, getBerths());
-        double oldTimeInSystem = timeInSystem;
+        double oldTimeInSystem = getTimeInSystem();
         timeInSystem = ship.getAge();
         firePropertyChange("timeInSystem", oldTimeInSystem, getTimeInSystem());
         if (!queue.isEmpty()){
@@ -111,13 +111,13 @@ public class TwoCranesBerth extends SimEntityBase {
         Ship ship = berths.first();
         berths.remove(ship);
         firePropertyChange("berths", oldBerths, getBerths());
+        double oldTimeInSystem = getTimeInSystem();
+        timeInSystem = ship.getAge();
+        firePropertyChange("timeInSystem", oldTimeInSystem, getTimeInSystem());
     }
     
-    public void Switchto2Cranes(){
-        SortedSet<Ship> oldBerths = getBerths();
+    public void doSwitchTo2Cranes(){
         Ship ship = berths.first();
-        berths.remove(ship);
-        firePropertyChange("berths", oldBerths, getBerths());
         ship.work(1);
         ship.stampTime();
         waitDelay("EndUnload2Cranes", ship.getRemainingUnloadingTime()/2);
@@ -142,13 +142,15 @@ public class TwoCranesBerth extends SimEntityBase {
      * @return the delayInQueue
      */
     public double getDelayInQueue() {
-        return delayInQueue;
+        double delayInQueueCopy = delayInQueue;
+        return delayInQueueCopy;
     }
 
     /**
      * @return the timeInSystem
      */
     public double getTimeInSystem() {
-        return timeInSystem;
+        double timeInSystemCopy = timeInSystem;
+        return timeInSystemCopy;
     }
 }
diff --git a/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java b/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java
index 5d099a2356..a037bd38f1 100644
--- a/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java
+++ b/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java
@@ -4,14 +4,15 @@
  */
 package mv3302.run;
 
-import mv3302.Ship;
 import mv3302.ShipArrivalProcess;
 import mv3302.TwoCranesBerth;
 import simkit.Adapter;
 import simkit.Schedule;
-import simkit.SimEventListener;
 import simkit.random.RandomVariate;
 import simkit.random.RandomVariateFactory;
+import simkit.stat.CollectionSizeTimeVaryingStats;
+import simkit.stat.MultipleSimpleStatsTally;
+import simkit.stat.SimpleStatsTally;
 import simkit.util.SimplePropertyDumper;
 
 /**
@@ -24,11 +25,11 @@ public class testShipArrivalProcess {
      * @param args the command line arguments
      */
     public static void main(String[] args) {
-        Ship ship = new Ship(5.0);
-        
-        System.out.println(ship.getRemainingUnloadingTime());
-        ship.work(2);
-        System.out.println(ship.getRemainingUnloadingTime());
+//        Ship ship = new Ship(5.0);
+//        
+//        System.out.println(ship.getRemainingUnloadingTime());
+//        ship.work(2);
+//        System.out.println(ship.getRemainingUnloadingTime());
         
         RandomVariate interarrivalTimeGenerator = RandomVariateFactory.getInstance("Exponential", .7);
         RandomVariate unloadingTimeGenerator = RandomVariateFactory.getInstance("Uniform", 0.5, 1.5);
@@ -38,7 +39,7 @@ public class testShipArrivalProcess {
         TwoCranesBerth twoCranesBerth = new TwoCranesBerth();
         System.out.println(twoCranesBerth);
         
-        //        TwoCranesBerth will "hear" ShipArrivalProcess(ShipArrival) events
+        //        TwoCranesBerth will "hear" ShipArrivalProcess(Arrival) events
         shipArrivalProcess.addSimEventListener(twoCranesBerth);
 
 //        The twoCranesBerth will "hear" the ShipArrival events of the
@@ -54,13 +55,30 @@ public class testShipArrivalProcess {
         
         //        Was "true" when debugging model
         Schedule.setVerbose(true);
-                double stopTime = 10.0;
+        double stopTime = 3650.0;
         Schedule.stopAtTime(stopTime);
 
 //        Initialized & run 
         Schedule.reset();
-        Schedule.startSimulation();
+        Schedule.startSimulation();       
+
+        //        Tally statistics are computed in the same way by "listening" 
+        SimpleStatsTally timeInSystemStat = new SimpleStatsTally("timeInSystem");
+        twoCranesBerth.addPropertyChangeListener(timeInSystemStat);
+        
+        CollectionSizeTimeVaryingStats numberInQueueStat = new CollectionSizeTimeVaryingStats("queue");
+        twoCranesBerth.addPropertyChangeListener(numberInQueueStat);
+        
+        
+        System.out.printf("%nSimulation ended at time %,.2f%n%n", Schedule.getSimTime());
         
+        System.out.printf("Number of Ships Arriving: %,d%n", shipArrivalProcess.getNumberArrivals());
+        System.out.printf("Number of Ships Unloaded: %,d%n", timeInSystemStat.getCount());
+        System.out.printf("Maximum # in Queue: %,d%n", numberInQueueStat.getCount());
+//        System.out.printf("Average # in Queue: %,.4f%n", numberInQueueStat.getMean());
+//        System.out.printf("Average # Busy Berths: %,.4f%n", numberInQueueStat.getMean());
+        System.out.printf("Average Time in System: %,.4f%n", timeInSystemStat.getMean());
+//        System.out.printf("Average Delay in Queue: %,.4f%n", delayInQueueStat.getMean());
     }
     
 }
-- 
GitLab