Skip to content
Snippets Groups Projects
Commit 8ed24bfc authored by Lars-Johan Larsen's avatar Lars-Johan Larsen :speech_balloon:
Browse files

Working Departure class and corresponding test class

parent 39fc1e77
Branches
Tags v0.14.2
No related merge requests found
...@@ -5,9 +5,9 @@ import edu.ntnu.stud.menubuilder.UserInterface; ...@@ -5,9 +5,9 @@ import edu.ntnu.stud.menubuilder.UserInterface;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
UserInterface blarg = new UserInterface(); UserInterface myUserInterface = new UserInterface();
blarg.init(); myUserInterface.init();
blarg.start(); myUserInterface.start();
} }
......
package edu.ntnu.stud.menubuilder.trainz; package edu.ntnu.stud.menubuilder.trainz;
import java.time.DateTimeException;
import java.time.Duration; import java.time.Duration;
import java.time.LocalTime; import java.time.LocalDateTime;
/** Represents a Departure. /** Represents a Departure.
...@@ -13,7 +12,7 @@ import java.time.LocalTime; ...@@ -13,7 +12,7 @@ import java.time.LocalTime;
class Departure { class Departure {
private Duration delay; private Duration delay;
private int track; private int track;
private final LocalTime originalDepartureTime; private final LocalDateTime originalDepartureTime;
private final String destination; private final String destination;
private final String line; private final String line;
private final int trainNumber; //can be replaced with a reference to a train object? private final int trainNumber; //can be replaced with a reference to a train object?
...@@ -28,7 +27,8 @@ class Departure { ...@@ -28,7 +27,8 @@ class Departure {
* @throws IllegalArgumentException For illegal durations. * @throws IllegalArgumentException For illegal durations.
*/ */
private void verifyDeparture( private void verifyDeparture(
int trainNumber, String line, String desination, LocalTime departureTime) { int trainNumber, String line, String desination) {
if (trainNumber <= 0) { if (trainNumber <= 0) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Train number must be a whole number larger than zero." "Train number must be a whole number larger than zero."
...@@ -46,17 +46,11 @@ class Departure { ...@@ -46,17 +46,11 @@ class Departure {
"Departure must have a destination." "Departure must have a destination."
); );
} }
if (departureTime.isAfter(LocalTime.MAX) || departureTime.isBefore(LocalTime.of(0, 0))) {
throw new IllegalArgumentException(
"Departure time must be a valid time of day."
);
}
} }
public Departure(int trainNumber, String line, String destination, LocalTime departureTime) { public Departure(int trainNumber, String line, String destination, LocalDateTime departureTime) {
verifyDeparture(trainNumber, line, destination, departureTime); verifyDeparture(trainNumber, line, destination);
this.trainNumber = trainNumber; this.trainNumber = trainNumber;
this.line = line; this.line = line;
this.destination = destination; this.destination = destination;
...@@ -79,32 +73,11 @@ class Departure { ...@@ -79,32 +73,11 @@ class Departure {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The new delay duration makes the departure before originalDeparture."); "The new delay duration makes the departure before originalDeparture.");
} }
if (this.originalDepartureTime.plus(duration).isAfter(LocalTime.MAX)) {
throw new IllegalArgumentException(
"Adding this delay duration exceeds the maximum value for the departure time LocalTime");
}
} }
/** Throws an error if invalid input is given to addDelay(). /** Sets a delay from this departure's departureTime(LocalDateTime).
* *
* @param duration A Duration representing a delay. * @param duration A Duration to add to the LocalDateTime of the departure.
* @throws IllegalArgumentException for illegal durations.
*/
private void verifyAddDelay(Duration duration) {
if (this.originalDepartureTime.plus(duration).isAfter(LocalTime.MAX)) {
throw new IllegalArgumentException(
"Adding this delay duration exceeds the maximum value for the departure time LocalTime");
}
if (duration.isZero()) {
throw new IllegalArgumentException(
"Cannot add a zero duration delay, this is pointless."
);
}
}
/** Sets a delay from this departure's departureTime(LocalTime).
*
* @param duration A Duration to add to the LocalTime of the departure.
* @throws IllegalArgumentException - if the Duration would result in an invalid departure time. * @throws IllegalArgumentException - if the Duration would result in an invalid departure time.
*/ */
public void setDelay(Duration duration) { public void setDelay(Duration duration) {
...@@ -112,18 +85,6 @@ class Departure { ...@@ -112,18 +85,6 @@ class Departure {
this.delay = duration; this.delay = duration;
} }
/** Adds a Duration from this departure's departuretime(LocalTime).
*
* @param duration A Duration to add to the LocalTime of the departure.
* @throws ArithmeticException - if numeric overflow occurs.
* @throws DateTimeException - if the addition cannot be made.
* @throws IllegalArgumentException - if the Duration would result in an invalid departure time.
*/
public void addDelay(Duration duration) {
verifyAddDelay(duration);
this.delay = this.delay.plus(duration);
}
/** Gets the delay for this departure. /** Gets the delay for this departure.
* *
* @return a Duration representing a delay. * @return a Duration representing a delay.
...@@ -173,17 +134,17 @@ class Departure { ...@@ -173,17 +134,17 @@ class Departure {
/** Gets the current departure time, factoring in the current delay. /** Gets the current departure time, factoring in the current delay.
* *
* @return a LocalTime representing a time of the day. * @return a LocalDateTime representing a time of the day.
*/ */
public LocalTime getCurrentDepartureTime() { public LocalDateTime getCurrentDepartureTime() {
return this.originalDepartureTime.plus(this.delay); return this.originalDepartureTime.plus(this.delay);
} }
/** Gets the originally planned departure time, without factoring in the current delay. /** Gets the originally planned departure time, without factoring in the current delay.
* *
* @return a LocalTime representing a time of the day. * @return a LocalDateTime representing a time of the day.
*/ */
public LocalTime getOriginalDepartureTime() { public LocalDateTime getOriginalDepartureTime() {
return this.originalDepartureTime; return this.originalDepartureTime;
} }
......
package edu.ntnu.stud.menubuilder.trainz;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.time.Duration;
import java.time.LocalDateTime;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
class DepTest {
private Departure departure;
@Nested
@DisplayName("Tests Departure Creation")
class DepartureCreationTests {
@Test
@DisplayName("Valid Departure Creation Test")
void testValidDepartureCreation() {
//Arrange
int trainNumber = 1;
String line = "AB123";
String destination = "TestDestination";
LocalDateTime departureTime = LocalDateTime.of(0, 1, 1, 12, 0);
Duration duration = Duration.ZERO;
int trackNumber = -1;
//Act
Departure departure = new Departure(trainNumber, line, destination, departureTime);
//Assert
try {
assertEquals(trainNumber, departure.getTrainNumber());
assertEquals(line, departure.getLine());
assertEquals(destination, departure.getDestination());
assertEquals(departureTime, departure.getOriginalDepartureTime());
assertEquals(trackNumber, departure.getTrack());
assertEquals(duration, departure.getDelay());
} catch (AssertionError e) {
fail("Test failed: " + e.getMessage());
}
}
@Test()
@DisplayName("Invalid trainNumber Departure Creation")
void testInvalidTrainNumberDepartureCreation() {
//Arrange
int trainNumber = -0;
String line = "AB123";
String destination = "TestDestination";
LocalDateTime departureTime = LocalDateTime.of(0, 1, 1, 12, 0);
try {
//Act
Departure departure = new Departure(trainNumber, line, destination, departureTime);
fail("Negative Test failed: Object creation was successfull.");
} catch (IllegalArgumentException e) {
//Assert
assertInstanceOf(IllegalArgumentException.class, e);
}
}
@Test()
@DisplayName("Invalid Line Departure Creation")
void testInvalidLineDepartureCreation() {
//Arrange
int trainNumber = 1;
String line = "";
String destination = "";
LocalDateTime departureTime = LocalDateTime.of(0, 1, 1, 12, 0);
try {
//Act
Departure departure = new Departure(trainNumber, line, destination, departureTime);
fail("Negative Test failed: Object creation was successfull.");
} catch (IllegalArgumentException e) {
//Assert
assertInstanceOf(IllegalArgumentException.class, e);
}
}
@Test()
@DisplayName("Invalid Destination Departure Creation")
void testInvalidDestinationDepartureCreation() {
//Arrange
int trainNumber = 1;
String line = "AB123";
String destination = "";
LocalDateTime departureTime = LocalDateTime.of(0, 1, 1, 12, 0);
try {
//Act
Departure departure = new Departure(trainNumber, line, destination, departureTime);
fail("Negative Test failed: Object creation was successfull.");
} catch (IllegalArgumentException e) {
//Assert
assertInstanceOf(IllegalArgumentException.class, e);
}
}
@Nested
@DisplayName("Tests Delay")
class DelayTests {
@BeforeEach
void seUp() {
// Arrange
int trainNumber = 123;
String line = "LineA";
String destination = "DestinationA";
LocalDateTime departureTime = LocalDateTime.of(0, 1, 1, 12, 0);
departure = new Departure(trainNumber, line, destination, departureTime);
}
@AfterEach
void tearDown() {
departure = null;
}
@Test
@DisplayName("Set Delay Test")
void testSetDelay() {
// Arrange
Duration delay = Duration.ofMinutes(10);
// Act
departure.setDelay(delay);
// Assert
try {
assertEquals(delay, departure.getDelay());
} catch (AssertionError e) {
fail("Test failed: " + e.getMessage());
}
}
@Test
@DisplayName("Set Delay Underflow Duration")
void testSetDelayUnderflowDuration() {
// Arrange
Duration delay = Duration.ofMinutes(-10);
// Act - Assert
assertThrows(IllegalArgumentException.class, () -> departure.setDelay(delay));
}
}
}
}
\ No newline at end of file
package edu.ntnu.stud.menubuilder.trainz;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalTime;
import org.junit.jupiter.api.Test;
class DepartureTest {
@Test
void testValidDepartureCreation() {
LocalTime departureTime = LocalTime.of(9, 0);
Departure departure = new Departure(123, "LineA", "DestinationA", departureTime);
assertEquals(123, departure.getTrainNumber());
assertEquals("LineA", departure.getLine());
assertEquals("DestinationA", departure.getDestination());
assertEquals(departureTime, departure.getOriginalDepartureTime());
assertEquals(-1, departure.getTrack());
assertEquals(Duration.ZERO, departure.getDelay());
}
@Test
void testInvalidDepartureCreation() {
//test illegal departure time
assertThrows(DateTimeException.class,
() -> new Departure(123, "LineA", "DestinationA", LocalTime.of(-1, 0)));
assertThrows(DateTimeException.class,
() -> new Departure(123, "LineA", "DestinationA", LocalTime.of(24, 1)));
//test illegal line
assertThrows(NullPointerException.class,
() -> new Departure(123, null, "DestinationA", LocalTime.of(9, 0)));
assertThrows(IllegalArgumentException.class,
() -> new Departure(123, "", "DestinationA", LocalTime.of(9, 0)));
//test illegal destination
assertThrows(NullPointerException.class,
() -> new Departure(123, "LineA", null, LocalTime.of(9, 0)));
assertThrows(IllegalArgumentException.class,
() -> new Departure(123, "LineA", "", LocalTime.of(9, 0)));
//test illegal train number
assertThrows(IllegalArgumentException.class,
() -> new Departure(0, "LineA", "DestinationA", LocalTime.of(9, 0)));
assertThrows(IllegalArgumentException.class,
() -> new Departure(-1, "LineA", "DestinationA", LocalTime.of(9, 0)));
}
@Test
void testSetTrack() {
Departure departure = new Departure(123, "LineA", "DestinationA", LocalTime.of(9, 0));
departure.setTrack(5);
assertEquals(5, departure.getTrack());
assertThrows(IllegalArgumentException.class,
() -> departure.setTrack(0));
}
@Test
void testSetDelay() {
Departure departure = new Departure(123, "LineA", "DestinationA", LocalTime.of(9, 0));
departure.setDelay(Duration.ofMinutes(10));
assertEquals(Duration.ofMinutes(10), departure.getDelay());
assertThrows(IllegalArgumentException.class,
() -> departure.setDelay(Duration.ofHours(-23)));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment