Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@
<artifactId>picocli</artifactId>
<version>4.7.6</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.18.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
package ua.com.javarush.gnew.m2.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import ua.com.javarush.gnew.m2.dto.ContactDto;

public interface ContactDtoRepository {
List<ContactDto> findAll();
ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

Optional<ContactDto> findById(long id);
@SneakyThrows
default List<ContactDto> findAll() throws IOException {
File file = new File("demo.st");
if (!file.exists()) {
return new ArrayList<>();
}

void deleteById(long id);
return objectMapper.readValue(
file, objectMapper.getTypeFactory().constructCollectionType(List.class, ContactDto.class));
}

void saveAll(List<ContactDto> contacts);
@SneakyThrows
default Optional<ContactDto> findById(long id) throws IOException {
return findAll().stream().filter(contact -> contact.getId() == id).findFirst();
}

void save(ContactDto contactDto);
default void deleteById(long id) throws IOException {
Optional<ContactDto> contactToDelete = findById(id);
if (contactToDelete.isPresent()) {
List<ContactDto> contacts = findAll();
contacts.remove(contactToDelete.get());
saveAll(contacts);
} else {
System.out.println("Контакт с id " + id + " не найден.");
}
}

@SneakyThrows
default void saveAll(List<ContactDto> contacts) throws IOException {
objectMapper.writeValue(new File("demo.st"), contacts);
}
;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Codacy found a minor Code Style issue: ';' should be separated from previous line.

The issue identified by the Checkstyle linter is that there is an unnecessary semicolon (;) on its own line, which is not properly separated from the previous line. In Java, a semicolon is used to terminate statements, and having it on a separate line without any context is not a standard practice and can lead to confusion.

To fix this issue, simply remove the semicolon from its own line. Here’s the suggested change:

Suggested change
;
}

This comment was generated by an experimental AI tool.


default void save(ContactDto contactDto) throws IOException {
List<ContactDto> contacts = findAll();
contacts.add(contactDto);
saveAll(contacts);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут треба перевірити ContactDto.id та створити новий унікальний якщо id = 0;
Тобто не зовсім тут, це інтерфейс.


default List<ContactDto> findByKeyword(String keyword) throws IOException {
List<ContactDto> contacts = findAll();
return contacts.stream()
.filter(
contact ->
contact.getFullName().contains(keyword)
|| contact.getPhones().stream().anyMatch(phones -> phones.contains(keyword))
|| contact.getEmails().stream().anyMatch(emails -> emails.contains(keyword)))
.collect(Collectors.toList());
}
}
23 changes: 20 additions & 3 deletions src/main/java/ua/com/javarush/gnew/m2/service/SimplePhoneBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import ua.com.javarush.gnew.m2.dto.ContactDto;
import ua.com.javarush.gnew.m2.repository.ContactDtoRepository;

public class SimplePhoneBook implements PhoneBookInterface {
public class SimplePhoneBook implements PhoneBookInterface, ContactDtoRepository {
@Override
public ContactDto add(ContactDto contactDto) {
return contactDto;

return ContactDto.builder()
.fullName(contactDto.getFullName())
.phones(List.of(String.valueOf(contactDto.getPhones())))
.emails(List.of(String.valueOf(contactDto.getEmails())))
.build();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не зовсім зрозумів що ти планував. Ти вже отримуєш ContactDto його треба зберегти до файлу за допомогою ContactDtoRepository

}

@SneakyThrows
@Override
public List<ContactDto> search(String str) {
return Collections.emptyList();

List<ContactDto> contacts = findAll();
return contacts.stream()
.filter(
contact ->
contact.getFullName().contains(str)
|| contact.getPhones().stream().anyMatch(phones -> phones.contains(str))
|| contact.getEmails().stream().anyMatch(emails -> emails.contains(str)))
.collect(Collectors.toList());
}

@Override
Expand Down