Skip to content

Instantly share code, notes, and snippets.

@jacobhq
Created October 23, 2024 13:13
Show Gist options
  • Save jacobhq/d54ddbd176ed6056d362f16e2c768f92 to your computer and use it in GitHub Desktop.
Save jacobhq/d54ddbd176ed6056d362f16e2c768f92 to your computer and use it in GitHub Desktop.
ElonsToyCar as done live in Java Tutorial at Glyn.
public class ElonsToyCar {
private int distance;
private int battery = 100;
public static ElonsToyCar buy() {
return new ElonsToyCar();
}
public String distanceDisplay() {
return String.format("Driven %d meters", distance);
}
public String batteryDisplay() {
if (this.battery == 0) {
return "Battery empty";
}
return String.format("Battery at %d%%", battery);
}
public void drive() {
if (this.battery != 0) {
this.distance += 20;
this.battery--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment