Question

In: Computer Science

Add the following private attributes: String publisher String title String ISBN String imageName double price Create...

Add the following private attributes: String publisher

String title
String ISBN
String imageName double price

Create getter/setter methods for all data types

Create a constructor that takes in all attributes and sets them

Remove the default constructor

Override the toString() method and return a String that represents the book object that is

formatted nicely and contains all information (attributes)

In the main class:

Create a main method that throws FileNotFoundException

Import java.io libraries

Import java.util.Scanner

Create a loop to read the file

Add and ArrayList to your main program to hold your books be sure to import java.util.ArrayList

Within your loop (each iteration)

Read a line of data into variables for publisher, title, ISBN, image name and price.

Instantiate a "Book" object using the constructor you created sending in the

information read from the file.

Add the book into the array list

Once your loop is complete, use a for each loop to print each book (using the book’s toString() method) in the ArrayList to the console.

Solutions

Expert Solution

Book.java


public class Book {
   //private attributes
   private String publisher;
   private String title;
   private String ISBN;
   private String imageName;
   private double price;

   //getter methods
   //method that returns publisher
   public String getPublisher()
   {
       return publisher;
   }

   //method that returns title
   public String getTitle()
   {
       return title;
   }

   //method that returns ISBN
   public String getISBN()
   {
       return ISBN;
   }

   //method the returns imageName
   public String getImageName()
   {
       return imageName;
   }

   //method that returns price
   private double getPrice()
   {
       return price;
   }

   //setter methods
   //method that sets publisher
   public void setPublisher(String strPublisher)
   {
       this.publisher = strPublisher;
   }

   //method that sets title
   public void setTitle(String strTitle)
   {
       this.title = strTitle;
   }

   //method that sets ISBN
   public void setISBN(String strISBN)
   {
       this.ISBN = strISBN;
   }

   //method the sets imageName
   public void setImageName(String strImageName)
   {
       this.imageName = strImageName;
   }

   //method that sets price
   private void setPrice(double dblPrice)
   {
       this.price = dblPrice;
   }

   //constructor that takes in all attributes and sets them
   public Book(String strPublisher, String strTitle, String strISBN, String strImageName, double dblPrice)
   {
       setPublisher(strPublisher);
       setTitle(strTitle);
       setISBN(strISBN);
       setImageName(strImageName);
       setPrice(dblPrice);
   }

   //override toString method
   public String toString()
   {
       return "Publisher: " + getPublisher() + "\tTitle: " + getTitle() + "\tISBN: " + getISBN()
       + "\tImage Name: " + getImageName() + "\tPrice: $" + getPrice();
   }
}

Main.java

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
   public static void main(String[] args) throws NumberFormatException, IOException {
       //Create object to the scanner class
       Scanner input= new Scanner(System.in);
       //read the filename
       String fileName;
       System.out.print("Enter the name of the file: ");
       fileName = input.nextLine();
       //create an arraylist to hold books
       ArrayList<Book> listOfBooks = new ArrayList<Book>();
       //create an object for BufferedReader class to read the file
       BufferedReader reader = new BufferedReader(new FileReader(fileName));
       //variable declarations
       String line;
       String publisher;
       String title;
       String ISBN;
       String imageName;
       double price;
       //run a loop to read the file until the end of the file is reached
       while ((line = reader.readLine()) != null) {
           //since we might have names containing spaces i used ','(comma) as delimiter in the text file
           String[] info = line.split(",");
           //store book information in their respective variables
           publisher = info[0];
           title = info[1];
           ISBN = info[2];
           imageName = info[3];
           price = Double.parseDouble(info[4]);
           //create Book object
           Book newBook = new Book(publisher, title, ISBN, imageName, price);
           //add book to the arraylist
           listOfBooks.add(newBook);
       }
       //close the reader
       reader.close();
       //print the book array list using for loop
       System.out.println("\nList Of Books read: ");
       for(int i = 0; i < listOfBooks.size(); i++)
           System.out.println(listOfBooks.get(i).toString());
       input.close();
   }
}

Output:

Program Screenshots

Text file used:


Related Solutions

The most common attributes of a book are the Book Title, and ISBN. The most common...
The most common attributes of a book are the Book Title, and ISBN. The most common functions are to set the Book Title, and ISBN, Write the code to implement this problem. 1. Write the UML Diagram that represents this class Book 2. Use code blocks editor and in C++ Write a header file Book with these properties. Write the implementation file for the member functions.
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition,...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition, price), PK: ISBN, FK: published_by refs Publisher, previous_edition refs Book. Author (SSN, first_name, last_name, address, income), PK: SSN. Write (aSSN, bISBN), PK: (aSSN, bISBN), FK: aSSN refs Author, bISBN refs Book. Editor (SSN, first_name, last_name, address, salary, works_for, book_count), PK: SSN, FK: works_for refs Publisher. Edit (eSSN, bISBN), PK: (eSSN, bISBN), FK: eSSN refs Editor, bISBN refs Book. Author_Editor (aeSSN, hours), PK: aeSSN, FK:...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition,...
Consider the following schema: Publisher (name, phone, city), PK: name. Book (ISBN, title, year, published_by, previous_edition, price), PK: ISBN, FK: published_by refs Publisher, previous_edition refs Book. Author (SSN, first_name, last_name, address, income), PK: SSN. Write (aSSN, bISBN), PK: (aSSN, bISBN), FK: aSSN refs Author, bISBN refs Book. Editor (SSN, first_name, last_name, address, salary, works_for, book_count), PK: SSN, FK: works_for refs Publisher. Edit (eSSN, bISBN), PK: (eSSN, bISBN), FK: eSSN refs Editor, bISBN refs Book. Author_Editor (aeSSN, hours), PK: aeSSN, FK:...
Create a class tuck shop with following data members: String Owner String Food_Items[100] Double Price [100]...
Create a class tuck shop with following data members: String Owner String Food_Items[100] Double Price [100] Int Quantity [100]                Note: All arrays are open ended. It is not necessary that they are completely filled. All three arrays will work in synchronization with each other i-e the item at index 0 will have its price in Price array at index 0 and quantity at index 0 of Quantity array. Methods: Two Constructors (default, four-argument) set for Owner , get for...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Consider the following statements: #include #include class Temporary { private: string description; double first; double second;...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second; public: Temporary(string = "", double = 0.0, double = 0.0); void set(string, double, double); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); }; Write the definition of the member function set() so that the instance variables are set according to the parameters. Write the definition of the constructor so that it initializes the instance variables using the function set() Write...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Here is the assignment description. * Create a class named 'Account' having the following private attributes...
Here is the assignment description. * Create a class named 'Account' having the following private attributes int accountNumber; double balance; * Write a constructor with parameters for each of the attributes. * Write another constructorwith one parameter for the accountNumber. * Write getter and setter methods for each of the private attributes. * Write a method void credit(double amount) which adds the given amount to the balance. * Write a method void debit(double amount) which subtracts the given amount from...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT