@@ -6,25 +6,280 @@ public class MenuManager implements Printable {
66
77
88 private final Scanner scanner = new Scanner (System .in );
9+ private boolean running = true ;
910
1011 public void run (){
12+ while (running ){
13+ try {
14+ printMainMenu ();
15+ String input = scanner .nextLine ();
16+ if (input .isEmpty ()){
17+ System .out .println ("Пожалуйста, выберите номер опции" );
18+ continue ;
19+ }
1120
12- printMainMenu ();
13- String input = scanner .nextLine ();
14- int mainOption = Integer .parseInt (input );
21+ int mainOption = Integer .parseInt (input );
1522
16- switch (mainOption ){
17- case 1 -> printFillingDatabaseMenu ();
18- case 2 -> printSortingMenu ();
23+ switch (mainOption ){
24+ case 1 -> printFillingDatabaseMenu ();
25+ case 2 -> printSortingMenu ();
26+ case 3 -> exitByChoice ();
27+ default -> System .out .println ("Неверный выбор. Пожалуйста, выберите 1, 2 или 3" );
28+ }
29+ } catch (Exception e ){
30+ System .out .println ("Ошибка: " + e .getMessage ());
31+ System .out .println ("Пожалуйста, введите корректный номер опции." );
32+ }
33+ scanner .close ();
1934 }
35+ }
36+
37+ @ Override
38+ public void printFillingDatabaseMenu () {
39+ boolean backToMain = false ;
40+ while (!backToMain && running ){
41+ Printable .super .printFillingDatabaseMenu ();
42+ String input = scanner .nextLine ().trim ();
43+ if (input .isEmpty ()){
44+ System .out .println ("Пожалуйста, введите номер опции." );
45+ continue ;
46+ }
47+ try {
48+ int fillOption = Integer .parseInt (input );
49+
50+ switch (fillOption ){
51+ case 1 -> fillManually ();
52+ case 2 -> fillFromFile ();
53+ case 3 -> fillRandom ();
54+ case 0 -> {
55+ System .out .println ("Возврат в главное меню..." );
56+ backToMain = true ;
57+ }
58+ default -> System .out .println ("Неверный выбор. Пожалуйста, выберите от 0 до 3" );
59+ }
60+ } catch (NumberFormatException e ){
61+ System .out .println ("Пожалуйста, введите цифру для выбора действия" );
62+ }
63+ }
64+
65+ }
66+
67+ @ Override
68+ public void printSortingMenu () {
69+ boolean backToMain = false ;
70+ while (!backToMain && running ){
71+ Printable .super .printSortingMenu ();
72+ String input = scanner .nextLine ().trim ();
73+
74+ if (input .isEmpty ()){
75+ System .out .println ("Пожалуйста, введите номер опции." );
76+ continue ;
77+ }
78+ try {
79+ int sortOption = Integer .parseInt (input );
2080
21- switch (mainOption ){
22- case 1 -> System .out .println ("Future custom collection" );
23- case 2 -> printNameSortingOptions ();
24- case 3 -> printIdSortingOptions ();
25- case 4 -> printPhoneSortingOptions ();
81+ switch (sortOption ){
82+ case 1 -> printDefaultOrder ();
83+ case 2 -> printNameSortingOptions ();
84+ case 3 -> printIdSortingOptions ();
85+ case 4 -> printPhoneSortingOptions ();
86+ case 5 -> {
87+ System .out .println ("Возврат в главное меню..." );
88+ backToMain = true ;
89+ }
90+ default -> System .out .println ("Неверный выбор. Пожалуйста, выберите от 1 до 5" );
91+ }
92+ } catch (NumberFormatException e ){
93+ System .out .println ("Пожалуйста, введите цифру для выбора действия." );
2694 }
95+ }
96+ }
97+
98+ @ Override
99+ public void printNameSortingOptions () {
100+ boolean backToMain = false ;
101+ while (!backToMain && running ){
102+ Printable .super .printNameSortingOptions ();
103+ String input = scanner .nextLine ().trim ();
104+
105+ if (input .isEmpty ()){
106+ System .out .println ("Пожалуйста, введите номер опции." );
107+ continue ;
108+ }
109+
110+ try {
111+ int nameSortOption = Integer .parseInt (input );
112+
113+ switch (nameSortOption ){
114+ case 1 -> sortByNameAlphabet ();
115+ case 2 -> sortByNameLength ();
116+ case 3 -> sortByAmountVowelsOfName ();
117+ case 4 -> {
118+ System .out .println ("Возврат в главное меню..." );
119+ backToMain = true ;
120+ }
121+ default -> System .out .println ("Неверный выбор. Пожалуйста, выберите от 1 до 5" );
122+ }
123+ } catch (NumberFormatException e ){
124+ System .out .println ("Пожалуйста, введите цифру для выбора действия." );
125+ }
126+ }
127+ }
128+
129+ @ Override
130+ public void printIdSortingOptions () {
131+ boolean backToMain = false ;
132+ while (!backToMain && running ){
133+ Printable .super .printIdSortingOptions ();
134+ String input = scanner .nextLine ().trim ();
135+
136+ if (input .isEmpty ()){
137+ System .out .println ("Пожалуйста, введите номер опции." );
138+ continue ;
139+ }
140+
141+ try {
142+ int idSortOption = Integer .parseInt (input );
143+
144+ switch (idSortOption ){
145+ case 1 -> sortIDByAscending ();
146+ case 2 -> sortIDByDescending ();
147+ case 3 -> sortIDByAmountOfNumbers ();
148+ case 4 -> {
149+ System .out .println ("Возврат в главное меню..." );
150+ backToMain = true ;
151+ }
152+ default -> System .out .println ("Неверный выбор. Пожалуйста, выберите от 1 до 5" );
153+ }
154+ } catch (NumberFormatException e ){
155+ System .out .println ("Пожалуйста, введите цифру для выбора действия." );
156+ }
157+ }
158+ }
159+
160+ @ Override
161+ public void printPhoneSortingOptions () {
162+ boolean backToMain = false ;
163+ while (!backToMain && running ){
164+ Printable .super .printPhoneSortingOptions ();
165+ String input = scanner .nextLine ().trim ();
166+
167+ if (input .isEmpty ()){
168+ System .out .println ("Пожалуйста, введите номер опции." );
169+ continue ;
170+ }
171+
172+ try {
173+ int phoneSortOption = Integer .parseInt (input );
174+
175+ switch (phoneSortOption ){
176+ case 1 -> sortPhoneByNormalizedFormat ();
177+ case 2 -> sortPhoneByCountryCode ();
178+ case 3 -> sortPhoneByOperatorCode ();
179+ case 4 -> {
180+ System .out .println ("Возврат в главное меню..." );
181+ backToMain = true ;
182+ }
183+ default -> System .out .println ("Неверный выбор. Пожалуйста, выберите от 1 до 5" );
184+ }
185+ } catch (NumberFormatException e ){
186+ System .out .println ("Пожалуйста, введите цифру для выбора действия." );
187+ }
188+ }
189+ }
190+
191+ private void sortByNameAlphabet (){
192+ System .out .println ("Клиенты отсортированы в алфавитном порядке имён" );
193+ }
27194
28- // ПРОДОЛЖИТЬ
195+ private void sortByNameLength (){
196+ System .out .println ("Клиенты отсортированы по длине имён" );
197+ }
198+
199+ private void sortByAmountVowelsOfName (){
200+ System .out .println ("Клиенты отсортированы по количеству гласных в имени" );
201+ }
202+
203+ private void sortIDByAscending (){
204+ System .out .println ("Клиенты отсортированы по возрастанию ID" );
205+ }
206+
207+ private void sortIDByDescending (){
208+ System .out .println ("Клиенты отсортированы по убыванию ID" );
209+ }
210+
211+ private void sortIDByAmountOfNumbers (){
212+ System .out .println ("Клиенты отсортированы по количеству цифр в ID" );
213+ }
214+
215+ private void sortPhoneByNormalizedFormat (){
216+ System .out .println ("Клиенты отсортированы по международному формату телефонного номера" );
217+ }
218+
219+ private void sortPhoneByCountryCode (){
220+ System .out .println ("Клиенты отсортированы по коду страны телефонного номера" );
221+ }
222+
223+ private void sortPhoneByOperatorCode (){
224+ System .out .println ("Клиенты отсортированы по коду оператора телефонного номера" );
225+ }
226+
227+ private void fillManually (){
228+ System .out .println ("Выбран способ ввода данных вручную" );
229+ System .out .println ("Введите данные..." );
230+ scanner .nextLine ();
231+ System .out .println ("Данные успешно сохранены" );
232+
233+ }
234+
235+ private void fillFromFile (){
236+ System .out .println ("Выбран способ ввода данных из файла\n " );
237+ System .out .println ("Введите путь к файлу: " );
238+ String filePath = scanner .nextLine ().trim ();
239+ System .out .println ("Загрузка из файла: " + filePath );
240+
241+ }
242+
243+ private void fillRandom (){
244+ System .out .println ("Выбран способ ввода случайных данных" );
245+ int count = inputLengthOfValue ("Сколько записей создать? " , 10 );
246+ System .out .println ("Создано: " + count + " записей" );
247+ }
248+
249+ private void printDefaultOrder (){
250+ System .out .println ("Список клиентов" );
251+ }
252+
253+ private void exitByChoice (){
254+ System .out .println ("Выход из программы..." );
255+ running = false ;
256+ }
257+
258+ private int inputLengthOfValue (String prompt , int defaultValue ){
259+ while (true ){
260+ System .out .println (prompt + "(по умолчанию: " + defaultValue + "): " );
261+ String input = scanner .nextLine ().trim ();
262+
263+ if (input .isEmpty ()){
264+ System .out .println ("Значение по умолчанию: " + defaultValue );
265+ return defaultValue ;
266+ }
267+
268+ try {
269+ int value = Integer .parseInt (input );
270+ if (value > 0 ){
271+ return value ;
272+ }
273+ else if (value == 0 ) {
274+ System .out .println ("Используется значение по умолчанию: " + defaultValue );
275+ return defaultValue ;
276+ }
277+ else {
278+ System .out .println ("Число должно быть положительным! Попробуйте еще раз." );
279+ }
280+ } catch (NumberFormatException e ){
281+ System .out .println ("Пожалуйста, введите целое число! Попробуйте еще раз" );
282+ }
283+ }
29284 }
30285}
0 commit comments