-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerStorage.java
More file actions
45 lines (38 loc) · 1.56 KB
/
CustomerStorage.java
File metadata and controls
45 lines (38 loc) · 1.56 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
import MyExeptions.MyExeptions.*;
import java.util.HashMap;
public class CustomerStorage {
private HashMap<String, Customer> storage;
public CustomerStorage() {
storage = new HashMap<>();
}
public void addCustomer(String data) throws RuntimeException {
String[] components = data.split("\\s+");
if (components.length != 4) {
throw new IllegalArgumentException("Invalid argument format. Correct format: " +
"\nВасилий Петров vasily.petrov@gmail.com +79215637722");
}
if (!components[0].toLowerCase().matches("^[а-я]+$") && !components[1].toLowerCase().matches("^[а-я]+$")) {
throw new NotValidNameException();
}
if (!components[2].toLowerCase().matches(".+@.+\\..+")) {
throw new NotValidEmailException();
}
if (!components[3].matches("^\\+7\\d{10}$")) {
throw new NotValidPhoneException();
}
String name = components[0] + " " + components[1];
storage.put(name, new Customer(name, components[3], components[2]));
}
public void listCustomers() {
storage.values().forEach(System.out::println);
}
public void removeCustomer(String name) throws IllegalArgumentException {
if (!name.toLowerCase().matches("^[а-я]+\\s[а-я]+$")){
throw new IllegalArgumentException("Invalid input name's format. Correct format: Василий Петров");
}
storage.remove(name);
}
public int getCount() {
return storage.size();
}
}