-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
71 lines (59 loc) · 1.73 KB
/
Copy pathBook.java
File metadata and controls
71 lines (59 loc) · 1.73 KB
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
// Book class represents a book with title and author
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;
}
public String getAuthor() {
return author;
}
}
// Library class manages books
class Library {
private Book[] books;
private int numberOfBooks;
public Library(int capacity) {
books = new Book[capacity];
numberOfBooks = 0;
}
public void addBook(Book book) {
if (numberOfBooks < books.length) {
books[numberOfBooks] = book;
numberOfBooks++;
} else {
System.out.println("Library is full, cannot add more books.");
}
}
public Book[] getBooks() {
return books;
}
}
// LibraryPrinter class is responsible for printing information about books
class LibraryPrinter {
public void printBooks(Book[] books) {
for (Book book : books) {
if (book != null) {
System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor());
}
}
}
}
// Main class to demonstrate the Single Responsibility Principle
public class Main {
public static void main(String[] args) {
Library library = new Library(3);
Book book1 = new Book("Book 1", "Author 1");
Book book2 = new Book("Book 2", "Author 2");
Book book3 = new Book("Book 3", "Author 3");
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
LibraryPrinter printer = new LibraryPrinter();
printer.printBooks(library.getBooks());
}
}