Hi guys,
I'm writing a program to do several things involving getting information from the user about a book (title, author, publisher etc.)
There is a Book class and then the client class BookClient.
I got the program working for the user entering a single book but then I changed it so the user could enter an array of books.
I can enter in new books fine but when it comes to outputting, nothing happens.
It works when I have the loop for entering the books and the loop for outputting the books in the same method but not in different ones.
Any help would be greatly appreciated.
Thanks in advance :)
Here is the Book class:
import java.util.*;
public class Book
{
private String title;
private String author;
private String publisher;
private int year;
private long isbn;
private double price;
private int quantity;
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Title: ");
title = keyboard.nextLine();
System.out.print("Author: ");
author = keyboard.nextLine();
System.out.print("Publisher: ");
publisher = keyboard.nextLine();
System.out.print("Year: ");
year = keyboard.nextInt();
System.out.print("ISBN: ");
isbn = keyboard.nextLong();
System.out.print("Price: ");
price = keyboard.nextDouble();
System.out.print("Quantity: ");
quantity = keyboard.nextInt();
}
public void writeOutput()
{
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publisher: " + author);
System.out.println("Year: " + year);
System.out.println("ISBN: " + isbn);
System.out.println("Price: " + price);
System.out.println("Quantity: " + quantity);
}
public double value()
{
return quantity * price;
}
public void setBook(String newTitle, String newAuthor, String newPublisher, int newYear, long newIsbn, double newPrice, int newQuantity)
{
title = newTitle;
author = newAuthor;
publisher = newPublisher;
if(newYear < 0)
{
System.out.println("Error: negative year.");
System.exit(0);
}
else
year = newYear;
if(newIsbn < 0)
{
System.out.println("Error: negative ISBN.");
System.exit(0);
}
else
isbn = newIsbn;
if(newPrice < 0)
{
System.out.println("Error: negative price.");
System.exit(0);
}
else
price = newPrice;
if(newQuantity < 0)
{
System.out.println("Error: negative year.");
System.exit(0);
}
else
quantity = newQuantity;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public String getPublisher()
{
return publisher;
}
public int getYear()
{
return year;
}
public long getIsbn()
{
return isbn;
}
public double getPrice()
{
return price;
}
public int getQuantity()
{
return quantity;
}
}
and the client class:
import java.util.*;
public class BookClient
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int num = 0;
Book[] bookInfo = new Book[num];
Choice1(bookInfo);
Choice2(bookInfo);
}
public static Book[] Choice1(Book[] bookInfo)
{
System.out.print("Number of books to enter: ");
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
bookInfo = new Book[num];
for(int i = 0; i < bookInfo.length; i++)
{
System.out.println();
bookInfo[i] = new Book();
bookInfo[i].readInput();
System.out.println();
}
return bookInfo;
}
public static void Choice2(Book[] bookInfo)
{
for(int i = 0; i < bookInfo.length; i++)
bookInfo[i].writeOutput();
}
}