Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in java eclipse IDE for a publisher. The Publisher class will ha

ID: 3704908 • Letter: W

Question

Write a program in java eclipse IDE for a publisher. The Publisher class will have a name, number of staff members, types of publications (books, magazines, videos). Book and Magazine are subclasses of Publisher. The instance variables of Book class are title, price, type (fiction or non-fiction), and year of publication. Magazine should have name of the magazine, type (monthly, weekly, yearly), and price per magazine. Each of the classes must have constructors, get() & set() methods, and toString() method. Book and Magazine classes must each have a charge() method that will accept either the number of books bought or number of subscriptions in the case of magazines and return the total charges. Book class must have a search() method which accepts a title and an array of Book objects, and prints a message as the book is available or not. Magazine class must also have a search() method that receives a type and an array of Magazine objects and print all the magazines with that type (if the type is weekly, it should print all the weekly magazines available from this publisher). Include a driver to test the classes. You should have two text files: one of the books and the other of magazines. The driver should read these into appropriate arrays. The main() should print a menu of all the choices. I will really appreciate if someone send me the text file along with it.

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

// Base class Publisher definition

class Publisher

{

// Instance variables to store data

String name;

int numberOfStaffMembers;

String typesOfPublications;// (books, magazines, videos)

// Default constructor definition

Publisher()

{

name = null;

numberOfStaffMembers = 0;

typesOfPublications = null;

}// End of default constructor

// Parameterized constructor to assign parameter data to instance variables

Publisher(String name, int numberOfStaffMembers, String typesOfPublications)

{

this.name = name;

this.numberOfStaffMembers = numberOfStaffMembers;

this.typesOfPublications = typesOfPublications;

}// End of parameterized constructor

// Method to set name

public void setName(String name)

{

this.name = name;

}// End of method

// Method to set number of members

public void setNumberOfStaffMembers(int numberOfStaffMembers)

{

this.numberOfStaffMembers = numberOfStaffMembers;

}

// Method to set type of publication

public void setTypesOfPublications(String typesOfPublications)

{

this.typesOfPublications = typesOfPublications;

}// End of method

// Method to return name

public String getName()

{

return name;

}// End of method

// Method to return number of members

public int getNumberOfStaffMembers()

{

return numberOfStaffMembers;

}// End of method

// Method to return type of publication

public String getTypesOfPublications()

{

return typesOfPublications;

}// End of method

// Overrides toString() method to return publisher information

public String toString()

{

return " Publisher Information Name: " + this.getName() +

" Number of Staff Members: " + this.getNumberOfStaffMembers() +

" Types of Publications: " + this.getTypesOfPublications();

}// End of method

}// End of class

// Class Book derived from Publisher class

class Book extends Publisher

{

// Instance variables to store data

String title;

double price;

String type; //(fiction or non-fiction)

int yearOfPublication;

// Default constructor definition

Book()

{

// Calls the base class default constructor

super();

title = null;

price = 0;

type = null;

yearOfPublication = 0;

}// End of default constructor

// Parameterized constructor to assign parameter data to instance variables

Book(String name, int number, String typesOfPublications, String title, double price, String type, int yearOfPublication)

{

// Calls the base class parameterized constructor

super(name, number, typesOfPublications);

this.title = title;

this.price = price;

this.type = type;

this.yearOfPublication = yearOfPublication;

}// End of parameterized constructor

// Method to set title

public void setTitle(String title)

{

this.title = title;

}// End of method

// Method to set price

public void setPrice(double price)

{

this.price = price;

}// End of method

// Method to set type of book

public void setType(String type)

{

this.type = type;

}// End of method

// Method to set the year of publication

public void setYearOfPublication(int yearOfPublication)

{

this.yearOfPublication = yearOfPublication;

}// End of method

// Method to return title

public String getTitle()

{

return title;

}// End of method

// Method to return price

public double getPrice()

{

return price;

}// End of method

// Method to return type of book

public String getType()

{

return type;

}// End of method

// Method to return year of publication

public int getYearOfPublication()

{

return yearOfPublication;

}// End of method

// Overrides toString() method to return publisher information

public String toString()

{

return super.toString() + " Book Information " + " Title: " + this.getTitle() +

" Price: " + this.getPrice() + " Type of Book: " + this.getType() +

" Year of Publication: " + this.getYearOfPublication();

}// End of method

// Method to receive number of books purchased

// Returns the amount to be paid

double charge(int no)

{

return (no * price);

}// End of method

// Method to receive the title of the book and array of Book objects

// Search and display book information if found

void search(String title, Book book[])

{

// Initializes the found status to zero for not found

int found = 0;

// Loops till length of the book

for(int c = 0; c < book.length; c++)

{

// Checks if the current object title is equals to the title in parameter

if(book[c].getTitle().equalsIgnoreCase(title))

{

// Set the found status to one for found

found = 1;

// Display the book information

System.out.print(" Book is available ");

System.out.print(book[c]);

// Come out of the loop

break;

}// End of if condition

}// End of for loop

// Checks if the found value is zero. Then book not found

if(found == 0)

System.out.print(" Book is not available");

}// End of method

}// End of class

