MIT-Curricular/OOP/Java/Lab/Week5/Book.java

50 lines
1.3 KiB
Java
Raw Normal View History

2024-09-02 00:07:44 +05:30
import java.util.Scanner;
class Book {
2024-09-02 01:24:23 +05:30
String title, author;
2024-09-02 00:07:44 +05:30
int edition;
public Book(String title, String author, int edition) {
this.title = title;
this.author = author;
this.edition = edition;
}
public String toString() {
2024-09-02 01:24:23 +05:30
return String.format("Title: %s, Author: %s, Edition: %d", title, author, edition);
2024-09-02 00:07:44 +05:30
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of books: ");
int numBooks = sc.nextInt();
sc.nextLine();
Book[] books = new Book[numBooks];
for (int i = 0; i < numBooks; i++) {
System.out.println("Enter details for book " + (i + 1) + ":");
System.out.print("Title: ");
String title = sc.nextLine();
System.out.print("Author: ");
String author = sc.nextLine();
System.out.print("Edition: ");
int edition = sc.nextInt();
sc.nextLine();
books[i] = new Book(title, author, edition);
}
System.out.print("Enter author name to search: ");
String searchAuthor = sc.nextLine();
for (Book book : books) {
2024-09-02 01:24:23 +05:30
if (book.author.equals(searchAuthor)) {
2024-09-02 00:07:44 +05:30
System.out.println(book);
}
}
}
}