I am writing a bookstore type program with a data class and an executable class. My problem is, when I display the array with all 6 books, it displays 6 lines of the same book. I am not exactly sure what I did wrong.
Here are the instructions I was given:
Class TestBook
This class needs a main method and two more methods.
In main:
- create an array capable of holding six Book objects.
- use the parameterized constructor to specify the data in the first four elements of this array
- use the no-arg constructor to create the two remaining books in the array.
- process the array with a foreach loop to display the array at this point.
- call the finishArray() method with the array as the only argument.
- call the reduceBooks() method with the array as the sole argument.
- repeat the code needed by Step 4 above.
- display the most expensive book after the discounts.
In finishArray():
- this is a void method.
- use the setter methods to specify the data in all fields of the last two books in the array.
In reduceBooks():
- this method returns a Book instance.
- use a loop (any type) to reduce the price of every book in the array by 40%.
- determine the most expensive book after the discounts and return this book to main.
Here is my code:
package week6.yedkois;
public class TestBook {
private static String _title;
private static String _author;
private static int _pages;
private static double _price;
public TestBook(String title, String author, int pages, int price){
this._title = title;
this._author = author;
this._pages = pages;
this._price = price;
}
public static void main(String[] args){
Book[] books = new Book[6];
Book book1 = new Book("Java Programming", "Liang", 1320, 149.99);
Book book2 = new Book("Rainbow Six", "Tom Clancy", 350, 10.99);
Book book3 = new Book("Tangerine", "Bloor", 180, 8.99);
Book book4 = new Book("End of Watch", "King", 320, 21.42);
Book book5 = new Book();
Book book6 = new Book();
books[0] = book1;
books[1] = book2;
books[2] = book3;
books[3] = book4;
books[4] = book5;
books[5] = book6;
for (Book u : books)
System.out.print(u);
}
public TestBook(){
Book[] books = new Book[6];
Book book5 = new Book("House", "House", 1320, 89);
Book book6 = new Book("Car", "Car", 1320, 89);
}
public static double reduceBooks(Book[] books){
return 0;
}
void finishArray(){
Book newbook = new Book();
newbook.setTitle(_title);
newbook.setAuthor(_author);
newbook.setPages(_pages);
newbook.setPrice(_price);
}
}
here is the data class
package week6.yedkois;
public class Book {
private static String _title;
private static String _author;
private static int _pages;
private static double _price;
public static int numBooks = 0;
public Book(){
for (int i = 0; i > 6; i++)
numBooks++;
}
public Book(String title, String author, int pages, double price)
{ super();
this._title = title;
this._author = author;
this._pages = pages;
this._price = price;
}
public String getTitle()
{
return _title;
}
public void setTitle(String title)
{
this._title = title;
}
public String getAuthor()
{
return _author;
}
public void setAuthor(String author)
{
this._author = author;
}
public int getPages()
{
return _pages;
}
public void setPages(int pages)
{
this._pages = pages;
}
public double getPrice()
{
return _price;
}
public void setPrice(double price)
{
this._price = price;
}
public String toString()
{
return "Book [Title: " + _title + ", Author: " + _author + ", Pages: " + _pages + ", Price: $" + _price +"]\n";
}
}