forked from oopd-gu-chalmers/lab1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCarWorkshop.java
37 lines (32 loc) · 967 Bytes
/
CarWorkshop.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
import java.util.List;
import java.util.ArrayList;
public class CarWorkshop <T extends Vehicle>{
private int capacity;
private int nrCars = 0;
private List<T> vehicles = new ArrayList<>();
public CarWorkshop(int capacity) {
this.capacity = capacity;
}
public void turnInCar(T vehicle) {
if (nrCars < this.capacity) {
vehicles.add(vehicle);
nrCars += 1;
} else {
throw new IllegalArgumentException("The workshop is at full capacity.");
}
}
public T removeVehicle (int index) {
T removedVehicle = null;
if (index >= 0 && index < vehicles.size()){
removedVehicle = vehicles.get(index);
vehicles.remove(index);
nrCars -= 1;
return removedVehicle;
}
else{ throw new IllegalArgumentException("Index not valid.");
}
}
public int getNrCars() {
return nrCars;
}
}