// class Magazine derived from Publisher class

class Magazine extends Publisher

{

// Instance variables to store data

String nameOfMagazine;

String type; //(monthly, weekly, yearly)

double pricePerMagazine;

// Default constructor definition

Magazine()

{

// Calls the base class default constructor

super();

nameOfMagazine = null;

type = null;

pricePerMagazine = 0;

}// End of default constructor

// Parameterized constructor to assign parameter data to instance variables

Magazine(String name, int number, String typesOfPublications, String nameOfMagazine, String type, double pricePerMagazine)

{

// Calls the base class parameterized constructor

super(name, number, typesOfPublications);

this.nameOfMagazine = nameOfMagazine;

this.type = type;

this.pricePerMagazine = pricePerMagazine;

}// End of parameterized constructor

// Method to set name of magazine

public void setNameOfMagazine(String nameOfMagazine)

{

this.nameOfMagazine = nameOfMagazine;

}// End of method

// Method to set the type of magazine

public void setType(String type)

{

this.type = type;

}// End of method

// Method to set the price

public void setPricePerMagazine(double pricePerMagazine)

{

this.pricePerMagazine = pricePerMagazine;

}// End of method

// Method to return name of the magazine

public String getNameOfMagazine()

{

return nameOfMagazine;

}// End of method

// Method to return type of magazine

public String getType()

{

return type;

}// End of method

// Method to return price

public double getPricePerMagazine()

{

return pricePerMagazine;

}// End of method

// Overrides toString() method to return publisher information

public String toString()

{

return super.toString() + " Magazine Information " +

" Name of Magazine: " + this.getNameOfMagazine() +

" Type of Magazine: " + this.getType() +

" Price Per Magazine: " + this.getPricePerMagazine();

}// End of method

// Method to receive number of magazines purchased

// Returns the amount to be paid

double charge(int no)

{

return (no * pricePerMagazine);

}// End of method

// Method to receive the type of the magazine and array of Magazine objects

// Search and display magazine information if found

void search(String type, Magazine magazine[])

{

// Initializes the found status to zero for not found

int found = 0;

// Loops till length of the magazine

for(int c = 0; c < magazine.length; c++)

{

// Checks if the current object type is equals to the type in parameter

if(magazine[c].getType().equalsIgnoreCase(type))

{

// Set the found status to one for found

found = 1;

// Displays the magazine information

System.out.print(" Book is available ");

System.out.print(magazine[c]);

}// End of if condition

}// End of for loop

// Checks if the found value is zero. Then magazine not found

if(found == 0)

System.out.print(" Book is not available");

}// End of method

}// End of class

// Driver class PublisherDemo definition

public class PublisherDemo

