Set-Up - Create a new project in your Eclipse workspace named: ******** In the s
ID: 3694533 • Letter: S
Question
Set-Up
- Create a new project in your Eclipse workspace named: ******** In the src folder, create a package named: **********
- Import the following file at ************************** into the src folder of your project.
o BookListApp.java
- Import the input file, BookData.txt, into the root of your project.
Problem
You will be writing a program to manage a list of books borrowed. Books already borrowed are stored in a file. The data in the file (title, author, isbn) is read into an array of Book objects. The user will be presented with a menu with the following choices: add a new book, display the list of books, display a book, or quit. You will create one new class and modifying an existing class.
Be sure to write good comments as you will be generating Javadoc API documents at the end of the lab.
Class diagram and main algorithm
Book
- title:String
- author:String
- isbn:String
- returnDate:String
+ Book(String title, String author, String isbn, String returnDate)
+ getIsbn():String
+ toString( ):String
main algorithm
CREATE array of Book objects
INITIALIZE the array to hold empty objects
READ book list file
PRINT menu
choice = READ menu choice
WHILE (choice != 4)
CASE choice
WHEN 1:
READ data for new book from keyboard
ADD the new Book object to the array
INCREMENT count
WHEN 2:
PRINT the array
WHEN 3:
READ data for isbn
FIND book in array and display book information
OTHERWISE:
PRINT "Invalid menu choice. Please try again"
END CASE
PRINT menu
choice = READ menu choice
END WHILE
WRITE to the output file
CLOSE output file
END MAIN
Processing Instructions (write the code in this order) – use the comments in the program to help with placement.
1. Implement the class called Book.java using the given class diagram.
- There is no default constructor, only the one accepting values for all instance fields
- toString - define it so that it prints in the form:
Title:
Author:
ISBN:
Return date:
2. Write the prompt and read the menu choice from the user. There are two of these statements. One before the loop and another at the bottom of the loop.
- Run your program to test printing and reading the menu choice to be sure this is working correctly.
3. Open the file, read the input data from the file, create a Book object for each set of data read, store the book object in the array. When done, close the file. Remember to use a try/catch when attempting to open the file. You will also be keeping track of how many Book objects are put into the array.
- Test this using menu choice 2 to see if everything loaded correctly.
4.Complete the BookListApp, using the comments as guides.
5. Run the code. Make sure to test all options. Watch out for buffer problems.
To Be Submitted
The following files should be zipped together into a file called Lab14.zip and submitted to ReggieNet by the beginning of your next lab.
BookListApp.java
Book.java
BookData.txt -
The Help
Kathryn Stockett
0425232204 04/08/2016
every day
David Levithan
0307931894 04/08/2016
The Lowland
Jhumpa Lahiri
0307278265 /3/31/2016
The Three Year Swim Club
Julie Checkoway
145555523443 04/15/2016
the imperfectionists
Tom Rachman
0385343671 /5/1/2016
and BookListApp.java
package edu.ilstu;
import java.io.IOException;
import java.util.Scanner;
/**
* Application class to keep track of books rented
*
* @author Ms. Patricia Matsuda,
*
*/
public class BookListApp
{
public static void main(String[] args)
{
final int MAX_ARRAY_SIZE = 20;
final String FILENAME = "BookData.txt";
final String OUTFILE = "UpdatedBooksData.txt";
Scanner keyboard = new Scanner(System.in);
// Counter to keep track of number of elements in the array
int count = 0;
// Menu choice
int choice = 0;
// Create array to hold book collection
//Open input file using try-catch
// Read input file until no more reconrds and keep count of the
// data from the input file into the array
// Close input file
// Print the menu
System.out.println();
System.out.println();
System.out.println("Books Rented ");
System.out.println("1. Add a Book");
System.out.println("2. Print Book List");
System.out.println("3. Display Book");
System.out.println("4. Quit");
// Prompt and read the menu choice
while (choice != 4)
{
switch (choice)
{
case 1:
// Prompt and read a new book from the keyboard to add to the list
// Read title from the keyboard
// Read author from the keyboard
// Read isbn from the keyboard
// Read date from the keyboard in format dd/mm/yyyy
// Create a new book object
// Add the author to the array
// Don't forget to increment the count
break;
case 2:
// Print the array
break;
case 3:
// ask user for isbn
// Search for and display information about a book
break;
default:
System.out.println("Invalid menu choice. Please try again.");
}
// Print the menu
System.out.println();
System.out.println("Books Rented ");
System.out.println("1. Add a Book");
System.out.println("2. Print Book List");
System.out.println("3. Display Book");
System.out.println("4. Quit");
// Prompt and read the menu choice
}
//Open output file using try-catch
//Write array to the output file
// Close the output file
}
}
Book
- title:String
- author:String
- isbn:String
- returnDate:String
+ Book(String title, String author, String isbn, String returnDate)
+ getIsbn():String
+ toString( ):String
Explanation / Answer
/**
* @author Srinivas Palli
*
*/
public class Book {
String title;
String author;
String isbn;
String returnDate;
/**
* @param title
* @param author
* @param isbn
* @param returnDate
*/
public Book(String title, String author, String isbn, String returnDate) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.returnDate = returnDate;
}
/**
* @return
*/
public String getIsbn() {
return isbn;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Title: " + title + " Author: " + author + " ISBN: " + isbn
+ " Return date:" + returnDate;
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
/**
* Application class to keep track of books rented
*
* @author Ms. Patricia Matsuda,
*
*/
public class BookListApp {
public static void main(String[] args) {
final int MAX_ARRAY_SIZE = 20;
final String FILENAME = "BookData.txt";
final String OUTFILE = "UpdatedBooksData.txt";
Scanner keyboard = new Scanner(System.in);
// Counter to keep track of number of elements in the array
int count = 0;
// Menu choice
int choice = 0;
// Create array to hold book collection
Book books[] = new Book[MAX_ARRAY_SIZE];
// Open input file using try-catch
Scanner fileScan = null;
try {
fileScan = new Scanner(new File(FILENAME));
} catch (Exception e) {
// TODO: handle exception
}
// Read input file until no more reconrds and keep count of the
while (fileScan.hasNext()) {
String title = fileScan.nextLine();
String author = fileScan.nextLine();
String nextL = fileScan.nextLine();
String isbn = nextL.split(" ")[0];
String returnDate = nextL.split(" ")[1];
books[count] = new Book(title, author, isbn, returnDate);
count++;
}
// data from the input file into the array
// Close input file
fileScan.close();
// Print the menu
System.out.println();
System.out.println();
System.out.println("Books Rented ");
System.out.println("1. Add a Book");
System.out.println("2. Print Book List");
System.out.println("3. Display Book");
System.out.println("4. Quit");
// Prompt and read the menu choice
System.out.print("Enter your choice:");
choice = keyboard.nextInt();
while (choice != 4) {
switch (choice) {
case 1:
// Prompt and read a new book from the keyboard to add to the
// list
// Read title from the keyboard
System.out.print("Enter the title:");
String title = keyboard.next();
// Read author from the keyboard
System.out.print("Enter the author:");
String author = keyboard.next();
// Read isbn from the keyboard
System.out.print("Enter the isbn:");
String isbn = keyboard.next();
// Read date from the keyboard in format dd/mm/yyyy
System.out.print("Enter the returnDate in format dd/mm/yyyy:");
String returnDate = keyboard.next();
// Create a new book object
Book book = new Book(title, author, isbn, returnDate);
// Add the author to the array
books[count] = book;
// Don't forget to increment the count
count++;
break;
case 2:
// Print the array
for (int i = 0; i < count; i++) {
System.out.println(books[i]);
}
break;
case 3:
// ask user for isbn
System.out.print("Enter the isbn:");
String isbn1 = keyboard.next();
// Search for and display information about a book
for (int i = 0; i < count; i++) {
if (books[i].getIsbn().equalsIgnoreCase(isbn1))
System.out.println(books[i]);
}
break;
default:
System.out.println("Invalid menu choice. Please try again.");
}
// Print the menu
System.out.println();
System.out.println("Books Rented ");
System.out.println("1. Add a Book");
System.out.println("2. Print Book List");
System.out.println("3. Display Book");
System.out.println("4. Quit");
// Prompt and read the menu choice
// Prompt and read the menu choice
System.out.print("Enter your choice:");
choice = keyboard.nextInt();
}
// Open output file using try-catch
try {
File file = new File(OUTFILE);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// Write array to the output file
for (int i = 0; i < count; i++) {
bw.write(books[i].toString() + " ");
}
bw.close();
// Close the output file
} catch (Exception e) {
// TODO: handle exception
} finally {
}
}
}
BookData.txt
The Help
Kathryn Stockett
0425232204 04/08/2016
every day
David Levithan
0307931894 04/08/2016
The Lowland
Jhumpa Lahiri
0307278265 /3/31/2016
The Three Year Swim Club
Julie Checkoway
145555523443 04/15/2016
the imperfectionists
Tom Rachman
0385343671 /5/1/2016
UpdatedBooksData.txt
Title: The Help Author: Kathryn Stockett ISBN: 0425232204 Return date:04/08/2016
Title: every day Author: David Levithan ISBN: 0307931894 Return date:04/08/2016
Title: The Lowland Author: Jhumpa Lahiri ISBN: 0307278265 Return date:/3/31/2016
Title: The Three Year Swim Club Author: Julie Checkoway ISBN: 145555523443 Return date:04/15/2016
Title: the imperfectionists Author: Tom Rachman ISBN: 0385343671 Return date:/5/1/2016
Title: newbook Author: newauth ISBN: 12345 Return date:10/11/2015
OUTPUT:
Books Rented
1. Add a Book
2. Print Book List
3. Display Book
4. Quit
Enter your choice:1
Enter the title:newbook
Enter the author:newauth
Enter the isbn:12345
Enter the returnDate in format dd/mm/yyyy:10/11/2015
Books Rented
1. Add a Book
2. Print Book List
3. Display Book
4. Quit
Enter your choice:2
Title: The Help Author: Kathryn Stockett ISBN: 0425232204 Return date:04/08/2016
Title: every day Author: David Levithan ISBN: 0307931894 Return date:04/08/2016
Title: The Lowland Author: Jhumpa Lahiri ISBN: 0307278265 Return date:/3/31/2016
Title: The Three Year Swim Club Author: Julie Checkoway ISBN: 145555523443 Return date:04/15/2016
Title: the imperfectionists Author: Tom Rachman ISBN: 0385343671 Return date:/5/1/2016
Title: newbook Author: newauth ISBN: 12345 Return date:10/11/2015
Books Rented
1. Add a Book
2. Print Book List
3. Display Book
4. Quit
Enter your choice:3
Enter the isbn:12345
Title: newbook Author: newauth ISBN: 12345 Return date:10/11/2015
Books Rented
1. Add a Book
2. Print Book List
3. Display Book
4. Quit
Enter your choice:3
Enter the isbn:0425232204
Title: The Help Author: Kathryn Stockett ISBN: 0425232204 Return date:04/08/2016
Books Rented
1. Add a Book
2. Print Book List
3. Display Book
4. Quit
Enter your choice:4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.