Mit dieser kleinen Java Anwendung habt ihr die Möglichkeit ein bisschen Auto zu fahren. Genauer, ihr dürft Gänge schalten. Das ist doch mal was.
With this small java application you have the opportunity to drive a car. Or to be more precise, you are allowed to shift gears. Great isn't it?
import java.util.Scanner;
public class MyCar {
public enum Movement{
REVERSE, NEUTRAL, FORWARD
}
private int actualGear;
private Movement movement;
public MyCar(){
actualGear = 0;
movement = Movement.NEUTRAL;
}
private void shiftUp(){
if(actualGear == -1){
actualGear++;
neutral();
} else if(actualGear >= 0 && actualGear < 5){
actualGear++;
movement = Movement.FORWARD;
}
}
private void shiftDown(){
if(actualGear > 1){
actualGear--;
} else if(actualGear == 1){
actualGear--;
neutral();
} else if(actualGear == 0){
reverse();
}
}
private void neutral(){
movement = Movement.NEUTRAL;
}
private void reverse(){
if(actualGear == 0){
actualGear = -1;
movement = Movement.REVERSE;
System.out.println("Look in the rear-view mirror!");
}
}
private void printStatus(){
System.out.println("Gear:"+actualGear+" Movement:"+movement);
}
public static void main(String[] args) {
MyCar driver = new MyCar();
Scanner sc = new Scanner(System.in);
while(true){
String choose = sc.nextLine();
switch(choose){
case "w":
driver.shiftUp();
break;
case "s":
driver.shiftDown();
break;
case "x":
sc.close();
return;
default:
continue;
}
driver.printStatus();
}
}
}