-
Notifications
You must be signed in to change notification settings - Fork 95
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
completed task #258
base: master
Are you sure you want to change the base?
completed task #258
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ru.faang.school.hashmap.task_2; | ||
|
||
import java.util.Objects; | ||
|
||
public class Book { | ||
private String title; | ||
private String author; | ||
private int year; | ||
|
||
public Book(String title, String author, int year) { | ||
this.title = title; | ||
this.author = author; | ||
this.year = year; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Book book = (Book) o; | ||
|
||
if (year != book.year) return false; | ||
if (!Objects.equals(title, book.title)) return false; | ||
return Objects.equals(author, book.author); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = title != null ? title.hashCode() : 0; | ||
result = 31 * result + (author != null ? author.hashCode() : 0); | ||
result = 31 * result + year; | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Book{" + | ||
"title='" + title + '\'' + | ||
", author='" + author + '\'' + | ||
", year=" + year + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ru.faang.school.hashmap.task_2; | ||
|
||
import javax.swing.plaf.synth.SynthEditorPaneUI; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
|
||
private final static Map<Book, String> library = new HashMap<>(); | ||
|
||
public static void addBookToLibrary(Book book, String location) { | ||
if (library.containsKey(book)) { | ||
System.out.println("Ну и приколы - абсолютно две одинаковые книги! Не может такое быть :)"); | ||
} else { | ||
library.put(book, location); | ||
System.out.println(String.format("Книгу \"%s\" положили на %s полку", book.getTitle(), location)); | ||
} | ||
} | ||
|
||
public static void removeBook(String title, String author, int year) { | ||
Book book = new Book(title, author, year); | ||
if (library.containsKey(book)) { | ||
library.remove(book); | ||
System.out.println(String.format("Книга %s автор %s год %d удалена", title, author, year)); | ||
} else { | ||
System.out.println("Такой книги нет - ничего не удалили"); | ||
} | ||
} | ||
|
||
public static void findBook(String title, String author, int year) { | ||
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. Можно отсюда вернуть ее location тогда) 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. имеешь в виду, чтобы метод возвращал location? |
||
Book book = new Book(title, author, year); | ||
if (library.containsKey(book)) { | ||
System.out.println(String.format("Книга находится %s", library.get(book))); | ||
} else { | ||
System.out.println("Такой книги нет в библиотеке"); | ||
} | ||
} | ||
|
||
public static void printAllBooks() { | ||
for (Map.Entry<Book, String> book: library.entrySet()) { | ||
System.out.println(book + " Местонахождение: " + book.getValue()); | ||
|
||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Book book1 = new Book("Мастер и Маргарита", "Михаил Булгаков", 1929); | ||
Book book2 = new Book("Собачье сердце", "Михаил Булгаков", 1925); | ||
Book book3 = new Book("Ревизор", "Николай Гоголь", 1836); | ||
Book book4 = new Book("Отцы и дети", "Иван Тургенев", 1860); | ||
Book book5 = new Book("Мастер и Маргарита", "Михаил Булгаков", 1929); | ||
|
||
addBookToLibrary(book1, "5"); | ||
addBookToLibrary(book5, "10"); | ||
addBookToLibrary(book3, "124"); | ||
printAllBooks(); | ||
|
||
removeBook("asdlas", "dals;kd", 100); | ||
removeBook("Ревизор", "Николай Гоголь", 1836); | ||
printAllBooks(); | ||
|
||
} | ||
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. Есть пара лишних пустых строчек. Если их удалить, то код будет чуть-чуть аккуратнее. Но мелочь совсем |
||
} |
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.
Если это static final, то это константа, а константы в джаве называются всеми большими буквами
Тогда LIBRARY