Question

Need in Java coding Write a program that will allow users to enter different types of...

Need in Java coding

Write a program that will allow users to enter different types of books into the program, and at the end print out everything that the user entered. Allow the user to enter as many books as they want. Please collect the following information and break down the classes as follows using inheritance/classes. I have added the extra properties that each class should include.

Book types:

Paper Book (nothing extra)

Comic Book (issue number)

Magazine (publisher, issue number) Note that the magazine title would be different than the article title. It would be something like US Weekly is magazine title and title for book would be whatever the article would be about.

Ebook (url)

Properties all books share:

Author(s)

Title

Price

Quantity sold

Date published

Homework Answers

Answer #1

Please find the below java program

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;


//This is the base class.
class Book{
String authors,title;
double price;
int quantity;
Date published;
Book(String authors,String title, double price, int quantity, Date published){
this.authors = authors;
this.title = title;
this.price = price;
this.quantity = quantity;
this.published = published;
}
}

//Extends Book
class PaperBook extends Book{
PaperBook(String authors,String title, double price, int quantity, Date published){
super( authors, title, price, quantity, published);
}
@Override
public String toString(){
return "Authors : "+authors+" Title : "+title+" Price : "+price+" Quantity : "+quantity+" Published : "+published;
}
}

//This ComicBook extends Book
class ComicBook extends Book{
int issueNumber;
ComicBook(String authors,String title, double price, int quantity, Date published, int issueNumber){
super( authors, title, price, quantity, published);
this.issueNumber = issueNumber;
}
@Override
public String toString(){
return "Authors : "+authors+" Title : "+title+" Price : "+price+" Quantity : "+quantity+" Published : "+published+" IssueNumber : "+issueNumber;
}
}

//Magazine extends ComicBook
class Magazine extends ComicBook{
String publisher;
Magazine(String authors,String title, double price, int quantity, Date published, String publisher, int issueNumber){
super( authors, title, price, quantity, published, issueNumber);
this.publisher = publisher;
}
@Override
public String toString(){
return "Authors : "+authors+" Title : "+title+" Price : "+price+" Quantity : "+quantity+" Published : "+published+" IssueNumber : "+issueNumber;
}
}
class EBook extends Book{
String URL;
EBook(String authors,String title, double price, int quantity, Date published, String URL){
super( authors, title, price, quantity, published);
this.URL = URL;
}
@Override
public String toString(){
return "Authors : "+authors+" Title : "+title+" Price : "+price+" Quantity : "+quantity+" URL : "+URL;
}
}
class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
   //Collect all the books in array list of books of the type Book
       ArrayList<Book> books = new ArrayList();
       Scanner obj = new Scanner(System.in);
       while(true){
       System.out.println("Enter the type of the book");
       String type = obj.next();
       //Read the inputs and create a corresponding object and add it to the list books
       if(type.equals("PaperBook")){
       System.out.println("Enter author name");
       String author = obj.next();
       System.out.println("Enter title of the book");
       String title = obj.next();
       System.out.println("Enter price of the book");
       double price = obj.nextDouble();
       System.out.println("Enter quantity of books");
       int quantity = obj.nextInt();
       System.out.println("Enter the date of publishing");
       String date = obj.next();
       Date d=new SimpleDateFormat("dd/MM/yyyy").parse(date);
       Book p = new PaperBook(author, title, price, quantity, d);
       books.add(p);
       }
       else if(type.equals("ComicBook")){
       System.out.println("Enter author name");
       String author = obj.next();
       System.out.println("Enter title of the book");
       String title = obj.next();
       System.out.println("Enter price of the book");
       double price = obj.nextDouble();
       System.out.println("Enter quantity of books");
       int quantity = obj.nextInt();
       System.out.println("Enter the date of publishing");
       String date = obj.next();
       Date d=new SimpleDateFormat("dd/MM/yyyy").parse(date);
       System.out.println("Enter issue number");
       int issue = obj.nextInt();
       Book p = new ComicBook(author, title, price, quantity, d,issue);
       books.add(p);
       }
       else if(type.equals("Magazine")){
       System.out.println("Enter author name");
       String author = obj.next();
       System.out.println("Enter title of the book");
       String title = obj.next();
       System.out.println("Enter price of the book");
       double price = obj.nextDouble();
       System.out.println("Enter quantity of books");
       int quantity = obj.nextInt();
       System.out.println("Enter the date of publishing");
       String date = obj.next();
       Date d=new SimpleDateFormat("dd/MM/yyyy").parse(date);
       System.out.println("Enter the name of the publisher");
       String publisher = obj.next();
       System.out.println("Enter issue number");
       int issue = obj.nextInt();
       Book p = new Magazine(author, title, price, quantity, d,publisher, issue);
       books.add(p);
       }
       else if(type.equals("EBook")){
       System.out.println("Enter author name");
       String author = obj.next();
       System.out.println("Enter title of the book");
       String title = obj.next();
       System.out.println("Enter price of the book");
       double price = obj.nextDouble();
       System.out.println("Enter quantity of books");
       int quantity = obj.nextInt();
       System.out.println("Enter the date of publishing");
       String date = obj.next();
       Date d=new SimpleDateFormat("dd/MM/yyyy").parse(date);
       System.out.println("Enter the URL of the book");
       String URL = obj.next();
       Book p = new EBook(author, title, price, quantity, d,URL);
       books.add(p);
       }
       else{
       break;
       }
           System.out.println();
       }
       //Iterate all the books in the list and print it
       for(Book x : books){
       System.out.println(x);
       }
   }
}

OUTPUT :

(Note : please input all the strings in one word only)

P:\>java Main
Enter the type of the book
ComicBook
Enter author name
Prudhvi
Enter title of the book
Comix
Enter price of the book
34.5
Enter quantity of books
1
Enter the date of publishing
21/01/1995
Enter issue number
12335

Enter the type of the book
PaperBook
Enter author name
JCDas
Enter title of the book
Programming
Enter price of the book
23
Enter quantity of books
2
Enter the date of publishing
04/11/2014

Enter the type of the book
Magazine
Enter author name
YVNath
Enter title of the book
Tulasi
Enter price of the book
12
Enter quantity of books
4
Enter the date of publishing
04/10/1988
Enter the name of the publisher
Vipula
Enter issue number
12331

Enter the type of the book
EBook
Enter author name
Forouzen
Enter title of the book
Networking
Enter price of the book
37
Enter quantity of books
1
Enter the date of publishing
16/08/1999
Enter the URL of the book
https://www.pactpub.ebooks/networking/fundamentals

Enter the type of the book
Exit
Authors : Prudhvi Title : Comix Price : 34.5 Quantity : 1 Published : Sat Jan 21
00:00:00 IST 1995 IssueNumber : 12335
Authors : JCDas Title : Programming Price : 23.0 Quantity : 2 Published : Tue No
v 04 00:00:00 IST 2014
Authors : YVNath Title : Tulasi Price : 12.0 Quantity : 4 Published : Tue Oct 04
00:00:00 IST 1988 IssueNumber : 12331
Authors : Forouzen Title : Networking Price : 37.0 Quantity : 1 URL : https://ww
w.pactpub.ebooks/networking/fundamentals

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT