Help with Java code, it is partially complete, but just needs a bit of stuff fil
ID: 3598713 • Letter: H
Question
Help with Java code, it is partially complete, but just needs a bit of stuff filled in. Thanks!
Assignment8.java(More code need to be added)
Book.java(it needs to be modified for this assignment)
Publication.java(it needs to be modified for this assignment)
BookComparator.java
Sorts.java
BookManagement.java
Skills to be Applied
In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:
Sorting
Searching Aggregate Objects Interfaces Serialization
File read/write
Program Description Class Diagram:
Publication
The Publication class implements the "Serializable" interface so that its object can
be stored. (The Serializable interface is defined in the "java.io" package.)
Book
The Book class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.) It needs to define the following method:
public void copy(Book other)
It needs to copy every member variable value from "other" parameter to their corresponding variable of the object itself using "this" reference.
BookComparator
The BookComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method that was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define:
public int compare(Book first, Book second)
instead by making the class implements Comparator.
If the first argument object has an author lexicographically less than that of the second argument, an int less than zero is returned. If the first argument object has an author lexicographically larger than that of the second argument, an int greater than zero is returned. If their authors are same, then their titles should be compared. (if the first argument object has a title lexicographically less than that of the second argument, an int less than zero is returned. If the first argument object has a title lexicographically larger than that of the second argument, an int greater than zero is returned). If their authors and titles are same, then their version should be compared (if the first argument object has a version smaller than that of the second argument, an int less than zero is returned. If the first argument object has a version larger than that of the second argument, an int greater than zero is returned). If they have same authors, titles and versions, then 0 should be returned.
Sorts
The Sorts class is a utility class that will be used to sort a list of Book objects. Sorting algorithms are described in the “Lecture Slides” tab of the Blackboard. These algorithms sort numbers stored in an array. It is your job to modify it to sort an array of objects.
The Sorts class object will never be instantiated. It must have the following methods:
public static void sort(Book[] bookList, int size, Comparator)
Your sort method utilizes the compare method of the parameter Comparator object to sort. You can use one of Selection sort or Insertion Sort. The parameter size specifies how many first elements should be sorted. Note that not all elements in the array should be sorted, since some of them will be null pointers.
BookManagement
The BookManagement class has a list of Book objects that can be organized at the book management system. The book management system will be a fully encapsulated object. The BookManagement class implements the Serializable interface.
It has the following attributes:
Attribute name
Attribute type
Description
bookList
an array of Book objects
A list of Book objects in the book management system
currentBooksCount
int
the number of Book objects created and stored in the bookList
maxSize
int
the maximum number of Book objects that can be stored in the bookList array. It is also the size of the bookList array.
The following public methods should be provided to interact with the book management system:
Method
Description
BookManagement(int maximumsize)
A Constructor of the BookManagement class. Using the parameter value, it should initialize the member variable maxSize. Then it should instantiate an array of Book objects using the maxSize, and initialize each slot of the array to null for every index. It should also initialize the member variable currentBooksCount to 0.
int bookExists(String author, String title, int version)
Search for a Book object by author and title, and return the index of the book object if found. Return -1 if not found. The parameters are author and title of a Book object.
boolean addBook(String author, String title, int version, String ISBN, double price)
Add a Book object to the book list and return true if such object was added successfully. Return false if an object with the same author, title, version, ISBN and price already exists or currentBooksCount is already same as maxSize, i.e., the array is full (the new object is not added).
boolean removeBook(String author, String title, int version)
Remove a Book object from the book list. Return true if the object was removed successfully. Return false if the object with the given author, title and version does not exist.
void sortBooks()
Sort the list of Book objects by their information including its author, title and version. This method calls the sort method defined in the Sorts class, using an object of BookComparator class as its second parameter. You should also make a use of copy() method of Book class.
String listBooks()
List all Book objects in the book list. The returned string is the concatenation of each Book object information in the list
for the number of currentBooksCount (do not use the size of the array here) Hint: you can utilize Book's toString method to help you complete this method. If there is no object in the list, This method should return the string containing " no book ".
void closeBookManagement()
Closes the book management system by making the list empty. This can be done by making every slot of the bookList array to null, and also by setting currentBooksCount to be 0.
No input/output should occur in the book management system. User interaction should be handled only by the driver class.
You may add other methods to the class in order to make your life easier.
Assignment8
All input and output should be handled in Assignment8 class. The main method should start by displaying this updated menu in this exact format:
Choice Action ------ ------
A Add Book
C Create BookManagement D Search Book
L List Books
O Sort Books
Q Quit
R Remove Book
T Close BookManagement
U Write Text to File
V Read Text from File
W Serialize BookManagement to File X Deserialize BookManagement from File ? Display Help
Next, the following prompt should be displayed:
What action would you like to perform?
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase. The following commands are modified or new.
Add Book
This part is already implemented in Assignment8.java file. Please see the case A in the main of Assignment8.java.
Create BookManagement
This part is already implemented in Assignment8.java file. Please see the case C in the main of Assignment8.java.
Search by Book Title
This part is already implemented in Assignment8.java file. Please see the case D in the main of Assignment8.java.
Sort Books
This part is already implemented in Assignment8.java file. Please see the case O in the main of Assignment8.java.
Remove Book
This part is already implemented in Assignment8.java file. Please see the case R in the main of Assignment8.java.
List Books
Each Book object information in the book list should be displayed using the toString method provided in the Book class. (and use listBooks( ) method in the BookManagement class.)
This part is already implemented in Assignment8.java file.
Please see the case L in the main of Assignment8.java.
Close BookManagement
Delete all Book objects. Then, display the following:
book management system closed
This part is already implemented in Assignment8.java file. Please see the case R in the main of Assignment8.javaT
Write Text to File
Your program should display the following prompt:
Please enter a file name to write:
Read in the filename and create an appropriate object to get ready to read from the
file. Then it should display the following prompts:
Please enter a string to write in the file:
Read in the string that a user types, say "input", then attach " " at the end of the
string, and write it to the file. (i.e. input+" " string will be written in the file.) If the operation is successful, display the following:
FILENAME was written
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch IOException. The file should be closed in a finally statement.
Read Text from File
Your program should display the following prompt:
Please enter a file name to read:
Read in the file name create appropriate objects to get ready to read from the file. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
FILENAME was read
Then read only the first line in the file, and display:
The first line of the file is:
CONTENT
where CONTENT should be replaced by the actual first line in the file.
Your program should catch the exceptions if there are. (Use try and catch statement to catch, FileNotFoundException, and the rest of IOException.)
If the file name cannot be found, display
FILENAME was not found
where FILENAME is replaced by the actual file name. Serialize BookManagement to File
Your program should display the following prompt: Please enter a file name to write:
Read in the filename and write the serialized BookManagement object out to it. Note that any objects to be stored must implement Serializable interface. The Serializable interface is defined in java.io.* package. If the operation is successful, display the following:
FILENAME was written
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch NotSerializableExeption and IOException. Deserialize BookManagement from File
Your program should display the following prompt:
Please enter a file name to read:
Read in the file name and attempt to load the BookManagement object from that file. Note that there is only one BookManagement object in the Assignment8 class, and the first object read from the file should be assigned to the BookManagement object. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
*FILENAME was read
Your program should catch the exceptions if there are.
(Use try and catch statement to catch ClassNotFoundException, FileNotFoundException, and the rest of IOException.)
This is the code I have as of right now. It needs to be added to a bit, and some new classes created as described above.
import java.io.*;
public class Assignment8
{
public static void main (String[] args)
{
char input1;
String author, title, ISBN;
String versionStr, priceStr, maxStr;
int version, max;
double price;
boolean operation = false;
int operation2 = 0;
String line;
String filename;
// create a BookManagement object. This is used throughout this class.
BookManagement manage1 = null;
try
{
// print out the menu
printMenu();
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
System.out.print("Please enter a maximum number of books ");
maxStr = stdin.readLine().trim(); //read in the max number as a string
max = Integer.parseInt(maxStr);
manage1 = new BookManagement(max);
do
{
System.out.print("What action would you like to perform? ");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) //check if a user entered only one character
{
switch (input1)
{
case 'A': //Add Book
System.out.print("Please enter a book to add: ");
System.out.print("Please enter its author to add: ");
author = stdin.readLine().trim();
System.out.print("Please enter its title to add: ");
title = stdin.readLine().trim();
System.out.print("Please enter its version: ");
versionStr = stdin.readLine().trim();
version = Integer.parseInt(versionStr);
System.out.print("Please enter its ISBN to add: ");
ISBN = stdin.readLine().trim();
System.out.print("Please enter its price to add: ");
priceStr = stdin.readLine().trim();
price = Double.parseDouble(priceStr);
operation = manage1.addBook(author, title, version, ISBN, price);
if (operation == true)
System.out.print("book added ");
else
System.out.print("book not added ");
break;
case 'C': //Create a new book management
System.out.print("Please enter a new maximum number of books: ");
maxStr = stdin.readLine().trim(); //read in the max number as a string
max = Integer.parseInt(maxStr);
manage1 = new BookManagement(max);
break;
case 'D': //Search book
System.out.print("Please enter author to search: ");
author = stdin.readLine().trim();
System.out.print("Please enter Book Title to search: ");
title = stdin.readLine().trim();
System.out.print("Please enter version to search: ");
versionStr = stdin.readLine().trim();
version = Integer.parseInt(versionStr);
operation2=manage1.bookExists(author, title, version);
if (operation2 > -1)
System.out.print("book by author " + author + " with title " + title + " and version " + version + " found ");
else
System.out.print("book by author " + author + " with title " + title + " and version " + version + " not found ");
break;
case 'L': //List books
System.out.print(manage1.listBooks());
break;
case 'O': // Sort books
manage1.sortBooks();
System.out.print("books sorted ");
break;
case 'Q': //Quit
break;
case 'R': //Remove book
System.out.print("Please enter author to search: ");
author = stdin.readLine().trim();
System.out.print("Please enter Book Title to search: ");
title = stdin.readLine().trim();
System.out.print("Please enter version to search: ");
versionStr = stdin.readLine().trim();
version = Integer.parseInt(versionStr);
operation=manage1.removeBook(author, title, version);
if (operation == true)
System.out.print("book by author " + author + " with title " + title + " and version " + version + " removed ");
else
System.out.print("book by author " + author + " with title " + title + " and version " + version + " not removed ");
break;
case 'T': //Close BookManagement
manage1.closeBookManagement();
System.out.print("book management system closed ");
break;
case 'U': //Write Text to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
/************************************************************************************
*** ADD your code to write a text (string) to the specified file. Catch exceptions.
************************************************************************************/
break;
case 'V': //Read Text from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
/************************************************************************************
*** ADD your code to read a text (string) from the specified file. Catch exceptions.
************************************************************************************/
break;
case 'W': //Serialize BookManagement to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
/************************************************************************************
*** ADD your code to write the book management bject to the specified file. Catch exceptions.
************************************************************************************/
break;
case 'X': //Deserialize BookManagement from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
/************************************************************************************
*** ADD your code to read a book management object from the specified file. Catch exception.
***********************************************************************************/
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
catch (IOException exception)
{
System.out.print("IO Exception ");
}
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add Book " +
"C Create BookManagement " +
"D Search Book " +
"L List Books " +
"O Sort Books " +
"Q Quit " +
"R Remove Book " +
"T Close BookManagement " +
"U Write Text to File " +
"V Read Text from File " +
"W Serialize BookManagement to File " +
"X Deserialize BookManagement from File " +
"? Display Help ");
}
} // end of Assignment8 class
import java.text.NumberFormat;
public class Publication {
private String ISBN;
private int version;
private double price;
/************************************************************************
Constructor is used to initialize intance variables.
************************************************************************/
public Publication()
{
ISBN = new String("?");
version = 1;
price = 0.0;
}
/************************************************************************
Accessor method:
This method returns the price of a book.
************************************************************************/
public double getPrice()
{
return price;
}
/************************************************************************
Accessor method:
This method returns the version of a book.
************************************************************************/
public int getVersion()
{
return version;
}
/************************************************************************
Accessor method:
This method returns the ISBN of a book.
************************************************************************/
public String getISBN()
{
return ISBN;
}
/************************************************************************
Modifier method:
This method sets the price of a book.
************************************************************************/
public void setPrice(double newPrice)
{
price = newPrice;
}
/************************************************************************
Modifier method:
This method sets the new version of a book.
************************************************************************/
public void setVersion(int newVersion)
{
version = newVersion;
}
/************************************************************************
Modifier method:
This method sets the ISBN of a book.
************************************************************************/
public void setISBN(String newISBN)
{
ISBN = newISBN;
}
/*****************************************************************************
This method return a string containing the publication information of a book.
*****************************************************************************/
public String toString()
{
String result;
NumberFormat money = NumberFormat.getCurrencyInstance();
result = "Edition:" + version + " ISBN: " + ISBN + " Price: " + money.format(price);
return result;
}
}
public class Book
{
private String title;
private String author;
private Publication pub;
public Book()
{
title = "?";
author = "?";
pub = new Publication();
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public Publication getPublication()
{
return pub;
}
public void setTitle(String newTitle)
{
title = newTitle;
}
public void setAuthor(String newAuthor)
{
author = newAuthor;
}
public void setPublication(int newVersion, String newISBN, double newPrice)
{
pub.setVersion(newVersion);
pub.setISBN(newISBN);
pub.setPrice(newPrice);
}
/*************************************
Define a copy method that copies every member variable value from "other" parameter to
their corresponding variable of the object itself using "this" reference
**************************************/
public void copy(Book other)
{
//To be defined
}
public String toString()
{
String result = " Title: "
+ getTitle()+" "
+ "Author: "+getAuthor()+" "
+ pub.toString()+" ";
return result;
}
}
If this is inputted
This is what should be outputted
that input/output can also be used as a guideline.
Thanks!
//if you answer this correctly you can also copy and paste your answer here
https://www.chegg.com/homework-help/questions-and-answers/help-java-code-thanks-assignment8java-code-need-added-bookjava-needs-modified-assignment-p-q24470006
//I posted the question 2x because no one was answering it, I will rate you in both places.
Attribute name
Attribute type
Description
bookList
an array of Book objects
A list of Book objects in the book management system
currentBooksCount
int
the number of Book objects created and stored in the bookList
maxSize
int
the maximum number of Book objects that can be stored in the bookList array. It is also the size of the bookList array.
Explanation / Answer
PROGRAM CODE:
Publication:
package book;
import java.io.Serializable;
import java.text.NumberFormat;
public class Publication implements Serializable{
private String ISBN;
private int version;
private double price;
/************************************************************************
Constructor is used to initialize intance variables.
************************************************************************/
public Publication()
{
ISBN = new String("?");
version = 1;
price = 0.0;
}
/************************************************************************
Accessor method:
This method returns the price of a book.
************************************************************************/
public double getPrice()
{
return price;
}
/************************************************************************
Accessor method:
This method returns the version of a book.
************************************************************************/
public int getVersion()
{
return version;
}
/************************************************************************
Accessor method:
This method returns the ISBN of a book.
************************************************************************/
public String getISBN()
{
return ISBN;
}
/************************************************************************
Modifier method:
This method sets the price of a book.
************************************************************************/
public void setPrice(double newPrice)
{
price = newPrice;
}
/************************************************************************
Modifier method:
This method sets the new version of a book.
************************************************************************/
public void setVersion(int newVersion)
{
version = newVersion;
}
/************************************************************************
Modifier method:
This method sets the ISBN of a book.
************************************************************************/
public void setISBN(String newISBN)
{
ISBN = newISBN;
}
/*****************************************************************************
This method return a string containing the publication information of a book.
*****************************************************************************/
public String toString()
{
String result;
NumberFormat money = NumberFormat.getCurrencyInstance();
result = "Edition:" + version + " ISBN: " + ISBN + " Price: " + money.format(price);
return result;
}
}
Book:
package book;
import java.io.Serializable;
public class Book implements Serializable
{
private String title;
private String author;
private Publication pub;
public Book()
{
title = "?";
author = "?";
pub = new Publication();
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public Publication getPublication()
{
return pub;
}
public void setTitle(String newTitle)
{
title = newTitle;
}
public void setAuthor(String newAuthor)
{
author = newAuthor;
}
public void setPublication(int newVersion, String newISBN, double newPrice)
{
pub.setVersion(newVersion);
pub.setISBN(newISBN);
pub.setPrice(newPrice);
}
/*************************************
Define a copy method that copies every member variable value from "other" parameter to
their corresponding variable of the object itself using "this" reference
**************************************/
public void copy(Book other)
{
this.title = other.getTitle();
this.author = other.getAuthor();
setPublication(other.getPublication().getVersion(), other.getPublication().getISBN(), other.getPublication().getPrice());
}
public String toString()
{
String result = " Title: "
+ getTitle()+" "
+ "Author: "+getAuthor()+" "
+ pub.toString()+" ";
return result;
}
}
BookComparator:
package book;
import java.util.Comparator;
public class BookComparator implements Comparator<Book>{
@Override
public int compare(Book o1, Book o2) {
if(o1.getAuthor().compareTo(o2.getAuthor()) == 0)
{
if(o1.getTitle().compareTo(o2.getTitle()) == 0)
{
if(o1.getPublication().getVersion() < o2.getPublication().getVersion())
return -1;
else if(o1.getPublication().getVersion() > o2.getPublication().getVersion())
return 1;
else return 0;
}
else return o1.getTitle().compareTo(o2.getTitle());
}
else return o1.getAuthor().compareTo(o2.getAuthor());
}
}
Sorts:
package book;
public class Sorts {
public static void sort(Book[] bookList, int size, BookComparator comparator)
{
for(int i=0; i<size; i++)
{
int index = i;
for (int j = i + 1; j < size; j++)
if(comparator.compare(bookList[j], bookList[index]) < 0)
index = j;
Book leastBook = bookList[index];
bookList[index] = bookList[i];
bookList[i] = leastBook;
}
}
}
BookManagement:
package book;
import java.io.Serializable;
import java.util.ArrayList;
public class BookManagement implements Serializable{
private Book bookList[];
private int currentBooksCount;
private int maxSize;
public BookManagement(int maximumsize) {
this.maxSize = maximumsize;
currentBooksCount = 0;
bookList = new Book[maximumsize];
for(int i=0; i<maxSize; i++)
bookList[i] = null;
}
public int bookExists(String author, String title, int version)
{
for(int i=0; i<currentBooksCount; i++)
{
if(bookList[i].getTitle().equals(title) && bookList[i].getAuthor().equals(author) && bookList[i].getPublication().getVersion() == version)
return i;
}
return -1;
}
public boolean addBook(String author, String title, int version, String ISBN, double price)
{
if(currentBooksCount == maxSize)
return false;
for(int i=0; i<currentBooksCount; i++)
{
if(bookList[i].getTitle().equals(title) && bookList[i].getAuthor().equals(author)
&& bookList[i].getPublication().getVersion() == version && bookList[i].getPublication().getISBN().equals(ISBN)
&& bookList[i].getPublication().getPrice() == price)
return false;
}
Book newBook = new Book();
newBook.setAuthor(author);
newBook.setPublication(version, ISBN, price);
newBook.setTitle(title);
bookList[currentBooksCount++] = newBook;
return true;
}
public boolean removeBook(String author, String title, int version)
{
int index = bookExists(author, title, version);
if(index == -1)
return false;
else
{
bookList[index] = null;
return true;
}
}
public void sortBooks()
{
Sorts.sort(bookList, currentBooksCount, new BookComparator());
}
public String listBooks()
{
if(currentBooksCount == 0)
return " no book ";
String result = "";
for(int i=0; i<currentBooksCount; i++)
{
result += bookList[i].toString() + " ";
}
return result;
}
public void closeBookManagement()
{
for(int i=0; i<maxSize; i++)
bookList[i] = null;
currentBooksCount = 0;
}
}
Assignment8:
package book;
import java.io.*;
import java.util.Scanner;
public class Assignment8
{
public static void main (String[] args)
{
char input1;
String author, title, ISBN;
String versionStr, priceStr, maxStr;
int version, max;
double price;
boolean operation = false;
int operation2 = 0;
String line;
String filename;
// create a BookManagement object. This is used throughout this class.
BookManagement manage1 = null;
try
{
// print out the menu
printMenu();
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
System.out.print("Please enter a maximum number of books ");
maxStr = stdin.readLine().trim(); //read in the max number as a string
max = Integer.parseInt(maxStr);
manage1 = new BookManagement(max);
do
{
System.out.print("What action would you like to perform? ");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) //check if a user entered only one character
{
switch (input1)
{
case 'A': //Add Book
System.out.print("Please enter a book to add: ");
System.out.print("Please enter its author to add: ");
author = stdin.readLine().trim();
System.out.print("Please enter its title to add: ");
title = stdin.readLine().trim();
System.out.print("Please enter its version: ");
versionStr = stdin.readLine().trim();
version = Integer.parseInt(versionStr);
System.out.print("Please enter its ISBN to add: ");
ISBN = stdin.readLine().trim();
System.out.print("Please enter its price to add: ");
priceStr = stdin.readLine().trim();
price = Double.parseDouble(priceStr);
operation = manage1.addBook(author, title, version, ISBN, price);
if (operation == true)
System.out.print("book added ");
else
System.out.print("book not added ");
break;
case 'C': //Create a new book management
System.out.print("Please enter a new maximum number of books: ");
maxStr = stdin.readLine().trim(); //read in the max number as a string
max = Integer.parseInt(maxStr);
manage1 = new BookManagement(max);
break;
case 'D': //Search book
System.out.print("Please enter author to search: ");
author = stdin.readLine().trim();
System.out.print("Please enter Book Title to search: ");
title = stdin.readLine().trim();
System.out.print("Please enter version to search: ");
versionStr = stdin.readLine().trim();
version = Integer.parseInt(versionStr);
operation2=manage1.bookExists(author, title, version);
if (operation2 > -1)
System.out.print("book by author " + author + " with title " + title + " and version " + version + " found ");
else
System.out.print("book by author " + author + " with title " + title + " and version " + version + " not found ");
break;
case 'L': //List books
System.out.print(manage1.listBooks());
break;
case 'O': // Sort books
manage1.sortBooks();
System.out.print("books sorted ");
break;
case 'Q': //Quit
break;
case 'R': //Remove book
System.out.print("Please enter author to search: ");
author = stdin.readLine().trim();
System.out.print("Please enter Book Title to search: ");
title = stdin.readLine().trim();
System.out.print("Please enter version to search: ");
versionStr = stdin.readLine().trim();
version = Integer.parseInt(versionStr);
operation=manage1.removeBook(author, title, version);
if (operation == true)
System.out.print("book by author " + author + " with title " + title + " and version " + version + " removed ");
else
System.out.print("book by author " + author + " with title " + title + " and version " + version + " not removed ");
break;
case 'T': //Close BookManagement
manage1.closeBookManagement();
System.out.print("book management system closed ");
break;
case 'U': //Write Text to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
try
{
PrintWriter writer = new PrintWriter(new File(filename));
System.out.println("Please enter a string to write in the file: ");
String userInput = stdin.readLine().trim();
writer.write(userInput + " ");
writer.close();
}catch (IOException e) {
System.out.println("Something went wrong!");
}
System.out.println(filename + " was written ");
break;
case 'V': //Read Text from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
try
{
Scanner reader = new Scanner(new File(filename));
String firstLine = reader.nextLine();
System.out.println(firstLine + " ");
reader.close();
} catch (FileNotFoundException e) {
System.out.println(filename + " was not found ");
}
catch (Exception ae) {
System.out.println("Something went wrong!");
}
System.out.println(filename + " was read ");
break;
case 'W': //Serialize BookManagement to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
try
{
FileOutputStream fOut = new FileOutputStream(new File(filename));
ObjectOutputStream out = new ObjectOutputStream(fOut);
System.out.println("Please enter a string to write in the file: ");
out.writeObject(manage1);
}catch (NotSerializableException qe) {
System.out.println("Not serializable!");
}
catch (IOException e) {
System.out.println("Something went wrong!");
}
System.out.println(filename + " was written ");
break;
case 'X': //Deserialize BookManagement from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
try
{
FileInputStream fin = new FileInputStream(new File(filename));
ObjectInputStream in = new ObjectInputStream(fin);
System.out.println("Please enter a string to write in the file: ");
manage1 = (BookManagement) in.readObject();
}catch (ClassNotFoundException qe) {
System.out.println("Not serializable!");
}
catch (FileNotFoundException ae) {
System.out.println("File not found!");
}
catch (IOException e) {
System.out.println("Something went wrong!");
}
System.out.println(filename + " was read ");
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
catch (IOException exception)
{
System.out.print("IO Exception ");
}
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add Book " +
"C Create BookManagement " +
"D Search Book " +
"L List Books " +
"O Sort Books " +
"Q Quit " +
"R Remove Book " +
"T Close BookManagement " +
"U Write Text to File " +
"V Read Text from File " +
"W Serialize BookManagement to File " +
"X Deserialize BookManagement from File " +
"? Display Help ");
}
} // end of Assignment8 class
OUTPUT:
Choice Action
------ ------
A Add Book
C Create BookManagement
D Search Book
L List Books
O Sort Books
Q Quit
R Remove Book
T Close BookManagement
U Write Text to File
V Read Text from File
W Serialize BookManagement to File
X Deserialize BookManagement from File
? Display Help
Please enter a maximum number of books
100
What action would you like to perform?
A
Please enter a book to add:
Please enter its author to add:
Nicholas Sparks
Please enter its title to add:
Dear John
Please enter its version:
26
Please enter its ISBN to add:
2364-24757-23
Please enter its price to add:
345
book added
What action would you like to perform?
L
Title: Dear John
Author: Nicholas Sparks
Edition:26
ISBN: 2364-24757-23
Price: $345.00
What action would you like to perform?
Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.