{

// Static method to read book file

static int readFileBook(Book b[])

{

// To keep track of records read from file

int c = 0;

// try block begins

try

{

// Scanner class object created to open the file for reading

Scanner sc = new Scanner(new File("bookData.txt"));

// Loops till end of the file

while(sc.hasNext())

{

// Reads data from file and stores it in book objects c index position using setter method

b[c].setName(sc.next());

b[c].setNumberOfStaffMembers(sc.nextInt());

b[c].setTypesOfPublications(sc.next());

b[c].setTitle(sc.next());

b[c].setPrice(sc.nextDouble());

b[c].setType(sc.next());

b[c].setYearOfPublication(sc.nextInt());

// Increase the record counter by one

c++;

}// End of while loop

// Close the file

sc.close();

}// End of try

// Catch to handle FileNotFoundException exception

catch(FileNotFoundException fe)

{

// Displays error message

fe.printStackTrace();

}// End of catch

// Returns number of records

return c;

}// End of method

// Static method to read magazine file

static int readFileMagazine(Magazine m[])

{

// To keep track of records read from file

int d = 0;

// try block begins

try

{

// Scanner class object created to open the file for reading

Scanner sc = new Scanner(new File("magazineData.txt"));

// Loops till end of the file

while(sc.hasNext())

{

// Reads data from file and stores it in magazine objects d index position using setter method

m[d].setName(sc.next());

m[d].setNumberOfStaffMembers(sc.nextInt());

m[d].setTypesOfPublications(sc.next());

m[d].setNameOfMagazine(sc.next());

m[d].setType(sc.next());

m[d].setPricePerMagazine(sc.nextDouble());

// Increase the record counter by one

d++;

}// End of while

// Close the file

sc.close();

}// End of try

// Catch to handle FileNotFoundException exception

catch(FileNotFoundException fe)

{

// Displays error message

fe.printStackTrace();

}// End of catch

// Returns number of records

return d;

}// End of method

// main method definition

public static void main(String[] args)

{

// To store number of items purchased entered by the user

int no;

// To store title entered by the user to search

String title;

// Scanner class object created to accept data from console

Scanner sc = new Scanner(System.in);

// Book and Magazine array of objects declared

Book books[] = new Book[3];

Magazine magazines[] = new Magazine[3];

// Loops till number of records

for(int x = 0; x < 3; x++)

{

// Creates each object for books and magazines

books[x] = new Book();

magazines[x] = new Magazine();

}// End of for loop

// Calls the method to read book file

int numberOfBooks = readFileBook(books);

// Calls the method to read magazine file

int numberOfMagazines = readFileMagazine(magazines);

// Loops till number of books

for(int x = 0; x < numberOfBooks; x++)

{

// Display book information

System.out.println(books[x]);

// Accepts number of books purchased

System.out.print(" Enter number of books bought: ");

no = sc.nextInt();

// Calls the method to calculate amount to be paid

// Displays amount to be paid

System.out.println(" Amount to be paid: " + books[x].charge(no));

}// End of for loop

// Loops till number of magazines

for(int x = 0; x < numberOfMagazines; x++)

{

// Display magazine information

System.out.println(magazines[x]);

// Accepts number of magazines purchased

System.out.print(" Enter number of subscriptions: ");

no = sc.nextInt();

// Calls the method to calculate amount to be paid

// Displays amount to be paid

System.out.println(" Amount to be paid: " + magazines[x].charge(no));

}// End of for loop

// Accepts the title of the book

System.out.print(" Enter a title of the book: ");

title = sc.next();

// Calls the method to search the book

books[0].search(title, books);

// Accepts the type of magazine

System.out.print(" Enter a type of the magazine: ");

title = sc.next();

// Calls the method to search the magazine type

magazines[0].search(title, magazines);

}// End of main method

}// End of class

Sample Output:


Publisher Information
Name: Pyari
Number of Staff Members: 3
Types of Publications: book

Book Information

Title: C++
Price: 123.22
Type of Book: Function
Year of Publication: 2012

Enter number of books bought: 2

Amount to be paid: 246.44

Publisher Information
Name: Mohan
Number of Staff Members: 2
Types of Publications: book

Book Information

Title: Java
Price: 523.29
Type of Book: Non-Function
Year of Publication: 2017

Enter number of books bought: 0

Amount to be paid: 0.0

Publisher Information
Name: Sita
Number of Staff Members: 3
Types of Publications: video

Book Information

Title: DS
Price: 323.24
Type of Book: Non-Function
Year of Publication: 2015

Enter number of books bought: 1

Amount to be paid: 323.24

Publisher Information
Name: Suresh
Number of Staff Members: 4
Types of Publications: magazine

Magazine Information

Name of Magazine: Discovery
Type of Magazine: monthly
Price Per Magazine: 133.56

Enter number of subscriptions: 3

Amount to be paid: 400.68

Publisher Information
Name: Amit
Number of Staff Members: 2
Types of Publications: magazine

Magazine Information

Name of Magazine: Challenge
Type of Magazine: weekly
Price Per Magazine: 323.86

Enter number of subscriptions: 0

Amount to be paid: 0.0

Publisher Information
Name: Binod
Number of Staff Members: 2
Types of Publications: magazine

Magazine Information

Name of Magazine: Task
Type of Magazine: Yearly
Price Per Magazine: 223.89

Enter number of subscriptions: 1

Amount to be paid: 223.89

Enter a title of the book: OS

Book is not available
Enter a type of the magazine: Yearly

Book is available

Publisher Information
Name: Binod
Number of Staff Members: 2
Types of Publications: magazine

Magazine Information

Name of Magazine: Task
Type of Magazine: Yearly
Price Per Magazine: 223.89

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote