Skip to content

Commit 70bc477

Browse files
Add files via upload
1 parent 8aed8ff commit 70bc477

File tree

13 files changed

+215
-0
lines changed

13 files changed

+215
-0
lines changed

base/mod1/contactsapp/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.example</groupId>
8+
<artifactId>contactsapp</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.springframework</groupId>
13+
<artifactId>spring-context</artifactId>
14+
<version>6.0.0</version>
15+
</dependency>
16+
</dependencies>
17+
18+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.contacts;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@ComponentScan(basePackages = "com.example.contacts")
9+
public class AppConfig {
10+
11+
@Bean
12+
public ContactService contactService() {
13+
return new ContactService();
14+
}
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.contacts;
2+
3+
public class Contact {
4+
private String fullName;
5+
private String phoneNumber;
6+
private String email;
7+
8+
// Конструктор
9+
public Contact(String fullName, String phoneNumber, String email) {
10+
this.fullName = fullName;
11+
this.phoneNumber = phoneNumber;
12+
this.email = email;
13+
}
14+
15+
// Геттеры
16+
public String getFullName() {
17+
return fullName;
18+
}
19+
20+
public void setFullName(String fullName) {
21+
this.fullName = fullName;
22+
}
23+
24+
public String getPhoneNumber() {
25+
return phoneNumber;
26+
}
27+
28+
public void setPhoneNumber(String phoneNumber) {
29+
this.phoneNumber = phoneNumber;
30+
}
31+
32+
public String getEmail() {
33+
return email;
34+
}
35+
36+
public void setEmail(String email) {
37+
this.email = email;
38+
}
39+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.example.contacts;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public class ContactService {
8+
private final List<Contact> contacts = new ArrayList<>();
9+
10+
// Получение всех контактов
11+
public List<Contact> getAllContacts() {
12+
return contacts;
13+
}
14+
15+
// Добавление контакта
16+
public void addContact(Contact contact) {
17+
contacts.add(contact);
18+
}
19+
20+
// Удаление контакта по email
21+
public boolean removeContactByEmail(String email) {
22+
Optional<Contact> contactToRemove = contacts.stream()
23+
.filter(contact -> contact.getEmail().equals(email))
24+
.findFirst();
25+
if (contactToRemove.isPresent()) {
26+
contacts.remove(contactToRemove.get());
27+
return true;
28+
}
29+
return false;
30+
}
31+
32+
// Сохранение контактов в файл
33+
public void saveContactsToFile(String filePath) {
34+
try (java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(filePath))) {
35+
for (Contact contact : contacts) {
36+
writer.write(contact.getFullName() + ";" + contact.getPhoneNumber() + ";" + contact.getEmail());
37+
writer.newLine();
38+
}
39+
} catch (Exception e) {
40+
System.out.println("Ошибка при сохранении контактов в файл: " + e.getMessage());
41+
}
42+
}
43+
44+
// Загрузка контактов из файла
45+
public void loadContactsFromFile(String filePath) {
46+
try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(filePath))) {
47+
String line;
48+
while ((line = reader.readLine()) != null) {
49+
String[] parts = line.split(";");
50+
if (parts.length == 3) {
51+
addContact(new Contact(parts[0], parts[1], parts[2]));
52+
}
53+
}
54+
} catch (Exception e) {
55+
System.out.println("Ошибка при загрузке контактов из файла: " + e.getMessage());
56+
}
57+
}
58+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.example.contacts;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
import java.util.Scanner;
7+
8+
public class MainApp {
9+
10+
public static void main(String[] args) {
11+
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
12+
ContactService contactService = context.getBean(ContactService.class);
13+
14+
String initFilePath = "src/main/resources/contacts.txt";
15+
contactService.loadContactsFromFile(initFilePath);
16+
System.out.println("Контакты успешно загружены из файла: contacts.txt");
17+
18+
Scanner scanner = new Scanner(System.in);
19+
while (true) {
20+
System.out.println("Введите команду: list, add, remove, save, exit");
21+
String command = scanner.nextLine();
22+
23+
switch (command) {
24+
case "list":
25+
// Проверка наличия контактов и вывод сообщения, если их нет
26+
if (contactService.getAllContacts().isEmpty()) {
27+
System.out.println("Тут пока что пусто, добавьте кто-то");
28+
} else {
29+
contactService.getAllContacts().forEach(contact ->
30+
System.out.println(contact.getFullName() + " | " + contact.getPhoneNumber() + " | " + contact.getEmail()));
31+
}
32+
break;
33+
case "add":
34+
// Добавление нового контакта
35+
System.out.println("Введите данные в формате: Ф. И. О.;номер телефона;email");
36+
String input = scanner.nextLine();
37+
String[] parts = input.split(";");
38+
if (parts.length == 3) {
39+
contactService.addContact(new Contact(parts[0], parts[1], parts[2]));
40+
System.out.println("Контакт добавлен");
41+
} else {
42+
System.out.println("Некорректный ввод. Ожидаемый формат: Ф. И. О.;номер телефона;email");
43+
}
44+
break;
45+
case "remove":
46+
// Удаление контакта по email
47+
System.out.println("Введите email контакта для удаления:");
48+
String email = scanner.nextLine();
49+
if (contactService.removeContactByEmail(email)) {
50+
System.out.println("Контакт удален");
51+
} else {
52+
System.out.println("Контакт с таким email не найден");
53+
}
54+
break;
55+
case "save":
56+
// Сохранение контактов в файл
57+
contactService.saveContactsToFile("src/main/resources/contacts.txt");
58+
System.out.println("Контакты сохранены");
59+
break;
60+
case "exit":
61+
// Завершение программы
62+
System.out.println("Выход");
63+
return;
64+
default:
65+
System.out.println("Неизвестная команда");
66+
}
67+
}
68+
}
69+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
contacts.init.file=src/main/resources/default-contacts.txt
2+
contacts.output.file=src/main/resources/contacts.txt
3+
spring.profiles.active=init
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Мамаев Сергей Анатольевич;+79441235812;kojhtddg@gmail.com
2+
Андреев Виктов Иванович;+89718124917;korwjkfj@gmail.com
3+
Синицин Алексей Викторович;+82921412481;kowfq3@yandex.ru
4+
Воробьев Сергей Андреевич;+89234142817;kfjawjfjrj@urfu.me
5+
Камушкин Олег Михайлови;+89233331231;akawjk@mail.ru
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
contacts.init.file=src/main/resources/default-contacts.txt
2+
contacts.output.file=src/main/resources/contacts.txt
3+
spring.profiles.active=init
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)