0% found this document useful (0 votes)
25 views

Books N Authors

Uploaded by

sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Books N Authors

Uploaded by

sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Arrays;
import java.util.Scanner;

public class Authors_N_Books_Solution {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Author[] arr1 = new Author[5];
Books[] arr2 = new Books[5];
for(int i=0; i<arr2.length; i++){
int a = sc.nextInt(); sc.nextLine();
String b = sc.nextLine();
int m = sc.nextInt(); sc.nextLine();
String n = sc.nextLine();
String o = sc.nextLine();
double p = sc.nextDouble();sc.nextLine();

// arr1[i] = new Author(a,b);


Author author = new Author(a, b);
arr2[i] = new Books(m,n,o,p,author);
}
String genre1 = sc.nextLine();
String genre2 = sc.nextLine();
double discount = Double.parseDouble(sc.nextLine());
// System.out.println("Enter Discount");

for (int i=0; i<arr2.length; i++){


if(arr2[i].getGenre().equalsIgnoreCase(genre1)){
System.out.println("AuthorName: " +
arr2[i].getAuthor().getAuthorName() + ", Title: " + arr2[i].getBookTitle());
}
}

Books[] res1 = Business.getBooksBelongingToGenre(arr2, genre2, discount);


if(res1==null){
System.out.println("Discounted books are unavailable in the given
genre");
}
else {
System.out.println("Discounted " + genre2 + " books:");
for(Books b : res1){
// System.out.println("Discounted " + b.getGenre() + " books:");
System.out.println("AuthorName: " + b.author.getAuthorName() + ",
Title: " + b.getBookTitle() + ", Updated Price: " + b.getPrice());
}
}

}
}

class Business{
public static Books[] getBooksBelongingToGenre(Books[] arr2, String genre2,
double discount){
Books[] refined = new Books[0];
for(int i=0; i<arr2.length; i++){
if(arr2[i].getGenre().equalsIgnoreCase(genre2)){
refined = Arrays.copyOf(refined, refined.length+1);
// refined[refined.length-1] = arr2[i];
arr2[i].applyDiscount(discount); // Apply discount method
refined[refined.length - 1] = arr2[i]; // Add to refined array
}
}
if (refined.length == 0){
return null;
}
else {
return refined;
}
}
}

class Author{
int authorId;
String authorName;

public Author(int authorId, String authorName) {


this.authorId = authorId;
this.authorName = authorName;
}

public int getAuthorId() {


return authorId;
}

public void setAuthorId(int authorId) {


this.authorId = authorId;
}

public String getAuthorName() {


return authorName;
}

public void setAuthorName(String authorName) {


this.authorName = authorName;
}
}

class Books{
int bookId;
String bookTitle;
String genre;
double price;
Author author;

public Books(int bookId, String bookTitle, String genre, double price, Author
author) {
this.bookId = bookId;
this.bookTitle = bookTitle;
this.genre = genre;
this.price = price;
this.author = author;
}
public int getBookId() {
return bookId;
}

public void setBookId(int bookId) {


this.bookId = bookId;
}

public String getBookTitle() {


return bookTitle;
}

public void setBookTitle(String bookTitle) {


this.bookTitle = bookTitle;
}

public String getGenre() {


return genre;
}

public void setGenre(String genre) {


this.genre = genre;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public Author getAuthor() {


return author;
}

public void setAuthor(Author author) {


this.author = author;
}

public void applyDiscount(double discount) {


this.price = this.price - (this.price * discount / 100);
}
}

You might also like