Skip to content

Solution for Enums 01 #85

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 5 commits into
base: exercises/enums/01
Choose a base branch
from
Open
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: 16 additions & 0 deletions Engine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public enum Engine {
DIESEL("Diesel"),
PETROL("Benzin"),
GAS("Gas"),
ELECTRO("Elektro");

private final String description;

Engine(String description) {
this.description = description;
}

public String getDescription() {
return description;
}
}
11 changes: 7 additions & 4 deletions Exercise.java
Original file line number Diff line number Diff line change
@@ -3,10 +3,13 @@ public class Exercise {
public static void main(String[] args) {
System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());

Vehicle vehicle = new Vehicle("Porsche", "911");
Vehicle vehicle1 = new Vehicle("Porsche", "911");
Vehicle vehicle2 = new Vehicle("MAN", "TGX");
Vehicle vehicle3 = new Vehicle("Opel", "Zafira Life");
Vehicle vehicle1 = new Vehicle("Porsche", "911", Engine.ELECTRO);
Vehicle vehicle2 = new Vehicle("MAN", "TGX", Engine.DIESEL);
Vehicle vehicle3 = new Vehicle("Opel", "Zafira Life", Engine.DIESEL);

System.out.println(vehicle1.toString());
System.out.println(vehicle2.toString());
System.out.println(vehicle3.toString());

System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());
}
22 changes: 14 additions & 8 deletions Vehicle.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
public class Vehicle {

private String make;
private String model;
private final String make;
private final String model;
private double speedInKmh;
private final Engine engine;
private static int numberOfVehicles;

public Vehicle(String make, String model) {
public Vehicle(String make, String model, Engine engine) {
this.make = make;
this.model = model;
Vehicle.numberOfVehicles++;
this.engine = engine;
}

public String getMake() {
@@ -19,12 +21,12 @@ public String getModel() {
return model;
}

public double getSpeedInKmh() {
return speedInKmh;
public Engine getEngine() {
return engine;
}

public static int getNumberOfVehicles() {
return Vehicle.numberOfVehicles;
public double getSpeedInKmh() {
return speedInKmh;
}

public void accelerate(int valueInKmh) {
@@ -38,6 +40,10 @@ public void brake(int valueInKmh) {
}

public String toString() {
return make + " " + model;
return getMake() + " " + getModel() + " (" + getEngine().getDescription() + ")";
}

public static int getNumberOfVehicles() {
return Vehicle.numberOfVehicles;
}
}