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

62 lines
1.5 KiB
Java

import java.util.Scanner;
class Book {
String title;
String author;
int edition;
public Book(String title, String author, int edition) {
this.title = title;
this.author = author;
this.edition = edition;
}
public String getAuthor() {
return author;
}
public String toString() {
return String.format(
"Title: %s, Author: %s, Edition: %d",
title,
author,
edition
);
}
}
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) {
if (book.getAuthor().equals(searchAuthor)) {
System.out.println(book);
}
}
}
}