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

Write a java program for a publisher. The Publisher class will have a name, numb

ID: 3708252 • Letter: W

Question

Write a java program 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.

Explanation / Answer

package first.first;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

/**

* @author prayalankar

* skype at live:prayalankar_1

* whatasapp : +91 7877647725

*

*/

public class App {

public static void main(String[] args) throws IOException, NumberFormatException, ParseException {

App app = new App();

String filePath1 = "magazines.txt";

String filePath2 = "books.txt";

ArrayList<String[]> magazineValues = app.read(filePath1);

ArrayList<String[]> bookValues = app.read(filePath2);

// Adding books and magazines

ArrayList<Magazine> magazines = new ArrayList();

ArrayList<Book> books = new ArrayList();

for (String[] magazineValue : magazineValues) {

Magazine magazine = new Magazine(magazineValue[0], magazineValue[1], magazineValue[2], magazineValue[3],

Float.valueOf(magazineValue[4]), first.first.Magazine.Type.valueOf(magazineValue[0]));

magazines.add(magazine);

}

for (String[] bookValue : bookValues) {

Book book = new Book(bookValue[0], bookValue[1], bookValue[2], bookValue[3], Float.valueOf(bookValue[4]),

bookValue[5], new SimpleDateFormat("dd/MM/yyyy").parse(bookValue[6]));

books.add(book);

}

}

public ArrayList<String[]> read(String filePath) throws IOException {

File file = new File(filePath);

ArrayList<String[]> values = new ArrayList();

BufferedReader br = new BufferedReader(new FileReader(file));

String st;

while ((st = br.readLine()) != null) {

values.add(st.split(","));

}

return values;

}

}

package first.first;

public class Publisher {

private String publisherName;

private String staffNumber;

private String publicationType;

public Publisher(String name, String staffNumber, String publicationType) {

this.publisherName = name;

this.staffNumber = staffNumber;

this.publicationType = publicationType;

}

public String getPublisherName() {

return publisherName;

}

public void setPublisherName(String name) {

this.publisherName = name;

}

public String getStaffNumber() {

return staffNumber;

}

public void setStaffNumber(String staffNumber) {

this.staffNumber = staffNumber;

}

public String getPublicationType() {

return publicationType;

}

public void setPublicationType(String publicationType) {

this.publicationType = publicationType;

}

@Override

public String toString() {

return "Publisher [name=" + publisherName + ", staffNumber=" + staffNumber + ", publicationType=" + publicationType

+ ", getName()=" + getPublisherName() + ", getStaffNumber()=" + getStaffNumber() + ", getPublicationType()="

+ getPublicationType() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="

+ super.toString() + "]";

}

}

package first.first;

import java.util.ArrayList;

import java.util.Date;

public class Book extends Publisher {

private String title;

private float price;

private String type;

private Date publicationYear;

public Book(String publisherName, String staffNumber, String publicationType, String bookTitle, float price,

String type, Date publicationYear) {

super(publisherName, staffNumber, publicationType);

this.title = bookTitle;

this.price = price;

this.type = type;

this.publicationYear = publicationYear;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public float getPrice() {

return price;

}

public void setPrice(float price) {

this.price = price;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public Date getPublicationYear() {

return publicationYear;

}

public void setPublicationYear(Date publicationYear) {

this.publicationYear = publicationYear;

}

public float charge(float numberOfBook, Book book) {

return numberOfBook * book.getPrice();

}

public void search(String title, ArrayList<Book> books) {

int flag = 0;

for (Book book : books) {

if (book.getTitle().equals(title)) {

System.out.println("Book is Available");

flag = 1;

break;

}

}

if (flag == 0) {

System.out.println("Sorry book is not availbale");

}

}

@Override

public String toString() {

return "Books [title=" + title + ", price=" + price + ", type=" + type + ", publicationYear=" + publicationYear

+ "]";

}

}

package first.first;

import java.util.ArrayList;

public class Magazine extends Publisher {

private String name;

private float price;

private Type type;

public Magazine(String publisherName, String staffNumber, String publicationType,

String magazineName, float price,

Type type) {

super(publisherName, staffNumber, publicationType);

this.name = magazineName;

this.price = price;

this.type = type;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public float getPrice() {

return price;

}

public void setPrice(float price) {

this.price = price;

}

public Type getType() {

return type;

}

public void setType(Type type) {

this.type = type;

}

public enum Type {

MONTHLY, WEEKLY, YEARLY;

}

public float charge(float numberOfSubscription, Magazine magazine) {

return numberOfSubscription * magazine.getPrice();

}

public void search(Type type, ArrayList<Magazine> magazines) {

int flag = 0;

for (Magazine magazine : magazines) {

if (magazine.getType().equals(type)) {

flag = 1;

System.out.println(magazine.toString());

}

}

if (flag == 0) {

System.out.println("Sorry this Type of magazine is not availbale");

}

}

@Override

public String toString() {

return "Magazines [name=" + name + ", price=" + price + ", type=" + type + "]";

}

}

// it has three required classes and one driver class APP

// if the book.txt and magazine.txt dont match please ping or change driver accordingly

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