Setup a class to store a book. The data is to include title, author, year of pub
ID: 3641709 • Letter: S
Question
Setup a class to store a book. The data is to include title, author, year of publication, dewey number and age. The class should include functions to read title, author, year and dewey number from a file, to determine the age of the book and to display that book’s details on screen. You may require other functions, that is for you to decide.The main program should contain an array of book objects which are used in conjunction with the file to load all books. See below for list of books. Your program should handle up to 20 books.
In this exercise it is expected you will require at least two non-class functions to calculate the age and to display all the results.
Moby Dick
Herman Melville
1851
501.234
The Wind in the Willows
Kenneth Grahame
1908
396.23
Wuthering Heights
Emily Bronte
1846
432.11
Jane Eyre
Charlotte Bronte
1847
521.34
Biggles of the Camel Squadron
Capt W.E. Johns
1934
389.34
The Old Man and the Sea
Ernest Hemmingway
1952
345.32
Catch 22
Joseph Heller
1955
376.66
Nineteen Eight-Four
George Orwell
1949
378.33
The War of the Worlds
H.G. Wells
1898
345.66
SlaughterHouse 5
Kurt Vonnegut
1969
456.77
The Gulag Archipelago
Alexander Solzhenitsyn
1973
388.99
Sons and Lovers
D.H. Lawrence
1913
324.78
I, Robot
Isaac Asimov
1956
453.22
Explanation / Answer
// Note: This program works. Due to insufficient time, I could not make the
// design as good as I wanted. Anyway, put all the java files and text files in
// the same folder, compile the code and run it.
import java.util.*;
public class Book
{
private String title;
private String author;
private int yearOfPublication;
private String deweyNumber;
private int age;
//Constructors
Book(){}
Book(String title, String author, int yearOfPublication, String deweyNumber)
{
this.title = title;
this.author = author;
this.yearOfPublication = yearOfPublication;
this.deweyNumber = deweyNumber;
Calendar c1 = Calendar.getInstance();
Date d1 = new Date();//Get current time.
c1.setTime(d1); //Store that time in a calendar object.
this.age = c1.get(Calendar.YEAR) - yearOfPublication;
}
Book(String title, String author, int yearOfPublication, String deweyNumber, int age)
{
this.title = title;
this.author = author;
this.yearOfPublication = yearOfPublication;
this.deweyNumber = deweyNumber;
this.age = age;
}
//setters
public void setTitle(String title){ this.title = title;}
public void setAuthor(String author){ this.author = author;}
public void setYearOfPublication(int yearOfPublication)
{
if(yearOfPublication >= 0)
{
this.yearOfPublication = yearOfPublication;
}
}
public void setDeweyNumber(String deweyNumber){this.deweyNumber = deweyNumber;}
public void setAge(int age)
{
if(age >= 0)
{
this.age = age;
}
}
//getters
public String getTitle(){return title;}
public String getAuthor(){return author;}
public int getYearOfPublication(){return yearOfPublication;}
public String getDeweyNumber(){return deweyNumber;}
public int getAge(){return age;}
//This method is called when you try to print the details of a book
//using System.out.println(aReferenceToABookObject);
public String toString()
{
String details = " Title: " + title + " Author: " + author + " Year: " + yearOfPublication + " Dewey Number: " + deweyNumber + " Age: " + age;
return details;
}
}
_____________________________________________________________________________
//The library
import java.io.*;
import java.util.*;
public class Library
{
static ArrayList<String> allLines = new ArrayList<String>();//For internal stuff.
static int num = 0;// number of books in library;
static Book[] books = new Book[20];
public static void main(String[]args)
{
System.out.println("Welcom to the library!");
try{
// Open the file.
FileInputStream fstream = new FileInputStream("BookList.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read the file line by line
while ((strLine = br.readLine()) != null)
{
allLines.add(strLine);
if (allLines.size() > 4)
{
makeBook();
allLines.clear();
}
}
//Close the input stream
in.close();
}catch (Exception e){System.err.println("Error: " + e.getMessage());}
//Display all the books
for(int i = 0; i < books.length; i++)
{
if(books[i] != null)
{
System.out.println(books[i]);
}
}
}
public static void makeBook()
{
String d1 = "";
String d2 = "";
int d3 = 0;
String d4 = "";
for(int i = 0; i < 4 ; i++)
{
switch(i)
{
case 0:
d1 = allLines.get(0);
break;
case 1:
d2 = allLines.get(1);
break;
case 2:
d3 = Integer.parseInt(allLines.get(2));
break;
case 3:
d4 = allLines.get(3);
break;
}
}
if(num < books.length)
{
books[num] = new Book(d1,d2,d3,d4);
num++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.