forked from oopd-gu-chalmers/lab1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.java
122 lines (111 loc) · 3.66 KB
/
Car.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.awt.*;
public abstract class Car implements Movable {
private final int nrDoors; // Number of doors on the car
private final double enginePower; // Engine power of the car
private double currentSpeed; // The current speed of the car // behövs pågrund att den behöver uppdateras i volvo, Saab
private Color color; // Color of the car
private final String modelName; // The car model name
private int direction; //
private int xPos;
private int yPos;
protected Point cordination = new Point(xPos, yPos);
public Car(int nrDoors, double enginePower, Color color, String modelName) {
this.nrDoors = nrDoors;
this.enginePower = enginePower;
this.color = color;
this.modelName = modelName;
this.direction = 1;
this.yPos = 0;
this.xPos = 0;
}
// Getters och setters
public int getxPos() {
return xPos;
}
public int getyPos() {
return yPos;
}
public String getModelname(){return modelName;}
public void setxPos(int value){xPos = value;}
public void setyPos(int value){yPos = value;}
public Point getPosition(){
return cordination;
}
public int getNrDoors() {return nrDoors;}
public void setCurrentSpeed(double amount) {currentSpeed = amount;}
public double getEnginePower() {
return enginePower;
}
public double getCurrentSpeed() {
return currentSpeed;
}
public Color getColor() {
return color;
}
public void setColor(Color clr) {
color = clr;
}
public void setDirection(int i) {direction = i; }
public int getDirection() { return direction; }
// Start- och stop -engine.
public void startEngine() { currentSpeed = 0.1; }
public void stopEngine() { currentSpeed = 0; }
// Generell move function för ALLA typer av fordon.
public void move() {
direction = this.getDirection();
switch (direction) {
case 0: // north
yPos += currentSpeed;
break;
case 1: // east
xPos += currentSpeed;
break;
case 2: // south
yPos -= currentSpeed;
break;
case 3: // west
xPos -= currentSpeed;
break;
default:
System.out.println("unknown direction");
break;
}
}
// Sväng vänster resp. höger.
public void turnleft() {
direction++;
if(direction > 4) {
direction = 0;
}
}
public void turnright() {
direction --;
if(direction < 0) {
direction = 4;
}
}
// Gasa / Öka farten
public void gas(double amount) {
if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) {
incrementSpeed(amount);
currentSpeed = Math.min(currentSpeed,getEnginePower());
}
else
System.out.println("amount is not ok");
this.move();
}
// Bromsa / Minska farten.
public void brake(double amount) {
if ((currentSpeed >= 0 && currentSpeed <= getEnginePower()) && (amount >= 0 && amount <= 1)) {
decrementSpeed(amount);
currentSpeed = Math.max(currentSpeed, 0);
}
else
throw new IllegalArgumentException("amount to high");
this.move();
}
// Abstrakta metoder som måste finnas i subklasserna. De skiljer från varandra. Här säger vi bara
// att de måste finnas.
protected void decrementSpeed(double amount) {};
protected void incrementSpeed(double amount) {};
}