Answer:
Check the explanation
Explanation:
WriteData.java:
import java.io.FileWriter;
import java.io.PrintWriter;
class WriteData {
String[][] data;
public WriteData() {
// Data to write to the file
data = new String[][]{{ "Fiction", "Abraham Lincoln Vampire Hunter", "Grahame-Smith", "Wiley", "NY", "13.99", "222"},
{"Fiction", "Frankenstein", "Shelley", "Prescott", "GA", "7.99", "321"},
{"NonFiction", "Life of Kennedy", "Jones", "Pearson", "MT", "12.90", "biography"},
{"Fiction", "Dracula", "Stoker", "Addison", "CA", "5.99", "145"},
{"Fiction", "Curse of the Wolfman", "Hageman", "Wesley", "MA", "10.59", "876"},
{"NonFiction", "How to Pass Java", "Willis", "Wiley", "NY", "1.99", "technology"},
{"Fiction", "The Mummy", "Rice", "Addision", "CA", "7.99", "954"},
{"NonFiction", "History of Texas", "Smith", "Prescott", "CA", "9.75", "history"}};
}
public void writeToFile() {
try {
PrintWriter pw = new PrintWriter(new FileWriter("books.txt")); // Creating an output stream to write to file
// Writing data to the file line by line
for (int i = 0; i < 8; i++) {
pw.println(data[i][0] + ";" + data[i][1] + ";" + data[i][2] + ";" + data[i][3] + ";" + data[i][4] + ";" + data[i][5] + ";" + data[i][6]);
}
pw.close(); // Closing the created output stream
}
catch(Exception e) {
System.err.println(e);
}
}
}
Publisher.java:
class Publisher {
private String name, state;
public Publisher(String name, String state) {
this.name = name;
this.state = state;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String toString() {
return name + ";" + state;
}
}
Book.java:
class Book {
private String title, author;
private double price;
private Publisher publisher;
public Book(String title, String author, Publisher publisher, double price) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public Double calculateCharge(int Qty){
return getPrice()*Qty;
}
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 String getPublisher() {
return publisher.toString();
}
public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
FictionBook.java:
class FictionBook extends Book {
String fictionCode;
public FictionBook(String title, String author, Publisher publisher, double price, String fictionCode) {
super(title, author, publisher, price); // Calling the parent constructor to initialize values
this.fictionCode = fictionCode;
}
// Getter and Setter
public String getFictionCode() {
return fictionCode;
}
public void setFictionCode(String fictionCode) {
this.fictionCode = fictionCode;
}
}
NonFictionBook.java:
class NonFictionBook extends Book {
String categoryCode;
public NonFictionBook(String title, String author, Publisher publisher, double price, String categoryCode) {
super(title, author, publisher, price); // Calling the parent constructor to initialize values
this.categoryCode = categoryCode;
}
// Getter and Setter
public String getCategoryCode() {
return categoryCode;
}
public void setCategoryCode(String categoryCode) {
this.categoryCode = categoryCode;
}
}
BookTest.java:
import java.util.Scanner;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class BookTest {
public static void main(String[] args) {
// TODO code application logic here
// Writing data to the file using WriteData.java program
WriteData writeData = new WriteData();
writeData.writeToFile();
int[] BookQauntity = {12, 8, 3, 53, 7, 23, 14, 5, 6};
Book[] book = new BookTest().buildInstances();
Double GrandTotal= new BookTest().createCharges(BookQauntity, book);
System.out.println("GrandTotal : "+ GrandTotal);
}
catch(FileNotFoundException e) {
System.err.println("File not found");
}
return bk;
};
// ISBN info is removed
Double createCharges(int[] BookQuantity, Book[] book){
Double Gtotal =0.0, total=0.0;
for(int i=0; i<8; i++){
total = book[i].calculateCharge(BookQuantity[i]);
System.out.println("Title : "+ book[i].getTitle() + ", Total Charge : "+ total);
Gtotal +=total;
}
return Gtotal;
};
}