Run it after compiling with: + *
{@code
+ * java -cp build/classes simulation.examples.CountdownSimulation
+ * }
+ */
+public class CountdownSimulation extends Simulation {
+
+ private static final int DEFAULT_START = 5;
+
+ private int remaining;
+
+ /**
+ * Creates a countdown simulation.
+ *
+ * @param start the first number to print
+ */
+ public CountdownSimulation(int start) {
+ remaining = start;
+ }
+
+ /**
+ * Runs the example from the command line.
+ *
+ * @param args ignored
+ */
+ public static void main(String[] args) {
+ new CountdownSimulation(DEFAULT_START).run();
+ }
+
+ @Override
+ protected void initialize() {
+ scheduleEvent(new TickEvent(0.0));
+ }
+
+ private void tick() {
+ System.out.printf("time %.1f: %d%n", getClock(), remaining);
+ remaining--;
+
+ if (remaining <= 0) {
+ stop();
+ } else {
+ scheduleEvent(new TickEvent(getClock() + 1.0));
+ }
+ }
+
+ private class TickEvent extends Event {
+
+ TickEvent(double time) {
+ super(time);
+ }
+
+ @Override
+ public void execute(Simulation sim) {
+ tick();
+ }
+ }
+}
diff --git a/test/java/simulation/EventQueueTest.java b/test/java/simulation/EventQueueTest.java
index 39e64b4..712372e 100644
--- a/test/java/simulation/EventQueueTest.java
+++ b/test/java/simulation/EventQueueTest.java
@@ -3,7 +3,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit tests for {@link EventQueue}.
diff --git a/test/java/simulation/EventTest.java b/test/java/simulation/EventTest.java
index 509098c..1ca3e40 100644
--- a/test/java/simulation/EventTest.java
+++ b/test/java/simulation/EventTest.java
@@ -2,7 +2,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit tests for {@link Event}.
@@ -11,10 +13,10 @@
class EventTest {
/** Concrete subclass used for testing. */
- private static class TestEvent extends Event {
+ private static class StubEvent extends Event {
boolean executed = false;
- TestEvent(double time) {
+ StubEvent(double time) {
super(time);
}
@@ -27,53 +29,53 @@ public void execute(Simulation sim) {
@Test
@DisplayName("constructor stores the scheduled time")
void constructorStoresTime() {
- Event e = new TestEvent(3.14);
+ Event e = new StubEvent(3.14);
assertEquals(3.14, e.getTime(), 1e-9);
}
@Test
@DisplayName("constructor accepts time zero")
void constructorAcceptsZero() {
- Event e = new TestEvent(0.0);
+ Event e = new StubEvent(0.0);
assertEquals(0.0, e.getTime());
}
@Test
@DisplayName("constructor rejects negative time")
void constructorRejectsNegativeTime() {
- assertThrows(IllegalArgumentException.class, () -> new TestEvent(-1.0));
+ assertThrows(IllegalArgumentException.class, () -> new StubEvent(-1.0));
}
@Test
@DisplayName("compareTo: earlier event is less than later event")
void compareToEarlierIsLess() {
- Event early = new TestEvent(1.0);
- Event late = new TestEvent(2.0);
+ Event early = new StubEvent(1.0);
+ Event late = new StubEvent(2.0);
assertTrue(early.compareTo(late) < 0);
}
@Test
@DisplayName("compareTo: later event is greater than earlier event")
void compareToLaterIsGreater() {
- Event early = new TestEvent(1.0);
- Event late = new TestEvent(2.0);
+ Event early = new StubEvent(1.0);
+ Event late = new StubEvent(2.0);
assertTrue(late.compareTo(early) > 0);
}
@Test
@DisplayName("compareTo: equal times compare as zero")
void compareToEqualIsZero() {
- Event a = new TestEvent(5.0);
- Event b = new TestEvent(5.0);
+ Event a = new StubEvent(5.0);
+ Event b = new StubEvent(5.0);
assertEquals(0, a.compareTo(b));
}
@Test
@DisplayName("toString contains the class name and time")
void toStringContainsInfo() {
- Event e = new TestEvent(2.5);
+ Event e = new StubEvent(2.5);
String s = e.toString();
- assertTrue(s.contains("TestEvent"), "should contain class name");
+ assertTrue(s.contains("StubEvent"), "should contain class name");
assertTrue(s.contains("2.5") || s.contains("2.5000"), "should contain time");
}
}
diff --git a/test/java/simulation/SimulationTest.java b/test/java/simulation/SimulationTest.java
index ce49267..8215d53 100644
--- a/test/java/simulation/SimulationTest.java
+++ b/test/java/simulation/SimulationTest.java
@@ -2,7 +2,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit tests for {@link Simulation}.
diff --git a/test/java/simulation/StatisticsTest.java b/test/java/simulation/StatisticsTest.java
index c370912..f9444f0 100644
--- a/test/java/simulation/StatisticsTest.java
+++ b/test/java/simulation/StatisticsTest.java
@@ -3,7 +3,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit tests for {@link Statistics}.