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

I uhh, I forgor.

parent d657145f
Branches
Tags v0.13
No related merge requests found
......@@ -4,6 +4,7 @@ import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalTime;
/** Represents a Departure.
* This class is responsible for keeping track og original departure time,
* destination, delay to departure, departure track, line, and train number.
......@@ -21,7 +22,41 @@ class Departure {
// constructors
/** Throws an error if invalid input is given to Departure().
*
* @param duration A Duration representing a delay.
* @throws IllegalArgumentException For illegal durations.
*/
private void verifyDeparture(
int trainNumber, String line, String desination, LocalTime departureTime) {
if (trainNumber <= 0) {
throw new IllegalArgumentException(
"Train number must be a whole number larger than zero."
);
}
if (line.isBlank() || line.isEmpty()) {
throw new IllegalArgumentException(
"Departure must have a line"
);
}
if (desination.isBlank() || desination.isEmpty()) {
throw new IllegalArgumentException(
"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) {
verifyDeparture(trainNumber, line, destination, departureTime);
this.trainNumber = trainNumber;
this.line = line;
this.destination = destination;
......@@ -40,9 +75,9 @@ class Departure {
* @throws IllegalArgumentException for illegal durations.
*/
private void verifySetDelay(Duration duration) {
if (this.originalDepartureTime.minus(duration).isBefore(LocalTime.of(0, 0))) {
if (this.originalDepartureTime.plus(duration).isBefore(this.originalDepartureTime)) {
throw new IllegalArgumentException(
"Substracting this delay duration makes the departure time LocalTime less than zero.");
"Substracting this delay duration makes the departure time LocalTime less than original departure.");
}
if (this.originalDepartureTime.plus(duration).isAfter(LocalTime.MAX)) {
throw new IllegalArgumentException(
......@@ -205,6 +240,9 @@ class Departure {
// line methods
/** Gets the line for this departure.
*
* @return A String representing a line.
......
......@@ -3,6 +3,7 @@ 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;
......@@ -25,10 +26,30 @@ class DepartureTest {
@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(-1, "LineA", "DestinationA", LocalTime.of(9, 0)));
() -> 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, null, "DestinationA", LocalTime.of(9, 0)));
() -> 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
......@@ -44,13 +65,12 @@ class DepartureTest {
@Test
void testSetDelay() {
// Test setting delay with valid and invalid values
Departure departure = new Departure(123, "LineA", "DestinationA", LocalTime.of(9, 0));
departure.setDelay(Duration.ofMinutes(10)); // Valid delay
departure.setDelay(Duration.ofMinutes(10));
assertEquals(Duration.ofMinutes(10), departure.getDelay());
assertThrows(IllegalArgumentException.class,
() -> departure.setDelay(Duration.ofMinutes(-5)));
() -> 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