forked from klmntv27/ulearn-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.1.java
More file actions
137 lines (112 loc) · 4.47 KB
/
6.1.java
File metadata and controls
137 lines (112 loc) · 4.47 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
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CustomerStorage customerStorage = new CustomerStorage();
System.out.println("Для взаимодействия с консольным приложением используйте следующий набор команд ");
System.out.println("Команды:\n" +
"add - добавить клиента\n"+
"list - вывести список всех клиентов\n"+
"remove - удалить клиента\n"+
"count - подсчитать всех клиентов\n"+
"help - выводит пример команды\n"+
"exit - завершает работу приложения");
String command = scanner.nextLine();
while(!command.equals("exit")){
try {
if (command.substring(0, 3).equals("add")) {
customerStorage.addCustomer(command.substring(4));
}
else if (command.substring(0, 4).equals("list")) {
System.out.println(customerStorage.listCustomers());
}
else if (command.substring(0, 4).equals("help")) {
String helpCom = command.split(" ")[1];
if (helpCom.equals("add") || helpCom.equals("remove")) {
System.out.println("Пример использования: [name_command] [name] [surname] [email] [phone]");
}
}
else if (command.substring(0, 5).equals("count")) {
System.out.println(customerStorage.getCount());
}
else if (command.substring(0, 6).equals("remove")) {
customerStorage.removeCustomer(command.split(" ")[1] + " " + command.split(" ")[2]);
}
}catch (Exception t){
System.out.println("Введите корректные значения!");
}
command = scanner.nextLine();
}
}
}
public class Customer {
private final String name;
private final String phone;
private final String email;
private final String surname;
public String getSurname(){
return surname;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getName() {
return name;
}
public Customer(String name,String surname,String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
this.surname = surname;
}
public String toString(){
return String.format("%s - %s - %s - %s",name,surname,email,phone);
}
}
public class CustomerStorage {
private ArrayList<Customer> customers = new ArrayList<>();
public void addCustomer(String input){
try{
String[] values = input.split(" ");
if (values.length!=4 || !isCorrectPhone(values[3]) || !isCorrectEmail(values[2])) throw new IllegalArgumentException();
customers.add(new Customer(values[0],values[1],values[3],values[2]));
}catch (Exception e){
throw new IllegalArgumentException();
}
}
public String listCustomers(){
StringBuilder text = new StringBuilder();
customers.stream().forEach(x->text.append(x.toString()+"\n"));
return text.toString();
}
public void removeCustomer(String input){
String[] values = input.split(" ");
for(Customer customer : customers){
if (customer.getName().equals(values[0]) & customer.getSurname().equals(values[1])){
customers.remove(customer);
return;
}
}
throw new IllegalArgumentException();
}
public Customer getCustomer(String input){
String[] values = input.split(" ");
for(Customer customer : customers){
if (customer.getName().equals(values[0]) & customer.getSurname().equals(values[1])){
return customer;
}
}
throw new IllegalArgumentException();
}
public int getCount(){
return customers.size();
}
public boolean isCorrectPhone(String phone) {
return phone.matches("((\\+7)?||8?)+\\d{10}");
}
public boolean isCorrectEmail(String email){
return email.matches("^[A-Za-z0-9+_.-]+@(.+)$");
}
}