-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasestudy1.java
More file actions
99 lines (82 loc) · 2.94 KB
/
Copy pathcasestudy1.java
File metadata and controls
99 lines (82 loc) · 2.94 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Scenario:
You are tasked with developing a simple Library Management System in Java. The system should allow users to add books, search for books by title, and display all books in the library. Each book has a title, author, and ISBN number.
Requirements:
Create a Book class with the following attributes:
title (String)
author (String)
isbn (String)
Create a Library class that manages a collection of books. The Library class should have the following methods:
addBook(Book book): Adds a book to the library.
searchByTitle(String title): Searches for a book by its title and returns the book if found.
displayAllBooks(): Displays all books in the library.
Create a Main class to test the functionality of the Library class.
Question:
Write the Java code to implement the above scenario. Your code should include the Book class, the Library class, and the Main class. Ensure that the Main class demonstrates adding books, searching for a book by title, and displaying all books.
*/
import java.util.ArrayList;
class Book {
private String title;
private String author;
private String isbn;
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void displayBook() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + isbn);
System.out.println("----------------------");
}
}
class Library {
private ArrayList<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("Book added successfully!");
}
public Book searchByTitle(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
public void displayAllBooks() {
if (books.isEmpty()) {
System.out.println("Library is empty.");
} else {
for (Book book : books) {
book.displayBook();
}
}
}
}
public class casestudy1 {
public static void main(String[] args) {
Library library = new Library();
Book book1 = new Book("Java Programming", "James Gosling", "101");
Book book2 = new Book("Data Structures", "Mark Allen", "102");
library.addBook(book1);
library.addBook(book2);
System.out.println("\nDisplaying All Books:");
library.displayAllBooks();
System.out.println("\nSearching for a Book:");
Book foundBook = library.searchByTitle("Java Programming");
if (foundBook != null) {
System.out.println("Book Found:");
foundBook.displayBook();
} else {
System.out.println("Book not found.");
}
}
}