Using the data array, create instances of books and store them in a one-dimensional array of
Book
, calledbookArray
. Using the quantity array, walk through the book array and calculate the total charge per book sold. Print in a dialog box the book title, the book isbn, and the total charge for each book. Then, print the grand total of all books sold.
So far I have done this, but I'm having issue with the JOptionPane
and formatting.
Error
$ javac BookTest.java
BookTest.java:41: error: incompatible types: String cannot be converted to String[][]
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
^
1 error
And the code:
import javax.swing.JOptionPane;
public class BookTest {
public static void main(String args[]) {
String dataArray [][]=
{{ "Fiction", "Abraham Lincoln Vampire Hunter", "Grahame-Smith", "Wiley", "NY", "978-0446563079", "13.99", "222"},
{"Fiction", "Frankenstein", "Shelley", "Prescott", "GA", "978-0486282114", "7.99", "321"},
{"NonFiction", "Life of Kennedy", "Jones", "Pearson", "MT", "758-29304566", "12.90", "biography"},
{"Fiction", "Dracula", "Stoker", "Addison", "CA", "978-0486411095","5.99", "145"},
{"Fiction", "Curse of the Wolfman", "Hageman", "Wesley", "MA", "B00381AKHG", "10.59", "876"},
{"NonFiction", "How to Pass Java", "Willis", "Wiley"," NY", "444-395869790", "1.99", "technology"},
{"Fiction", "The Mummy", "Rice", "Addision", "CA", "978-0345369949", "7.99", "954"},
{"NonFiction", "History of Texas", "Smith", "Prescott", "CA", "123-683947687", "9.75", "history"}
};
Book bookArray[] = new Book [8];
for (int i = 0; i < bookArray.length; i++) {
if (dataArray[i][0].equals("Fiction")) {
Publisher p = new Publisher(dataArray[i][3], dataArray[i][4]);
bookArray[i] = new Fiction(dataArray[i][1], dataArray[i][2], dataArray[i][5],
p, Double.parseDouble(dataArray[i][6]), dataArray[i][7]);
} else {
Publisher p = new Publisher(dataArray[i][3], dataArray[i][4]);
bookArray[i] = new NonFiction(dataArray[i][1], dataArray[i][2], dataArray[i][5],
p, Double.parseDouble(dataArray[i][6]), dataArray[i][7]);
}
}
for (Book b:bookArray) {
dataArray +=String.format(" %s, %s ", b.getTitle(), b.getIsbn());
JOptionPane.showMessageDialog(null, dataArray);
}
}
}
Don't use your dataArray
to output things in the message box. Use a String
variable.
for (Book b : bookArray) {
String bookOutput = String.format(" %s, %s ", b.getTitle(), b.getIsbn());
JOptionPane.showMessageDialog(null, bookOutput);
}
Even better, add a toString()
to your Book
class that returns String.format(" %s, %s ", getTitle(), getIsbn())
and then do your loop like this:
for (Book b : bookArray) {
JOptionPane.showMessageDialog(null, b.toString());
}
However, if you want to output the price for each book and then the total, your loop will look more like this:
double totalPrice = 0.0;
for (int i = 0; i < bookArray.length; i++) { //can't use foreach because of use of parallel arrays bookArray & quantityArray
Book b = bookArray[i];
double price = b.getPrice() * quantityArray[i];
totalPrice += price;
String bookStr = String.format(" %s, %s %s", b.getTitle(), b.getIsbn(),
DecimalFormat.getCurrencyInstance().format(price));
JOptionPane.showMessageDialog(null, bookStr);
}
String totalStr = String.format("Total Charge: %s",
DecimalFormat.getCurrencyInstance().format(totalPrice)));
JOptionPane.showMessageDialog(null, totalStr);