Created with <3 with dartpad.dev.
Last active
April 4, 2023 03:30
-
-
Save topex-psy/830d8a7490e6e04650559b15fc70fa7a to your computer and use it in GitHub Desktop.
Dart 5. Enum & Switch Case
This file contains 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
void main() { | |
var spacecraft = Spacecraft("Roket 101", type: SpacecraftType.lander, launched: DateTime.tryParse("2022-01-02")); | |
spacecraft.launchDate = "2021-01-02"; | |
spacecraft.describe(); | |
} | |
enum SpacecraftType { | |
flyby, | |
orbitter, | |
lander, | |
commNav | |
} | |
class Spacecraft { | |
// properties | |
String name; | |
DateTime? launched; | |
SpacecraftType type; | |
// getter | |
int? get launchYear => launched?.year; | |
String get spacecraftType { | |
switch (type) { | |
case SpacecraftType.flyby: return "Flyby"; | |
case SpacecraftType.orbitter: return "Orbitter"; | |
default: return "Unknown"; | |
} | |
} | |
// setter | |
set launchDate(String date) { | |
launched = DateTime.tryParse(date); | |
} | |
// constructor | |
Spacecraft(this.name, {required this.type, this.launched}) { | |
// di sini inisialisasi | |
} | |
//method | |
void describe() { | |
print('$spacecraftType spacecraft "$name" ${launched == null | |
? "belum meroket" | |
: "sudah meroket pada $launchYear"}'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment