-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathMusicalExpression.cs
87 lines (75 loc) · 2 KB
/
MusicalExpression.cs
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
namespace InterpreterPattern;
internal abstract class MusicalExpression
{
public virtual void Interpret(PlayContext context)
{
if (string.IsNullOrEmpty(context.PlayText))
{
return;
}
var playKey = context.PlayText.Substring(0, 1);
context.PlayText = context.PlayText.Substring(2);
var playValue = context.PlayText.IndexOf(" ", StringComparison.Ordinal) > 0 ? Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" ", StringComparison.Ordinal))) : 0;
context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ", StringComparison.Ordinal) + 1);
Execute(playKey, playValue);
}
public abstract void Execute(string key, double value);
}
internal class MusicalNote : MusicalExpression
{
public override void Execute(string key, double value)
{
var note = key switch
{
"C" => "1",
"D" => "2",
"E" => "3",
"F" => "4",
"G" => "5",
"A" => "6",
"B" => "7",
_ => string.Empty
};
Console.Write($"{note} ");
}
}
internal class MusicalScale : MusicalExpression
{
public override void Execute(string key, double value)
{
var scale = string.Empty;
switch (value)
{
case 1:
scale = "低音";
break;
case 2:
scale = "中音";
break;
case 3:
scale = "高音";
break;
}
Console.Write(scale + " ");
}
}
internal class MusicalSpeed : MusicalExpression
{
public override void Execute(string key, double value)
{
string speed;
if (value < 500)
{
speed = "快速";
}
else if (value >= 1000)
{
speed = "快速";
}
else
{
speed = "中速";
}
Console.Write(speed + " ");
}
}