-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramework.java
More file actions
144 lines (110 loc) · 2.88 KB
/
Framework.java
File metadata and controls
144 lines (110 loc) · 2.88 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package framework;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Framework
{
private final Map<String, MenuItem> menuItems;
private static class Quit extends MenuItem
{
public Quit()
{
super("Выход", "q");
}
@Override
public String doAction()
{
System.exit(0);
return "";
}
}
private static class Help extends MenuItem
{
private static final String SEPARATION = "////////////////////////////////\n";
private final List<MenuItem> items;
private final String additionalDescription;
public Help(Map<String, MenuItem> menuItems, String additionalDescription)
{
super("Подсказка", "h");
items = new ArrayList<>();
this.additionalDescription = additionalDescription;
for (Map.Entry<String, MenuItem> i : menuItems.entrySet())
{
items.add(i.getValue());
}
}
@Override
public String doAction()
{
StringBuilder builder = new StringBuilder();
builder.append(SEPARATION);
builder.append(additionalDescription).append('\n');
builder.append(getAccessCommand()).append(' ').append(getDescription()).append('\n');
for (MenuItem i : items)
{
builder.append(i.toString()).append('\n');
}
builder.append(SEPARATION);
return builder.toString();
}
}
/**
* Очищает консоль, не работает в IDE
*/
private static class Clear extends MenuItem
{
Clear()
{
super("Очистить консоль", "cls");
}
@Override
public String doAction()
{
try
{
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
catch (IOException | InterruptedException e)
{
return "Не удалось очистить консоль";
}
return "Консоль очищена";
}
}
private void addStandardMenuItems()
{
MenuItem tem = new Quit();
menuItems.put(tem.getAccessCommand(), tem);
tem = new Clear();
menuItems.put(tem.getAccessCommand(), tem);
tem = new Help(menuItems, "Приветствие");
menuItems.put(tem.getAccessCommand(), tem);
}
public Framework(MenuItem... menuItems) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException
{
this.menuItems = new HashMap<>();
for (MenuItem i : menuItems)
{
this.menuItems.put(i.getAccessCommand(), i);
}
this.addStandardMenuItems();
}
public String manageInput(String accessCommand)
{
return menuItems.get(accessCommand).doAction();
}
public void run()
{
Scanner scanner = new Scanner(System.in);
System.out.println(this.manageInput("h"));
while (true)
{
String accessCommand = scanner.next();
System.out.println(this.manageInput(accessCommand));
}
}
}