-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library2
73 lines (57 loc) · 1.74 KB
/
Library2
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Main {
public static void main(String[] args) {
Library library = new Library();
// Добавление книг в библиотеку
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee"));
library.addBook(new Book("1984", "George Orwell"));
library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald"));
// Поиск книги по названию
Book book = library.findBookByTitle("1984");
if (book != null) {
System.out.println("Найдена книга: " + book);
} else {
System.out.println("Книга не найдена");
}
// Вывод всех книг
System.out.println("Все книги в библиотеке:");
library.listBooks();
}
}
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Название: " + title + ", Автор: " + author;
}
}
import java.util.ArrayList;
public class Library {
private ArrayList<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public Book findBookByTitle(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
public void listBooks() {
for (Book book : books) {
System.out.println(book);
}
}
}