need help outputing the details enter by the user please . public class Book { p
ID: 3548498 • Letter: N
Question
need help outputing the details enter by the user please .
public class Book
{
private String title;
private String author;
private int pages;
private String category;
private boolean read;
private double cost;
//default constructor
public Book()
{
title = "Not yet assigned";
author = "Not yet assigned";
pages = 0;
category = "Not yet assigned";
read = false;
cost = 0.0;
}
//non default constructor
public Book(String titlePassed, String authorPassed, int pagesPassed, String categoryPassed,
boolean readPassed, double costPassed)
{
title = titlePassed;
author = authorPassed;
pages = pagesPassed;
category = categoryPassed;
read = readPassed;
cost = costPassed;
}//end non default constructor
//getters
public String getTitle()
{
return title;
}//end getTitle
public String getAuthor()
{
return author;
}//end getAuthor
public int getPages()
{
return pages;
}//end get Pages
public String getCategory()
{
return category;
}//end getCategory
public boolean getRead()
{
return read;
}//end getRead
public double getCost()
{
return cost;
}//end getCost
//setters
public void setTitle(String titlePassed)
{
title = titlePassed;
}//end setTitle
public void setAuthor(String authorPassed)
{
author = authorPassed;
}//end setAuthor
public void setPages(int pagesPassed)
{
if(pagesPassed < 0)
pages = 0;
else
pages = pagesPassed;
}//end setPages
public void setCategory(String categoryPassed)
{
category = categoryPassed;
}//end setCategory
public void setRead(boolean readPassed)
{
read = readPassed;
}//end setRead
public void setCost(double costPassed)
{
if(costPassed < 0)
cost = 0;
else
cost = costPassed;
}//end setCost
//toString method
public String toString()
{
return " Title: " + title +
" Author: " + author +
" Pages: " + pages +
" Category: " + category +
" Read: " + read +
" Cost: " + cost;
}//end toString
//equals method
public boolean equals(Book bookPassed)
{
return (
this.title.equals(bookPassed.getTitle())
&&
this.author.equals(bookPassed.getAuthor())
&&
this.pages == bookPassed.getPages()
&&
this.category.equals(bookPassed.getCategory())
&&
this.read == bookPassed.getRead()
&&
this.cost == bookPassed.getCost()
);
}//end method equals
}//end class
import java.util.Scanner;
public class BookExceptionsDemodemo555
{
public static void main(String[] args) throws Exception
{
int n = 0, i = 0;
//Book temp = null;
boolean validPage = true;
boolean validCategory = true;
boolean validCost = true;
Book[] bookArray = null;
System.out.println("Enter the number of books");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
bookArray = new Book[n];
for (; i < n; i++) {
System.out.println("Enter Title of the Book");
String title = keyboard.next();
System.out.println("Enter name of the Author of Book");
String author = keyboard.next();
try{
System.out.println("Enter the number of pages in the Book");
int pages = keyboard.nextInt();
if(pages < 0){validPage = false;
throw new PageException("Pages should be > 0 . Enter Details of book again");
}
else{validPage = true;}
}
catch (PageException e) {
System.out.println(e.getMessage());
i--;
}
try{
System.out.println("Enter the Category of the Book (category should be any of Fiction, Non-Fiction, Biography, Technical, Children or Other)");
String category = keyboard.next();
if( category.equalsIgnoreCase("Fiction" || "NonFiction" || "Biography" ||
"Technical" || "Children"|| "Other")){
{validCategory = true; }
else {validCategory = false;
throw new CategoryException("Invalid Category. Enter Details of book again");
}
}
catch (CategoryException e) {
System.out.println(e.getMessage());
i--;
}
System.out.println("Enter Weather the Book is read");
boolean read = keyboard.nextBoolean();
try{
System.out.println("Enter the cost of Book");
double cost = keyboard.nextDouble();
if(cost < 0)
{validCost = false;
throw new CostException("Invalid Cost. Enter Details of book again");
}
else{validCost = true;}
}//end try
catch (CostException e) {
System.out.println(e.getMessage());
i--;
}
}//end for loop
System.out.println("The details of books are");
for (int j = 0; j < n-1
; j++) {
System.out.println(bookArray[n].toString());
}
}//end main
}//end class
import java.util.Scanner;
public class CategoryException extends Exception
{
public CategoryException()
{
super("Length Exception of unknown specificity.");
getAnotherCategory();
}
public CategoryException(String messagePassed)
{
super(messagePassed);
}
public String getAnotherCategory()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Category must be a Fiction, Non-Fiction, Biography, Technical, Children or Other");
return keyboard.nextLine();
}
}//end class
import java.util.Scanner;
public class CostException extends Exception
{
public CostException()
{
super("Cost Exception of unknown specificity.");
getAnotherCost();
}
public CostException(String messagePassed)
{
super(messagePassed);
}
public String getAnotherCost()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Cost must be > 0");
return keyboard.nextLine();
}
}//end class
import java.util.Scanner;
public class PageException extends Exception
{
//public boolean validLength;
public PageException()
{
super("Page Exception of unknown specificity.");
getAnotherPage();
}
public PageException(String messagePassed)
{
super(messagePassed);
}
public String getAnotherPage()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Page must be > 0");
return keyboard.nextLine();
}
}//end class
Explanation / Answer
DO RATE if satisfied.
Find Code with indentation at http://pastebin.com/b2qtPu22
//===========================BookDemo.java=========================
import java.util.Scanner;
public class BookDemo {
public static void main(String[] args) {
int n = 0, i = 0;
//int n;
Book temp = null;
Book[] bookArray = null;
System.out.println("Enter the number of books");
Scanner reader = new Scanner(System.in);
n=reader.nextInt();
bookArray = new Book[n];
for (; i < n; i++) {
try {
temp = new Book();
System.out.println("Enter Title of the Book");
temp.setTitle(reader.next());
System.out.println("Enter name of the Author of Book");
temp.setAuthor(reader.next());
System.out.println("Enter the number of pages in the Book");
temp.setPages(reader.nextInt());
System.out.println("Enter the Category of the Book (category should be any of Fiction, Non-Fiction, Biography, Technical, Children or Other)");
temp.setCategory(reader.next());
System.out.println("Enter Weather the Book is read");
temp.setRead(reader.nextBoolean());
System.out.println("Enter the cost of Book");
temp.setCost(reader.nextDouble());
bookArray[i] = temp;
temp = null;
} catch (PageException ex) {
System.out.println(ex.getMessage());
i--;
} catch (CostException ex) {
System.out.println(ex.getMessage());
i--;
} catch (CategoryException ex) {
System.out.println(ex.getMessage());
i--;
}
}
System.out.println("The details of books are");
for (int j = 0; j < n; j++) {
System.out.println(bookArray[j].toString());
}
}
}
//================================Book.java===========================
public class Book {
private String title;
private String author;
private int pages;
private String category;
private boolean read;
private double cost;
//default constructor
public Book() {
title = "Not yet assigned";
author = "Not yet assigned";
pages = 0;
category = "Not yet assigned";
read = false;
cost = 0.0;
}
//non default constructor
public Book(String titlePassed, String authorPassed, int pagesPassed, String categoryPassed,
boolean readPassed, double costPassed) {
title = titlePassed;
author = authorPassed;
pages = pagesPassed;
category = categoryPassed;
read = readPassed;
cost = costPassed;
}//end non default constructor
//getters
public String getTitle() {
return title;
}//end getTitle
public String getAuthor() {
return author;
}//end getAuthor
public int getPages() {
return pages;
}//end get Pages
public String getCategory() {
return category;
}//end getCategory
public boolean getRead() {
return read;
}//end getRead
public double getCost() {
return cost;
}//end getCost
//setters
public void setTitle(String titlePassed) {
title = titlePassed;
}//end setTitle
public void setAuthor(String authorPassed) {
author = authorPassed;
}//end setAuthor
public void setPages(int pagesPassed) throws PageException {
if (pagesPassed < 0) {
throw new PageException("Pages should be >0 . Enter Details of book again");
} else {
pages = pagesPassed;
}
}//end setPages
public void setCategory(String categoryPassed) throws CategoryException {
//Fiction, Non-Fiction, Biography, Technical, Children or Other
int i;
String [] cats = { "Fiction", "Non-Fiction", "Biography",
"Technical", "Children", "Other" };
for(i=0; i<cats.length; i++)
if( cats[i].equalsIgnoreCase(categoryPassed))
break;
if( i==cats.length )
throw new CategoryException("Invalid Category. Enter Details of book again");
category = categoryPassed;
}//end setCategory
public void setRead(boolean readPassed) {
read = readPassed;
}//end setRead
public void setCost(double costPassed) throws CostException {
if (costPassed < 0) {
throw new CostException("Cost should be >0 . Enter Details of book again");
} else {
cost = costPassed;
}
}//end setCost
//toString method
public String toString() {
return " Title: " + title
+ " Author: " + author
+ " Pages: " + pages
+ " Category: " + category
+ " Read: " + read
+ " Cost: " + cost;
}//end toString
//equals method
public boolean equals(Book bookPassed) {
return (this.title.equals(bookPassed.getTitle())
&& this.author.equals(bookPassed.getAuthor())
&& this.pages == bookPassed.getPages()
&& this.category.equals(bookPassed.getCategory())
&& this.read == bookPassed.getRead()
&& this.cost == bookPassed.getCost());
}//end method equals
}//end class
//================================PageException.java=================================
import java.util.Scanner;
public class PageException extends Exception {
public PageException() {
super("Page Exception of unknown specificity.");
getAnotherPage();
}
public PageException(String messagePassed) {
super(messagePassed);
}
public String getAnotherPage() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Page must be > 0");
return keyboard.nextLine();
}
}//end class PageException
//================================CostException.java=================================
import java.util.Scanner;
public class CostException extends Exception {
public CostException() {
super("Cost Exception of unknown specificity.");
getAnotherCost();
}
public CostException(String messagePassed) {
super(messagePassed);
}
public String getAnotherCost() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Cost must be > 0");
return keyboard.nextLine();
}
}//end class CostException
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.