@@ -6,87 +6,228 @@ 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 ();
19- }
20-
21- switch (mainOption ){
22- case 1 -> System .out .println ("Future custom collection" );
23- case 2 -> printNameSortingOptions ();
24- case 3 -> printIdSortingOptions ();
25- case 4 -> printPhoneSortingOptions ();
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 ();
2634 }
27-
28- // ПРОДОЛЖИТЬ
2935 }
3036
3137 @ Override
3238 public void printFillingDatabaseMenu () {
33- Printable .super .printFillingDatabaseMenu ();
34- String input = scanner .nextLine ().trim ();
35- try {
36- int fillOption = Integer .parseInt (input );
37-
38- switch (fillOption ){
39- case 1 -> fillManually ();
40- case 2 -> fillFromFile ();
41- case 3 -> fillRandom ();
42- case 0 -> System .out .println ("Возврат в главное меню..." );
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 ("Пожалуйста, введите цифру для выбора действия" );
4362 }
44- } catch (NumberFormatException e ){
45- System .out .println ("Пожалуйста, введите цифру для выбора действия или выхода в меню" );
4663 }
4764
4865 }
4966
5067 @ Override
5168 public void printSortingMenu () {
52- Printable .super .printSortingMenu ();
53- String input = scanner .nextLine ().trim ();
54- try {
55- int fillSortOption = Integer .parseInt (input );
56-
57- switch (fillSortOption ){
58- case 1 -> printDefaultOrder ();
59- case 2 -> printNameSortingOptions ();
60- case 3 -> printIdSortingOptions ();
61- case 4 -> printPhoneSortingOptions ();
62- case 5 -> System .out .println ("Возврат в главное меню..." );
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 ;
6377 }
64- } catch (NumberFormatException e ){
65- System .out .println ("Пожалуйста, введите цифру для выбора действия или выхода в меню" );
78+ try {
79+ int sortOption = Integer .parseInt (input );
80+
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 ("Пожалуйста, введите цифру для выбора действия." );
6694 }
6795 }
96+ }
6897
6998 @ Override
7099 public void printNameSortingOptions () {
71- Printable .super .printNameSortingOptions ();
72- String input = scanner .nextLine ().trim ();
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+ }
73126 }
127+ }
74128
75129 @ Override
76130 public void printIdSortingOptions () {
77- Printable .super .printIdSortingOptions ();
78- String input = scanner .nextLine ().trim ();
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+ }
79158 }
80159
81160 @ Override
82161 public void printPhoneSortingOptions () {
83- Printable .super .printPhoneSortingOptions ();
84- String input = scanner .nextLine ().trim ();
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 ("Клиенты отсортированы в алфавитном порядке имён" );
85193 }
194+
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+
86227 private void fillManually (){
87228 System .out .println ("Выбран способ ввода данных вручную" );
88229 System .out .println ("Введите данные..." );
89- String input = scanner .nextLine (). trim ();
230+ scanner .nextLine ();
90231 System .out .println ("Данные успешно сохранены" );
91232
92233 }
@@ -101,22 +242,44 @@ private void fillFromFile(){
101242
102243 private void fillRandom (){
103244 System .out .println ("Выбран способ ввода случайных данных" );
104- System .out .println ("Сколько записей создать? " );
105- String input = scanner .nextLine ().trim ();
106- try {
107- int count = Integer .parseInt (input );
108- if (count > 0 ) {
109- System .out .println ("Будет создано " + count + "Случайных записей" );
110- }
111- else {
112- System .out .println ("Должна быть как минимум одна запись!" );
113- }
114- } catch (NumberFormatException e ){
115- System .out .println ("Пожалуйста, введите количество записей" );
116- }
245+ int count = inputLengthOfValue ("Сколько записей создать? " , 10 );
246+ System .out .println ("Создано: " + count + " записей" );
117247 }
118248
119249 private void printDefaultOrder (){
120250 System .out .println ("Список клиентов" );
121251 }
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+ }
284+ }
122285}
0 commit comments