Repository navigation
Expand file tree
/
Copy pathTestLibrary.java
More file actions
59 lines (51 loc) · 1.75 KB
/
Copy pathTestLibrary.java
File metadata and controls
59 lines (51 loc) · 1.75 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
import java.util.Scanner;
class Book {
String name, author;
double price;
int copies;
public void readBook(Scanner sc) {
System.out.print("Enter Book Name: ");
name = sc.nextLine();
System.out.print("Enter Author: ");
author = sc.nextLine();
System.out.print("Enter Price: ");
price = sc.nextDouble();
System.out.print("Enter Copies: ");
copies = sc.nextInt();
sc.nextLine();
}
public void display() {
System.out.println("Name: " + name + ", Author: " + author + ", Price: " + price + ", Copies: " + copies);
}
}
public class TestLibrary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Book[] library = new Book[5];
System.out.println("Enter details for 5 books:");
for (int i = 0; i < 5; i++) {
library[i] = new Book();
library[i].readBook(sc);
}
System.out.print("\nSearch Book by Name: ");
String searchName = sc.nextLine();
boolean foundName = false;
for (Book b : library) {
if (b.name.equalsIgnoreCase(searchName)) {
System.out.println("Book found! Copies available: " + b.copies);
foundName = true;
}
}
if (!foundName)
System.out.println("Book not available.");
System.out.print("\nSearch Book by Author: ");
String searchAuthor = sc.nextLine();
System.out.println("Books by " + searchAuthor + ":");
for (Book b : library) {
if (b.author.equalsIgnoreCase(searchAuthor)) {
b.display();
}
}
sc.close();
}
}