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

64 lines
1.7 KiB
Java

import java.util.Scanner;
class Book {
String title, author;
int edition;
public Book(String title, String author, int edition) {
this.title = title;
this.author = author;
this.edition = edition;
}
public String toString() {
return String.format(
"Title: %s, Author: %s, Edition: %d",
title,
author,
edition
);
}
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) {
if (book.author.equalsIgnoreCase(searchAuthor)) {
System.out.println(book);
return;
}
}
System.out.println("No books found for the author: " + searchAuthor);
if (!found) {
System.out.println(
"No books found for the author: " + searchAuthor
);
}
sc.close();
}
}