forked from oopd-gu-chalmers/lab1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCarTransport.java
83 lines (69 loc) · 2.65 KB
/
CarTransport.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
import java.awt.*;
import java.util.Stack;
public class CarTransport extends Vehicle {
protected CargoTruck CargoTruck;
private int loadCapacity;
protected Stack<Vehicle> cargo = new Stack<>();
public CarTransport(int loadCapacity) {
super ("Biltransport", 2, 175,"North", Color.red);
CargoTruck = new CargoTruck(0,1);
this.loadCapacity = loadCapacity;
}
protected void raiseTruckBed(){
if (getCurrentSpeed() > 0) {
throw new IllegalStateException
("Truck must be stationary to raise or lower the truckbed");
} else if (CargoTruck.getAngle() == CargoTruck.getMin()){
CargoTruck.setAngle(CargoTruck.getMax());
} else {
throw new IllegalStateException
("Truckbed is already raised");
}
}
protected void lowerTruckBed() {
if (getCurrentSpeed() > 0) {
throw new IllegalStateException
("Truck must be stationary to raise or lower the truckbed");
} else if (CargoTruck.getAngle() == CargoTruck.getMax()) {
CargoTruck.setAngle(CargoTruck.getMin());
} else {
throw new IllegalStateException
("Truckbed is already lowered");
}
}
protected void loadCar(Vehicle car) {
if (getCurrentSpeed() > 0 || CargoTruck.getAngle() != 0) {
throw new IllegalStateException(
"Truck must be both stationary and with the truckbed lowered to load cars.");
} else if (Math.abs(getX() - car.getX()) >= 10 && Math.abs(getY() - car.getY()) >= 10) {
throw new IllegalStateException(
"Car must be within 10 meters of the truck to be loaded.");
} else if (car instanceof CarTransport) {
throw new IllegalArgumentException(
"Cannot load vehicle transports");
} else {
if (cargo.size() < this.loadCapacity) {
cargo.push(car);
}
}
}
protected void unLoadCar() {
if (getCurrentSpeed() > 0 && CargoTruck.getAngle() != 0) {
throw new IllegalStateException(
"Truck must be both stationary and with the truckbed lowered to load cars.");
} else {
cargo.pop().addPosition(5, 5);
}
}
@Override
public void move() {
if (CargoTruck.getAngle() == 0);{
super.move();
for (int i=0; i<cargo.size(); i++) {
cargo.get(i).setPosition(getX(), getY());
}
}
}
@Override
protected double speedFactor() { return enginePower * 0.01;}
}