-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
55 lines (44 loc) · 1.25 KB
/
Main.java
File metadata and controls
55 lines (44 loc) · 1.25 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
import java.util.ArrayList;
import java.util.List;
// Define a Book class
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;
}
}
// Define a Library class
class Library {
private List<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
this.books.add(book);
}
public void printBooks() {
for (Book book : books) {
System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor());
}
}
}
public class Main {
public static void main(String[] args) {
// Create a library
Library library = new Library();
// Add books to the library
library.addBook(new Book("1984", "George Orwell"));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee"));
library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald"));
// Print all books in the library
library.printBooks();
}
}