Question

Given the attached data file, write a payload class Book that represents a book. The fields...

Given the attached data file, write a payload class Book that represents a book.

The fields are id, title, publisher, type, price and paperback. Most should be String fields, but price should be a double and paperback should be a boolean.

The one constructor should take a Scanner parameter opened to the file. It should input each of the fields using the Scanner. Note that for the paperback field, you should input a String, and set the field to true or false according to whether the String is "Y" or not.

The toString method should print out the title, type, price and paperback fields as in the sample output below. Use a DecimalFormat object to format the price, and print out "paperback" or "hardback" based on the value of the paperback field.

Write a drive in a separate file.

The driver should declare an ArrayList of Book objects and instantiate a Scanner for the attached books.txt, then use it to instantiate and add new books to the list as long as hasNextLine() is true for the Scanner. Then, print out each of the books.

A Deepness in the Sky (SFI) $7.19 paperback

Magic Terror (HOR) $7.99 paperback

The Stranger (FIC) $8.00 paperback

Venice (ART) $24.50 hardback

Second Wind (MYS) $24.95 hardback

The Edge (MYS) $6.99 paperback

Dreamcatcher: A Novel (HOR) $19.60 hardback

Treasure Chests (ART) $24.46 hardback

Beloved (FIC) $12.95 paperback

Harry Potter and the Prisoner of Azkaban (SFI) $13.96 hardback

Van Gogh and Gauguin (ART) $21.00 hardback

Of Mice and Men (FIC) $6.95 paperback

Electric Light (POE) $14.00 hardback

Group: Six People in Search of a Life (PSY) $10.40 paperback

Nine Stories (FIC) $5.99 paperback

The Soul of a New Machine (SCI) $11.16 paperback

Travels with Charley (TRA) $7.95 paperback

Catch-22 (FIC) $12.00 paperback

Jazz (FIC) $12.95 paperback

Band of Brothers (HIS) $9.60 paperback

A Guide to SQL (CMP) $37.95 paperback

Franny and Zooey (FIC) $5.99 paperback

East of Eden (FIC) $12.95 paperback

Harry Potter and the Goblet of Fire (SFI) $18.16 hardback

The Fall (FIC) $8.00 paperback

Godel Escher Bach (PHI) $14.00 paperback

When Rabbit Howls (PSY) $6.29 paperback

Black House (HOR) $18.81 hardback

Song of Solomon (FIC) $14.00 paperback

The Grapes of Wrath (FIC) $13.00 paperback

Slay Ride (MYS) $6.99 paperback

The Catcher in the Rye (FIC) $5.99 paperback

To Kill a Mockingbird (FIC) $18.00 hardback

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included the code below. The Book class reads the inputs using the Scanner object. The DriverClass creates and ArrayList of type Book and prints the books. The books.txt file should be in the same folder as the driver class. If not, then change the file url in File class instantiation.

// This is the Book class

import java.io.*;
import java.util.Scanner;
import java.text.DecimalFormat;

/*
   -> Book constructor need a parameter Scanner object
   -> the "tostring" method prints the book details
*/

class Book
{
   String id, title, publisher, type;
   double price;
   boolean paperback;
  
   Book(Scanner sc)
   {
       id = sc.nextLine();
       title = sc.nextLine();
       publisher = sc.nextLine();
       type = sc.nextLine();
       price = Double.parseDouble(sc.nextLine());
       paperback = sc.nextLine().substring(0,1).compareTo("Y")==0;
   }
  
   void tostring()
   {
       DecimalFormat df = new DecimalFormat("$#.00");
       System.out.println(title+" ("+type+") "+df.format(price)+" "+((paperback)?"paperback":"hardback"));
   }
}

// From here Driver class

import java.io.*;
import java.util.*;

/*
   -> ArrayList books contains a list of Book objects
   -> the books.txt file should be in the same folder as the drive class
*/

class DriverClass
{
   public static void main(String args[])
   {
       ArrayList<Book> books = new ArrayList<Book>();

//change the url to the location of the books.txt
       File file = new File("books.txt");
      
       try
       {
           Scanner sc = new Scanner(file);
           while(sc.hasNextLine()) books.add(new Book(sc));
       }
       catch(Exception e) System.out.println("can't find the file or scanner exception");
      
       for(Book b : books) b.tostring();
   }
}

Add a comment
Know the answer?
Add Answer to:
Given the attached data file, write a payload class Book that represents a book. The fields...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • are Each question is worth 10 points. apply 1. List the book code and book title...

    are Each question is worth 10 points. apply 1. List the book code and book title of each book that has the type HOR 2. List the book code and book title of each book that has the type HOR or is published by the publisher with the publisher code SC 3. List the book code and book title of each book that has the type MYS and a price of less than $20 4. List the book code, book...

  • are Each question is worth 10 points. apply 1. List the book code and book title...

    are Each question is worth 10 points. apply 1. List the book code and book title of each book that has the type HOR 2. List the book code and book title of each book that has the type HOR or is published by the publisher with the publisher code SC 3. List the book code and book title of each book that has the type MYS and a price of less than $20 4. List the book code, book...

  • How would you use this stored procedure to change the price of any copy of book 0180 whose format...

    How would you use this stored procedure to change the price of any copy of book 0180 whose format is paperback to $10.95? using MYSQL 8.0 Command line Client This is the database script for the homework above CREATE DATABASE HENRY; USE HENRY; CREATE TABLE AUTHOR (AUTHOR_NUM DECIMAL(2,0) PRIMARY KEY, AUTHOR_LAST CHAR(12), AUTHOR_FIRST CHAR(10) ); CREATE TABLE BOOK (BOOK_CODE CHAR(4) PRIMARY KEY, TITLE CHAR(40), PUBLISHER_CODE CHAR(3), TYPE CHAR(3), PRICE DECIMAL(4,2), PAPERBACK CHAR(1) ); CREATE TABLE BRANCH (BRANCH_NUM DECIMAL(2,0) PRIMARY KEY,...

  • MYSQL Questions: 1.For every author, display the number of books written by that author that is...

    MYSQL Questions: 1.For every author, display the number of books written by that author that is carried by Henry Books. Display the author number as ‘Author Number’, author name concatenated (first last) as ‘Author Name’, and the total number of books written by the author as ‘Number of Titles by Author’. List in descending order by author number. Limit the output to 10 rows. Insert your snip of the query and resultset together here. 2.Using a function, what are the...

  • MYSQL: Question: 1.Using a function, what are the lowest price books found in paperback? Only return...

    MYSQL: Question: 1.Using a function, what are the lowest price books found in paperback? Only return the lowest price books. Display the book code as ‘Book Code’, the book title as ‘Title’, author name concatenated (first last) as ‘Author Name’ and price as ‘Price’. Insert your snip of the query and resultset together here. 2. Display the number of publishers by city.   Display the city as ‘City’ and the number of publishers in that city as ‘Number of Publishers’. Insert...

  • *Java* Given the attached Question class and quiz text, write a driver program to input the...

    *Java* Given the attached Question class and quiz text, write a driver program to input the questions from the text file, print each quiz question, input the character for the answer, and, after all questions, report the results. Write a Quiz class for the driver, with a main method. There should be a Scanner field for the input file, a field for the array of Questions (initialized to 100 possible questions), and an int field for the actual number of...

  • Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class,...

    Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class, Write a class named Retailltem that holds data about an item in a retail store. The class should have the following fields description: The description field is a String object that holds a brief description of the item . unitsOnHand: The unitsOnHand field is an int variable that holds the number of units currently in inventory Price: The price field is a double that...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT