-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFleet.java
100 lines (86 loc) · 2.89 KB
/
Fleet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package it.uniud.poo.trasporti_2024;
import lombok.NonNull;
import java.util.*;
import java.util.stream.Collectors;
/**
* MISSION: to represent a fleet of trucks, which is a collection of trucks without duplicates.
* It is observable: it notifies its
* observers when a truck changes its status.
*/
public class Fleet extends ObservableImpl {
/**
* CONCRETE State: the trucks of the fleet which must be not null
* and a list of observers which must be not null
*/
private @NonNull Set<Truck> trucks = new HashSet<>();
/**
* Method to notify all observers, which should have a notify() method
*/
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(this);
}
}
/**
* Creates a fleet with the given trucks.
*
* @param trucks the trucks of the fleet. Must be non-null and not empty.
* @throws IllegalArgumentException if trucks is empty.
*/
public Fleet(@NonNull Set<Truck> trucks) {
if (trucks.isEmpty()) {
throw new IllegalArgumentException("A fleet must have at least one truck");
}
this.trucks = trucks;
this.observers = new ArrayList<>();
}
public Fleet() {
this.trucks = new HashSet<>();
this.observers = new ArrayList<>();
}
/**
* find the truck with that ID if it exists, otherwise return null
*
* @param truckId the ID of the truck to find
* @return the truck with that ID if it exists, otherwise return null
*/
public Truck getTruckById(@NonNull String truckId) {
return this.trucks.stream()
.filter(truck -> truck.getId().equals(truckId))
.findFirst()
.orElse(null);
}
/**
* Finds a suitable and available truck for given goods and date.
* TODO FIX this
* */
public Optional<Truck> findSuitableAndAvailableTruck(GoodsKind goods,
double quantity) {
return trucks.stream() // TODO FIX this
.filter(truck -> truck.isSuitableForGoods(goods, quantity))
.findFirst();
}
/**
* add a truck to the fleet
*/
public void addTruck(Truck truck) {
this.trucks.add(truck);
}
/**
* extract the set of trucks that are suitable for the given trip
* based on the goods to be transported
* @param trip
* @return the set of trucks that are suitable for the given kind and
* quantity of goods to be transported, possibly empty
*/
public Set<Truck> findSuitableTrucks(Trip trip) {
Set<Truck> results = new HashSet<>();
for (Truck truck : trucks) {
if (truck.isSuitableForGoods(trip.getGoodsKind(), trip.getGoodsQuantity())) {
results.add(truck);
}
}
return results;
}
}