Skip to content

Commit 728ec68

Browse files
committed
Убрал лишний код, оставил в методах по сортировке имён и телефонов только сортировку без меню. В сортировке по id оставил меню для сортировки по возрастанию и четным.
1 parent 9a3ca28 commit 728ec68

File tree

2 files changed

+67
-152
lines changed

2 files changed

+67
-152
lines changed

src/main/java/userInterface/MenuManager.java

Lines changed: 47 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ public class MenuManager implements Printable {
88
private final Scanner scanner = new Scanner(System.in);
99
private boolean running = true;
1010

11-
public void run(){
12-
while (running){
11+
public void run() {
12+
while (running) {
1313
try {
1414
printMainMenu();
1515
String input = scanner.nextLine();
16-
if (input.isEmpty()){
16+
if (input.isEmpty()) {
1717
System.out.println("Пожалуйста, выберите номер опции");
1818
continue;
1919
}
2020

2121
int mainOption = Integer.parseInt(input);
2222

23-
switch (mainOption){
23+
switch (mainOption) {
2424
case 1 -> printFillingDatabaseMenu();
2525
case 2 -> printSortingMenu();
2626
case 0 -> exitByChoice();
2727
default -> System.out.println("Неверный выбор. Пожалуйста, выберите 1, 2 или 0");
2828
}
29-
} catch (Exception e){
29+
}
30+
catch (Exception e) {
3031
System.out.println("Ошибка: " + e.getMessage());
3132
System.out.println("Пожалуйста, введите корректный номер опции.");
3233
}
@@ -36,17 +37,17 @@ public void run(){
3637
@Override
3738
public void printFillingDatabaseMenu() {
3839
boolean backToMain = false;
39-
while (!backToMain && running){
40+
while (!backToMain && running) {
4041
Printable.super.printFillingDatabaseMenu();
4142
String input = scanner.nextLine().trim();
42-
if (input.isEmpty()){
43+
if (input.isEmpty()) {
4344
System.out.println("Пожалуйста, введите номер опции.");
4445
continue;
4546
}
4647
try {
4748
int fillOption = Integer.parseInt(input);
4849

49-
switch (fillOption){
50+
switch (fillOption) {
5051
case 1 -> fillManually();
5152
case 2 -> fillFromFile();
5253
case 3 -> fillRandom();
@@ -56,7 +57,8 @@ public void printFillingDatabaseMenu() {
5657
}
5758
default -> System.out.println("Неверный выбор. Пожалуйста, выберите от 0 до 3");
5859
}
59-
} catch (NumberFormatException e){
60+
}
61+
catch (NumberFormatException e) {
6062
System.out.println("Пожалуйста, введите цифру для выбора действия");
6163
}
6264
}
@@ -66,18 +68,18 @@ public void printFillingDatabaseMenu() {
6668
@Override
6769
public void printSortingMenu() {
6870
boolean backToMain = false;
69-
while (!backToMain && running){
71+
while (!backToMain && running) {
7072
Printable.super.printSortingMenu();
7173
String input = scanner.nextLine().trim();
7274

73-
if (input.isEmpty()){
75+
if (input.isEmpty()) {
7476
System.out.println("Пожалуйста, введите номер опции.");
7577
continue;
7678
}
7779
try {
7880
int sortOption = Integer.parseInt(input);
7981

80-
switch (sortOption){
82+
switch (sortOption) {
8183
case 1 -> printDefaultOrder();
8284
case 2 -> printNameSortingOptions();
8385
case 3 -> printIdSortingOptions();
@@ -88,185 +90,114 @@ public void printSortingMenu() {
8890
}
8991
default -> System.out.println("Неверный выбор. Пожалуйста, выберите от 0 до 4");
9092
}
91-
} catch (NumberFormatException e){
92-
System.out.println("Пожалуйста, введите цифру для выбора действия.");
93-
}
94-
}
95-
}
96-
97-
@Override
98-
public void printNameSortingOptions() {
99-
boolean backToMain = false;
100-
while (!backToMain && running){
101-
Printable.super.printNameSortingOptions();
102-
String input = scanner.nextLine().trim();
103-
104-
if (input.isEmpty()){
105-
System.out.println("Пожалуйста, введите номер опции.");
106-
continue;
10793
}
108-
109-
try {
110-
int nameSortOption = Integer.parseInt(input);
111-
112-
switch (nameSortOption){
113-
case 1 -> sortByNameAlphabet();
114-
case 2 -> sortByNameLength();
115-
case 3 -> sortByAmountVowelsOfName();
116-
case 0 -> {
117-
System.out.println("Назад...");
118-
backToMain = true;
119-
}
120-
default -> System.out.println("Неверный выбор. Пожалуйста, выберите от 0 до 3");
121-
}
122-
} catch (NumberFormatException e){
94+
catch (NumberFormatException e) {
12395
System.out.println("Пожалуйста, введите цифру для выбора действия.");
96+
}
12497
}
12598
}
126-
}
12799

128100
@Override
129101
public void printIdSortingOptions() {
130102
boolean backToMain = false;
131-
while (!backToMain && running){
103+
while (!backToMain && running) {
132104
Printable.super.printIdSortingOptions();
133105
String input = scanner.nextLine().trim();
134106

135-
if (input.isEmpty()){
107+
if (input.isEmpty()) {
136108
System.out.println("Пожалуйста, введите номер опции.");
137109
continue;
138110
}
139111

140112
try {
141113
int idSortOption = Integer.parseInt(input);
142114

143-
switch (idSortOption){
115+
switch (idSortOption) {
144116
case 1 -> sortIDByAscending();
145-
case 2 -> sortIDByDescending();
146-
case 3 -> sortIDByAmountOfNumbers();
117+
case 2 -> sortIDbyEven();
147118
case 0 -> {
148119
System.out.println("Назад...");
149120
backToMain = true;
150121
}
151122
default -> System.out.println("Неверный выбор. Пожалуйста, выберите от 0 до 3");
152123
}
153-
} catch (NumberFormatException e){
124+
}
125+
catch (NumberFormatException e) {
154126
System.out.println("Пожалуйста, введите цифру для выбора действия.");
155127
}
156128
}
157129
}
158130

159131
@Override
160-
public void printPhoneSortingOptions() {
132+
public void printNameSortingOptions() {
161133
boolean backToMain = false;
162-
while (!backToMain && running){
163-
Printable.super.printPhoneSortingOptions();
164-
String input = scanner.nextLine().trim();
165-
166-
if (input.isEmpty()){
167-
System.out.println("Пожалуйста, введите номер опции.");
168-
continue;
169-
}
170-
171-
try {
172-
int phoneSortOption = Integer.parseInt(input);
173-
174-
switch (phoneSortOption){
175-
case 1 -> sortPhoneByNormalizedFormat();
176-
case 2 -> sortPhoneByCountryCode();
177-
case 3 -> sortPhoneByOperatorCode();
178-
case 0 -> {
179-
System.out.println("Назад...");
180-
backToMain = true;
181-
}
182-
default -> System.out.println("Неверный выбор. Пожалуйста, выберите от 0 до 3");
183-
}
184-
} catch (NumberFormatException e){
185-
System.out.println("Пожалуйста, введите цифру для выбора действия.");
186-
}
134+
while (!backToMain && running) {
135+
Printable.super.printNameSortingOptions();
136+
backToMain = true;
187137
}
188138
}
189139

190-
private void sortByNameAlphabet(){
191-
System.out.println("Клиенты отсортированы в алфавитном порядке имён");
192-
}
193-
194-
private void sortByNameLength(){
195-
System.out.println("Клиенты отсортированы по длине имён");
196-
}
197-
198-
private void sortByAmountVowelsOfName(){
199-
System.out.println("Клиенты отсортированы по количеству гласных в имени");
140+
@Override
141+
public void printPhoneSortingOptions() {
142+
boolean backToMain = false;
143+
while (!backToMain && running) {
144+
Printable.super.printPhoneSortingOptions();
145+
backToMain = true;
146+
}
200147
}
201148

202-
private void sortIDByAscending(){
149+
private void sortIDByAscending() {
203150
System.out.println("Клиенты отсортированы по возрастанию ID");
204151
}
205152

206-
private void sortIDByDescending(){
207-
System.out.println("Клиенты отсортированы по убыванию ID");
208-
}
209-
210-
private void sortIDByAmountOfNumbers(){
211-
System.out.println("Клиенты отсортированы по количеству цифр в ID");
212-
}
213-
214-
private void sortPhoneByNormalizedFormat(){
215-
System.out.println("Клиенты отсортированы по международному формату телефонного номера");
216-
}
217-
218-
private void sortPhoneByCountryCode(){
219-
System.out.println("Клиенты отсортированы по коду страны телефонного номера");
220-
}
221-
222-
private void sortPhoneByOperatorCode(){
223-
System.out.println("Клиенты отсортированы по коду оператора телефонного номера");
153+
private void sortIDbyEven() {
154+
System.out.println("Клиенты отсортированы по четным ID");
224155
}
225156

226-
private void fillManually(){
157+
private void fillManually() {
227158
System.out.println("Выбран способ ввода данных вручную");
228159
System.out.println("Введите данные...");
229160
System.out.println("Данные успешно сохранены");
230161

231162
}
232163

233-
private void fillFromFile(){
164+
private void fillFromFile() {
234165
System.out.println("Выбран способ ввода данных из файла\n");
235166
System.out.println("Введите путь к файлу: ");
236167
String filePath = scanner.nextLine().trim();
237168
System.out.println("Загрузка из файла: " + filePath);
238169

239170
}
240171

241-
private void fillRandom(){
172+
private void fillRandom() {
242173
System.out.println("Выбран способ ввода случайных данных");
243174
int count = inputLengthOfValue("Сколько записей создать? ", 10);
244175
System.out.println("Создано: " + count + " записей");
245176
}
246177

247-
private void printDefaultOrder(){
178+
private void printDefaultOrder() {
248179
System.out.println("Список клиентов");
249180
}
250181

251-
private void exitByChoice(){
182+
private void exitByChoice() {
252183
System.out.println("Выход из программы...");
253184
running = false;
254185
scanner.close();
255186
}
256187

257-
private int inputLengthOfValue(String prompt, int defaultValue){
258-
while (true){
188+
private int inputLengthOfValue(String prompt, int defaultValue) {
189+
while (true) {
259190
System.out.println(prompt + "(по умолчанию: " + defaultValue + "): ");
260191
String input = scanner.nextLine().trim();
261192

262-
if (input.isEmpty()){
193+
if (input.isEmpty()) {
263194
System.out.println("Значение по умолчанию: " + defaultValue);
264195
return defaultValue;
265196
}
266197

267198
try {
268199
int value = Integer.parseInt(input);
269-
if (value > 0){
200+
if (value > 0) {
270201
return value;
271202
}
272203
else if (value == 0) {
@@ -276,7 +207,8 @@ else if (value == 0) {
276207
else {
277208
System.out.println("Число должно быть положительным! Попробуйте еще раз.");
278209
}
279-
} catch (NumberFormatException e){
210+
}
211+
catch (NumberFormatException e) {
280212
System.out.println("Пожалуйста, введите целое число! Попробуйте еще раз");
281213
}
282214
}

src/main/java/userInterface/Printable.java

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,45 @@ default void printMainMenu(){
1111
}
1212

1313
default void printFillingDatabaseMenu(){
14-
System.out.println(
15-
"Заполнить базу данных: \n" +
16-
"1) Ручной ввод\n" +
17-
"2) Файлом\n" +
18-
"3) Случайный набор" +
19-
"0) Возврат в главное меню"
14+
System.out.println("""
15+
Заполнить базу данных:
16+
1) Ручной ввод
17+
2) Файлом
18+
3) Случайный набор\
19+
0) Возврат в главное меню"""
2020
);
2121
}
2222

2323
default void printSortingMenu(){
24-
System.out.println(
25-
"Выберите вариант:\n" +
26-
"1) Отображать клиентов в порядке по умолчанию\n" +
27-
"2) Сортировать по имени\n" +
28-
"3) Сортировать по ID\n" +
29-
"4) Сортировать по номеру телефона" +
30-
"0) Возврат в главное меню"
24+
System.out.println("""
25+
Выберите вариант:
26+
1) Отображать клиентов в порядке по умолчанию
27+
2) Сортировать по имени
28+
3) Сортировать по ID
29+
4) Сортировать по номеру телефона\
30+
0) Возврат в главное меню"""
3131
);
3232
}
3333

3434
default void printNameSortingOptions(){
3535
System.out.println(
36-
"Сортировать клиентов:\n" +
37-
"1) По алфавитному порядку\n" +
38-
"2) По длине имени\n" +
39-
"3) По количеству гласных в имени\n" +
40-
"4) По количеству согласных в имени\n" +
41-
"5) По имени в кодировке unicode\n" +
42-
"0) Назад"
36+
"Клиенты отсортированы по умолчанию\n"
4337
);
4438
}
4539

4640
default void printIdSortingOptions(){
47-
System.out.println(
48-
"Сортировать клиентов:\n" +
49-
"1) По возрастанию ID \n" +
50-
"2) По уменьшению ID \n" +
51-
"3) По количеству цифр в ID\n" +
52-
"4) По четным ID\n" + // ЭТО ДОП ЗАДАНИЕ
53-
"5) По нечетным ID\n" + // ЭТО ДОП ЗАДАНИЕ
54-
"6) По кодировке unicode\n" +
55-
"0) Назад"
41+
System.out.println("""
42+
Сортировать клиентов:
43+
1) По возрастанию ID
44+
4) По четным ID
45+
0) Назад"""
5646

5747
);
5848
}
5949

6050
default void printPhoneSortingOptions(){
6151
System.out.println(
62-
"Сортировать клиентов:\n" +
63-
"1) По номеру телефона в международном формате\n" + // приведенный к международному формату
64-
"2) По городскому коду\n" +
65-
"3) По коду оператора\n" +
66-
"4) По последним цифрам в номере телефона\n" +
67-
"5) По длине номера телефона\n" +
68-
"0) Назад"
69-
52+
"Клиенты отсортированы по умолчанию\n"
7053
);
7154
}
7255
}

0 commit comments

Comments
 (0)