-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLesson5_1.java
More file actions
91 lines (68 loc) · 2.93 KB
/
MainLesson5_1.java
File metadata and controls
91 lines (68 loc) · 2.93 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
package ru.MylearnCh1J1L1;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
public class MainLesson5_1 {
public static void main(String[] args) {
Map<String, ArrayList<String>> phoneBook = new HashMap<>();
Scanner sc = new Scanner(System.in, "cp866");
boolean mainCycle = true;
while (mainCycle) {
System.out.println();
System.out.print("Введите команду:\n\t1 - Показать все записи в телефонной книге\n\t2 - Добавить номер в книгу\n\t0 - Выход: ");
String decisision = sc.nextLine();
switch (decisision) {
case "1":
showPhoneBook(phoneBook);
break;
case "2":
addContact(phoneBook, sc);
break;
case "0":
mainCycle = false;
System.out.println("Выход...");
break;
default:
System.out.println("Такой команды нет!");
}
}
sc.close();
}
public static void addContact(Map<String, ArrayList<String>> map, Scanner scanner) {
int index = 1;
Object[] names = map.keySet().toArray();
System.out.println();
System.out.println("Выберите, кому добавить номер:");
for (Object el: names) {
System.out.println("\t" + index + ". " + el);
index++;
}
System.out.print("\t0. Добавить новый контакт\n Ваш выбор: ");
int decision = scanner.nextInt();
scanner.nextLine();
if (decision <= names.length && decision > 0) {
System.out.print("Введите номер телефона: ");
String phoneNumber = scanner.nextLine();
map.get(names[decision - 1]).add(phoneNumber);
}
else if (decision == 0) {
System.out.print("Введите ФИО нового контакта: ");
String name = scanner.nextLine();
System.out.print("Введите номер телефона: ");
String phoneNumber = scanner.nextLine();
ArrayList<String> numbers = new ArrayList<>();
numbers.add(phoneNumber);
map.put(name, numbers);
}
else System.out.println("Такого выбора нет!");
}
public static void showPhoneBook(Map<String, ArrayList<String>> map) {
System.out.println();
for (var el: map.entrySet()) {
System.out.println(el.getKey() + ":");
for (String inner: el.getValue()) System.out.println("\t" + inner);
System.out.println();
}
}
}