Created
October 23, 2024 13:13
-
-
Save jacobhq/d54ddbd176ed6056d362f16e2c768f92 to your computer and use it in GitHub Desktop.
ElonsToyCar as done live in Java Tutorial at Glyn.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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