8000 encapsulated-collections.0 : baseline · test-driven-development/code@d92c216 · GitHub
[go: up one dir, main page]

Skip to content

Commit d92c216

Browse files
Duncan McGregordmcg
authored andcommitted
encapsulated-collections.0 : baseline
1 parent 1d281b0 commit d92c216

File tree

12 files changed

+366
-0
lines changed

12 files changed

+366
-0
lines changed

src/main/java/travelator/Id.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package travelator;
2+
3+
import java.util.Objects;
4+
import java.util.UUID;
5+
6+
public class Id<T> {
7+
private final String raw;
8+
9+
private Id(String raw) {
10+
this.raw = raw;
11+
}
12+
13+
public static <T> Id<T> of(String raw) {
14+
return new Id<T>(raw);
15+
}
16+
17+
public static <T> String raw(Id<T> id) {
18+
return id.raw;
19+
}
20+
21+
public static <T> Id<T> mint() {
22+
return Id.of(UUID.randomUUID().toString());
23+
}
24+
25+
@Override
26+
public boolean equals(Object o) {
27+
if (this == o) return true;
28+
if (o == null || getClass() != o.getClass()) return false;
29+
Id<?> id = (Id<?>) o;
30+
return raw.equals(id.raw);
31+
}
32+
33+
@Override
34+
public int hashCode() {
35+
return Objects.hash(raw);
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return raw;
41+
}
42+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package travelator;
2+
3+
public class Location {
4+
}

src/main/java/travelator/UI.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package travelator;
2+
3+
import travelator.itinerary.Journey;
4+
import travelator.itinerary.Route;
5+
6+
import java.time.Duration;
7+
8+
public class UI {
9+
10+
public void render(Iterable<Journey> route) {
11+
for (var journey : route) {
12+
render(journey);
13+
}
14+
}
15+
16+
17+
public void render(Route route) {
18+
for (int i = 0; i < route.size(); i++) {
19+
var journey = route.get(i);
20+
render(journey);
21+
}
22+
}
23+
24+
public void renderWithHeader(Route route) {
25+
renderHeader(
26+
route.getDepartsFrom(),
27+
route.getArrivesAt(),
28+
route.getDuration()
29+
);
30+
for (int i = 0; i < route.size(); i++) {
31+
var journey = route.get(i);
32+
render(journey);
33+
}
34+
}
35+
36+
private void render(Journey journey) {
37+
}
38+
39+
private void renderHeader(
40+
Location departsFrom,
41+
Location arrivesAt,
42+
Duration duration) {
43+
}
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package travelator.itinerary
2+
3+
class Accommodation {
4+
5+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package travelator.itinerary;
2+
3+
import travelator.Location;
4+
5+
import java.time.Duration;
6+
import java.time.ZonedDateTime;
7+
import java.util.Objects;
8+
9+
public class Interchange {
10+
private final Location arrivalLocation;
11+
private final ZonedDateTime arrivalTime;
12+
private final Location departureLocation;
13+
private final ZonedDateTime departureTime;
14+
15+
public Interchange(Location arrivalLocation, ZonedDateTime arrivalTime, Location departureLocation, ZonedDateTime departureTime) {
16+
this.arrivalLocation = arrivalLocation;
17+
this.arrivalTime = arrivalTime;
18+
this.departureLocation = departureLocation;
19+
this.departureTime = departureTime;
20+
}
21+
22+
public static Interchange between(Journey incoming, Journey outgoing) {
23+
return new Interchange(
24+
incoming.getArrivesAt(), incoming.getArrivalTime(),
25+
outgoing.getDepartsFrom(), outgoing.getDepartureTime());
26+
}
27+
28+
public Location getArrivalLocation() {
29+
return arrivalLocation;
30+
}
31+
32+
public ZonedDateTime getArrivalTime() {
33+
return arrivalTime;
34+
}
35+
36+
public Location getDepartureLocation() {
37+
return departureLocation;
38+
}
39+
40+
public ZonedDateTime getDepartureTime() {
41+
return departureTime;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) return true;
47+
if (o == null || getClass() != o.getClass()) return false;
48+
Interchange that = (Interchange) o;
49+
return arrivalLocation.equals(that.arrivalLocation) &&
50+
arrivalTime.equals(that.arrivalTime) &&
51+
departureLocation.equals(that.departureLocation) &&
52+
departureTime.equals(that.departureTime);
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(arrivalLocation, arrivalTime, departureLocation, departureTime);
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "RequiredStay{" +
63+
"arrivalLocation=" + arrivalLocation +
64+
", arrivalTime=" + arrivalTime +
65+
", departureLocation=" + departureLocation +
66+
", departureTime=" + departureTime +
67+
'}';
68+
}
69+
70+
public boolean isAccommodationRequired() {
71+
boolean waitIsOvernight =
72+
arrivalTime.toLocalDate().isBefore(departureTime.toLocalDate());
73+
Duration waitDuration =
74+
Duration.between(arrivalTime, departureTime);
75+
76+
return waitIsOvernight && waitDuration.toHours() >= 6;
77+
}
78+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package travelator.itinerary
2+
3+
import travelator.Id
4+
5+
class Itinerary(
6+
val id: Id<Itinerary>,
7+
val route: Route
8+
) {
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package travelator.itinerary
2+
3+
import travelator.Location
4+
import java.time.Duration
5+
import java.time.ZonedDateTime
6+
7+
data class Journey(
8+
val departsFrom: Location,
9+
val arrivesAt: Location,
10+
val departureTime: ZonedDateTime,
11+
val arrivalTime: ZonedDateTime,
12+
val method: TravelMethod
13+
) {
14+
val duration: Duration
15+
get() = Duration.between(departureTime, arrivalTime)
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package travelator.itinerary;
2+
3+
public interface Operator {
4+
String getName();
5+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package travelator.itinerary;
2+
3+
import travelator.Location;
4+
5+
import java.time.Duration;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Route {
10+
private final List<Journey> journeys; // <1>
11+
12+
public Route(List<Journey> journeys) {
13+
this.journeys = journeys; // <2>
14+
}
15+
16+
public int size() { // <3>
17+
return journeys.size();
18+
}
19+
20+
public Journey get(int index) { // <3>
21+
return journeys.get(index);
22+
}
23+
24+
public Location getDepartsFrom() { // <4>
25+
return get(0).getDepartsFrom();
26+
}
27+
28+
public Location getArrivesAt() { // <4>
29+
return get(size() - 1).getArrivesAt();
30+
}
31+
32+
public Duration getDuration() { // <4>
33+
return Duration.between(
34+
get(0).getDepartureTime(),
35+
get(size() - 1).getArrivalTime());
36+
}
37+
38+
public Route withJourneyAt(int index, Journey replacedBy) {
39+
var newJourneys = new ArrayList<>(this.journeys);
40+
newJourneys.set(index, replacedBy);
41+
return new Route(newJourneys);
42+
}
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package travelator.itinerary;
2+
3+
public enum TravelMethod {
4+
AIR,
5+
SEA,
6+
RAIL,
7+
BUS,
8+
CAR,
9+
CARRIAGE,
10+
CAMEL;
11+
}

0 commit comments

Comments
 (0)
0