1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Andrei [34K]
3 years ago
11

1. Write program, WriteData.java, that writes the following data to file books.txt. Include the semicolons. Fiction;Abraham Linc

oln 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 2. Write class Publisher with attributes name and state. 3. Rewrite the Book class to include a type Publisher attribute. 4. Write two children of the Book class: FictionBook and NonFictionBook. FictionBook has an additional attribute, fictionCode. NonFictionBook has an additional attribute, catagoryCode. 5. Rewrite the BookTest program. Method buildInstances will read the data from the file, create instances of FictionBook and NonfictionBook from the data, assign these instances to the Book array and return the bookArray as before. Since it is reading data from the file, it does not have any method parameters. 6. Method createCharges should work the same.
Computers and Technology
1 answer:
Amiraneli [1.4K]3 years ago
7 0

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;

  };

}

You might be interested in
4.2 lesson practice help plzs
sertanlavr [38]

The output is 21 because c is incremented by 3 until it surpasses 10 and the value of c is added to sum each time the loop runs.

I hope this helps!

8 0
2 years ago
Which of the following is NOT an option in the comments group
IgorLugansk [536]
I need the options to help you out, thanks. :)
4 0
3 years ago
Suppose that a disk drive has 5,000 cylinders, numbered 0 to 4,999. The drive is currently serving a request at cylinder 2,050,
cluponka [151]

Answer:

a. 18102

b. 5656

c. 6090

Explanation:

Pleaae kindly check attachment for the detailed and step by step solution of the given problem.

7 0
2 years ago
I need subscribers plz ​
melamori03 [73]

Answer:

No :) here this is my channel name jshdhebejdb

8 0
2 years ago
Read 2 more answers
Sami needs to decide how the fonts, colors, and images will look on her new web site. Which will help her pian her design?
shtirl [24]
Maybe a sketchbook:))))
6 0
2 years ago
Other questions:
  • What happens when the computer is thrashing? quizzlet?
    12·1 answer
  • What are the main differences between a workgroup and a domain?
    14·2 answers
  • Describe any four rights of users of information systems.
    8·1 answer
  • Which of the following are types of home internet service? Check all that apply
    7·1 answer
  • What is the importance of Mathematical Modeling in the field of bioinformatics.
    8·1 answer
  • Which tabs contains options for adding shapes to a chart ? Check all that apply.
    13·1 answer
  • Fred wants to analyze his spending habits of the past few years and has gathered information on the checks he has written from 2
    6·1 answer
  • Suppose you're currently completing an examination online. When you're finished, you click on Reset Exam. Why would you do this?
    8·1 answer
  • Create a dictionary password cracker or a brute force
    13·1 answer
  • ________ of Willa Catha present an unadorned picture oflife on the prairies of the Midwestern United States during the19th centu
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!