Upload files to "OOP/Java/Lab/Week5"
This commit is contained in:
parent
cb924d0b60
commit
8c2e55a281
5 changed files with 413 additions and 0 deletions
61
OOP/Java/Lab/Week5/Book.java
Normal file
61
OOP/Java/Lab/Week5/Book.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue