Skip to content

Solution for Polymorphy 02 #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: exercises/polymorphy/02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ public int getSeats() {
public void doATurboBoost() {
speedInKmh *= 2;
System.out.println(
getMake() + " " + getModel() + " macht einen Turboboost und beschleunigt auf " + speedInKmh + " km/h");
getMake()
+ " "
+ getModel()
+ " macht einen Turboboost und beschleunigt auf "
+ speedInKmh
+ " km/h");
}

public String toString() {
return getMake() + " " + getModel() + " (" + getEngine().getDescription() + ", " + seats + " Sitzplaetze)";
return getMake()
+ " "
+ getModel()
+ " ("
+ getEngine().getDescription()
+ ", "
+ seats
+ " Sitzplaetze)";
}
}
7 changes: 6 additions & 1 deletion Exercise.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public class Exercise {

public static void main(String[] args) {
System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());
Rental rental = new Rental("Fahrzeugvermietung Mueller");

Car car1 = new Car("Porsche", "911", Engine.ELECTRO, 2);
Truck truck1 = new Truck("MAN", "TGX", Engine.DIESEL, 20);
Expand All @@ -17,5 +17,10 @@ public static void main(String[] args) {
truck1.transform();
car1.doATurboBoost();
truck1.transform();
rental.addVehicle(car1);
rental.addVehicle(truck1);
rental.addVehicle(car2);

System.out.println(rental.toString());
}
}
38 changes: 38 additions & 0 deletions Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayList;

public class Rental {

private final String name;
private final ArrayList<Vehicle> vehicles;

public Rental(String name) {
this.name = name;
vehicles = new ArrayList<>();
}

public String getName() {
return name;
}

public ArrayList<Vehicle> getVehicles() {
return vehicles;
}

public void addVehicle(Vehicle vehicle) {
vehicles.add(vehicle);
}

public void addAllVehicles(Vehicle... vehicles) {
for (Vehicle v : vehicles) {
this.vehicles.add(v);
}
}

public String toString() {
String result = name + "\n" + "Unsere Fahrzeuge: \n";
for (Vehicle vehicle : vehicles) {
result += vehicle.toString() + "\n";
}
return result;
}
}
3 changes: 2 additions & 1 deletion Truck.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public boolean isTransformed() {

public void transform() {
if (isTransformed) {
System.out.println(getMake() + " " + getModel() + " verwandelt sich in einen Lastwagen zurueck");
System.out.println(
getMake() + " " + getModel() + " verwandelt sich in einen Lastwagen zurueck");
} else {
System.out.println(getMake() + " " + getModel() + " verwandelt sich in einen Autobot");
}
Expand Down