-
Notifications
You must be signed in to change notification settings - Fork 2
/
fbz_psycho_2.dart
89 lines (67 loc) · 1.85 KB
/
fbz_psycho_2.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/// Replacing if-else conditions with enum
void main(List<String> args) {
for (var i = 0; i < 31; i++) {
OutputType.selectBy(number: i).output(number: i);
}
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
enum OutputType {
fizzbuzz(FizzBuzzOutput()),
fizz(FizzOutput()),
buzz(BuzzOutput()),
num(NumberOutput());
const OutputType(this.output);
final Output output;
static OutputType selectBy({required int number}) {
for (var output in OutputType.values) {
if (number % output.output.divisor == 0) {
return output;
}
}
return num;
}
}
abstract interface class Output {
static const fbOutput = 'FizzBuzz,';
static const fOutput = 'Fizz,';
static const bOutput = 'Buzz,';
static const fbDivisor = 15;
static const fDivisor = 3;
static const bDivisor = 5;
static const nDivisor = 1;
int get divisor;
void call({required int number});
}
class FizzBuzzOutput implements Output {
const FizzBuzzOutput();
final int _divisor = Output.fbDivisor;
@override
int get divisor => _divisor;
@override
void call({required int number}) => print('${Output.fbOutput}');
}
class FizzOutput implements Output {
const FizzOutput();
final int _divisor = Output.fDivisor;
@override
int get divisor => _divisor;
@override
void call({required int number}) => print('${Output.fOutput}');
}
class BuzzOutput implements Output {
const BuzzOutput();
final int _divisor = Output.bDivisor;
@override
int get divisor => _divisor;
@override
void call({required int number}) => print('${Output.bOutput}');
}
class NumberOutput implements Output {
const NumberOutput();
final int _divisor = Output.nDivisor;
@override
int get divisor => _divisor;
@override
void call({required int number}) => print('${number},');
}