Created with <3 with dartpad.dev.
Last active
October 11, 2023 00:26
-
-
Save ElanDeyan/a0557faea05de9a35e72db846e89fa64 to your computer and use it in GitHub Desktop.
elegant-lantern-8613
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
import 'dart:math' show Random; | |
void main() { | |
print('Testing'); | |
const age = 59; | |
print('Age: $age. Class: ${classifyAthlet(age)}.'); | |
print('Random tests'); | |
for(var i = 0; i < 10; i++) { | |
final randomAge = Random().nextInt(120); | |
print('Age: $randomAge. Class: ${classifyAthlet(randomAge)}'); | |
} | |
} | |
AthletCategory classifyAthlet(int age) { | |
if (_validateAge(age)) { | |
return switch (age) { | |
< 15 => Undefined(), | |
>= 15 && <= 17 => Base(), | |
18 || 19 => Under20(), | |
>= 20 && <= 34 => Profesional(), | |
<= 35 => Master(), | |
_ => Undefined() | |
}; | |
} else { | |
throw ArgumentError.value(age, null, 'Invalid age!'); | |
} | |
} | |
bool _validateAge(int age) { | |
return age > 0 || age <= 120; | |
} | |
sealed class AthletCategory {} | |
final class Base extends AthletCategory { | |
Base(); | |
@override | |
String toString() => 'Base'; | |
} | |
final class Under20 extends AthletCategory { | |
Under20(); | |
@override | |
String toString() => 'Under20'; | |
} | |
final class Profesional extends AthletCategory { | |
Profesional(); | |
@override | |
String toString() => 'Profesional'; | |
} | |
final class Master extends AthletCategory { | |
Master(); | |
@override | |
String toString() => 'Master'; | |
} | |
final class Undefined extends AthletCategory { | |
Undefined(); | |
@override | |
String toString() => 'Undefined'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment