Update OOP/Java/Lab/Week5/Book.java

This commit is contained in:
Aadit Agrawal 2024-09-02 01:24:23 +05:30
parent dcfe2c14e5
commit b178260bf8

View File

@ -1,9 +1,7 @@
import java.util.Scanner; import java.util.Scanner;
class Book { class Book {
String title, author;
String title;
String author;
int edition; int edition;
public Book(String title, String author, int edition) { public Book(String title, String author, int edition) {
@ -12,22 +10,12 @@ class Book {
this.edition = edition; this.edition = edition;
} }
public String getAuthor() {
return author;
}
public String toString() { public String toString() {
return String.format( return String.format("Title: %s, Author: %s, Edition: %d", title, author, edition);
"Title: %s, Author: %s, Edition: %d",
title,
author,
edition
);
} }
} }
class Main { class Main {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of books: "); System.out.print("Enter the number of books: ");
@ -53,7 +41,7 @@ class Main {
String searchAuthor = sc.nextLine(); String searchAuthor = sc.nextLine();
for (Book book : books) { for (Book book : books) {
if (book.getAuthor().equals(searchAuthor)) { if (book.author.equals(searchAuthor)) {
System.out.println(book); System.out.println(book);
} }
} }