-
Notifications
You must be signed in to change notification settings - Fork 4
Fr] implement add and search methods for phone book interface #30 #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| ; | ||
|
|
||
| default void save(ContactDto contactDto) throws IOException { | ||
| List<ContactDto> contacts = findAll(); | ||
| contacts.add(contactDto); | ||
| saveAll(contacts); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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:
This comment was generated by an experimental AI tool.