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

USE JAVA PART1 : Objectives: After finishing the lab4 part 1, you can: -handle t

ID: 3587144 • Letter: U

Question

USE JAVA

PART1:

Objectives: After finishing the lab4 part 1, you can:

-handle the stack structure when applying push and pop operations to add or fetch and delete the nodes -handle the queue structure when applying enque and deque operations to add or fetch and delete the nodes.

Requirement Statement:

Write the application to allow the librarians to use Stack or Queue structure to work with books.

Suppose the library only keep 2 categories of books: some books are only read at the library (Book) and some books can be checked out to bring home (BorrowedBook)

Each information of book (Book) has the following information: bookID (string), ISBN (string), title (string), writer (string), publisher (string)

For books that can be checked out (BorrowedBook) should have more information: Checkout date (string) and due date (string)

For each structure, the librarians can

Add a book

Remove a book

Display the book at top (or at front of Queue)

Show all books in the structure

After finishing a task, the application should all librarians to continue to do other task until they want to exit

TASK ADD A BOOK

-Allow users to type in the information of the Book or BorrowedBook

-add to the stack/queue

-print the the information of inserted book in the following format

For Book               

Book ID:                           495.1 YIP

Book ISBN:                      0-415-13535

Book title:                        Chinese An Essential Grammar

Writer:                              Don Rimmington

Publisher:                         Routledge

OR for BorrowedBook

Book ID:

495.1 YIP

Book ISBN:

0-415-13535

Book title:

Chinese An Essential Grammar

Writer:

Don Rimmington

Publisher:

Routledge

Date Check out:

09/22/2017

Due date

10/06/2017

TASK REMOVE A BOOK

-remove a book then print out the removed book or display message box: “The Stack is empty” or “The queue is empty”

TASK DISPLAY THE BOOK AT TOP (or AT FRONT FOR THE QUEUE)

-display the book at top (or front) or display the message box: “The Stack is empty” or “The queue is empty”

TASK SHOW ALL BOOKS

-display all the books or borrowed book on the stack (or queue)

INSTRUCTION HOW TO DO THE LAB PART1

ANALYZE

:

-You should read the requirement again to see what the requirement gives you and what it asks you to do then write them on the paper.

-The Book and BorrowedBook should have Inheritance relationship

-Also, you need to apply polymorphism in the main to deal with Book object and BorrowedBook

PSEUDO-CODD AND UML

-Provide UML of data type classes: class Book_yourLastName, class BorrowedBook_yourLastName

-provide pseudo-code of main(): Psuedo-code is written in English that lists what you have to do step by step in the main such that your program is qualified to the requirement. Write the pseudo-code in notepad and save with the file name as PART2_Pseudocode_yourLastName

-Based on what should be done in the Analyze, you should list step by step how you will do on the paper, organize these steps in good order such that if you follow these steps to write the code, your program can have the result as requested

Spend time to analyze and create the pseudo-code before writing the code is the best way to improve yourself in programming. If you do not know how to write the pseudo-code, contact me to get help.

WRITE THE CODE

1.Create the project name FA2017LAB4_PART1

2.Add Five classes: two data type classes, two data structure classes for Stack and Queue and one driver class FA2017LAB4_BookSystem_yourLastName

Based on the UML, write the code for data type classes Based on the pseudo-code write the code for main()

COMPILE THE PROJECT - RUN THE PROJECT

-ensure you have the comments

-ensure you have all tasks the requirements asking for

  

Book ID:

495.1 YIP

Book ISBN:

0-415-13535

Book title:

Chinese An Essential Grammar

Writer:

Don Rimmington

Publisher:

Routledge

Date Check out:

09/22/2017

Due date

10/06/2017

Explanation / Answer

1.Book.java

public class Book {

private String bookId;

private String isbn;

private String title;

private String writer;

private String publisher;

public Book(String bookId, String isbn, String title, String writer, String publisher) {

this.bookId = bookId;

this.isbn = isbn;

this.title = title;

this.writer = writer;

this.publisher = publisher;

}

public String getBookId() {

return bookId;

}

public void setBookId(String bookId) {

this.bookId = bookId;

}

public String getIsbn() {

return isbn;

}

public void setIsbn(String isbn) {

this.isbn = isbn;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getWriter() {

return writer;

}

public void setWriter(String writer) {

this.writer = writer;

}

public String getPublisher() {

return publisher;

}

public void setPublisher(String publisher) {

this.publisher = publisher;

}

}

2.BorrowedBook.java

//inheritance appiled

public class BorrowedBook extends Book{

private String checkOut;

private String dueDate;

public BorrowedBook(String bookId, String isbn, String title, String writer, String publisher, String checkOut,

String dueDate) {

super(bookId, isbn, title, writer, publisher);

this.checkOut = checkOut;

this.dueDate = dueDate;

}

public String getCheckOut() {

return checkOut;

}

public void setCheckOut(String checkOut) {

this.checkOut = checkOut;

}

public String getDueDate() {

return dueDate;

}

public void setDueDate(String dueDate) {

this.dueDate = dueDate;

}

}

3.Stack.java

import java.util.ArrayList;

public class Stack {

private ArrayList<Book> book=new ArrayList<Book>() ;

private ArrayList<BorrowedBook> borrowedBook=new ArrayList<BorrowedBook>();

static int top1=-1;

static int top2=-2;

public void push(Book b)//Push method is the example of polymorphism

{

book.add(b);

top1++;

}

public void push(BorrowedBook bb)

{

borrowedBook.add(bb);

top2++;

}

public Book pop1()

{

Book b;

if(top1==-1)

b=null;

else{

b = book.get(top1);

book.remove(top1);

top1--;

}

return b;

}

public BorrowedBook pop2()

{

BorrowedBook bb;

if(top2==-1)

bb=null;

else{

bb = borrowedBook.get(top2);

borrowedBook.remove(top2);

top2--;

}

return bb;

}

public Book displayTop1()

{

Book b;

if(top1==-1)

b=null;

else{

b=book.get(0);

}

return b;

}

public BorrowedBook displayTop2()

{

BorrowedBook bb;

if(top2==-1)

bb=null;

else{

bb = borrowedBook.get(0);

}

return bb;

}

public ArrayList<Book> getBook() {

return book;

}

public ArrayList<BorrowedBook> getBorrowedBook() {

return borrowedBook;

}

}

4.Queue.java

import java.util.ArrayList;

public class Queue {

private ArrayList<Book> book=new ArrayList<Book>() ;

private ArrayList<BorrowedBook> borrowedBook=new ArrayList<BorrowedBook>();

static int r1=-1;

static int r2=-1;

static int f1=-1;

static int f2=-1;

public void insert(Book b)//insert method is the example of polymorphism

{

book.add(b);

if(f1==-1)

f1++;

r1++;

}

public void insert(BorrowedBook bb)

{

borrowedBook.add(bb);

if(f2==-1)

f2++;

r2++;

}

public Book remove1()

{

Book b;

if(f1==-1)

b=null;

else{

b = book.get(f1);

book.remove(f1);

r1--;

if(r1==-1)

f1=-1;

}

return b;

}

public BorrowedBook remove2()

{

BorrowedBook bb;

if(f2==-1)

bb=null;

else{

bb = borrowedBook.get(f2);

borrowedBook.remove(f2);

r2--;

if(r2==-1)

f1=-1;

}

return bb;

}

public Book displayTop1()

{

Book b;

if(f1==-1)

b=null;

else{

b=book.get(0);

}

return b;

}

public BorrowedBook displayTop2()

{

BorrowedBook bb;

if(f2==-1)

bb=null;

else{

bb = borrowedBook.get(0);

}

return bb;

}

public ArrayList<Book> getBook() {

return book;

}

public ArrayList<BorrowedBook> getBorrowedBook() {

return borrowedBook;

}

}

5.DriverClass.java

//Please change the name of your driver class according to your instruction

import java.io.*;

import java.util.ArrayList;

public class DriverClass {

public static void main(String []args) throws Exception

{

String bookId;

String isbn;

String title;

String writer;

String publisher;

String checkOut;

String dueDate;

int choice;

System.out.println("1.Add a book 2.Add a borrowed book 3.Remove a book 4.Remove a borrowed book 5.Display the book at top "

+ "6.Display the borrowed book at top 7.Show all books 8.Exit");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Your Choice");

choice = Integer.parseInt(br.readLine());

Stack stack=new Stack();

Queue queue= new Queue();//Here we are using stack.You may use queue also.Just change the corresopnding methods

while(true)//we are incorporating an infinite loop for a continuous flow to test all methods

{

if (choice == 1)//adding books

{

System.out.println("Enter book id");

bookId = br.readLine();

System.out.println("Enter book ISBN");

isbn = br.readLine();

System.out.println("Enter book Title");

title = br.readLine();

System.out.println("Enter book Writer");

writer = br.readLine();

System.out.println("Enter book Publisher");

publisher = br.readLine();

Book b=new Book(bookId,isbn,title,writer,publisher);

stack.push(b);

}

else if (choice == 2)

{

System.out.println("Enter book id");

bookId = br.readLine();

System.out.println("Enter book ISBN");

isbn = br.readLine();

System.out.println("Enter book Title");

title = br.readLine();

System.out.println("Enter book Writer");

writer = br.readLine();

System.out.println("Enter book Publisher");

publisher = br.readLine();

System.out.println("Enter book checkout date");

checkOut = br.readLine();

System.out.println("Enter book due date");

dueDate = br.readLine();

BorrowedBook bb=new BorrowedBook(bookId,isbn,title,writer,publisher,checkOut,dueDate);

stack.push(bb);

}

else if (choice == 3)//removing books

{

Book b=stack.pop1();

if(b==null)

System.out.println("Stack is empty");

else

{

System.out.println("The following book is removed");

System.out.println(b);

}

}

else if (choice == 4)

{

BorrowedBook bb=stack.pop2();

if(bb==null)

System.out.println("Stack is empty");

else

{

System.out.println("The following borrowed book is removed");

System.out.println(bb);

}

}

else if(choice == 5)//displays the top book

{

Book b=stack.displayTop1();

if(b==null)

System.out.println("Stack is empty");

else

{

System.out.println("The following book is at top");

System.out.println(b);

}

}

else if (choice == 6)

{

BorrowedBook bb=stack.displayTop2();

if(bb==null)

System.out.println("Stack is empty");

else

{

System.out.println("The following borrowed book is at top");

System.out.println(bb);

}

}

else if(choice == 7)//prints the list of all the books

{

ArrayList<Book> book=new ArrayList<Book>() ;

book=stack.getBook();

ArrayList<BorrowedBook> borrowedBook=new ArrayList<BorrowedBook>();

borrowedBook=stack.getBorrowedBook();

System.out.println("List of books");

System.out.println(book);

System.out.println("List of borrowed books");

System.out.println(borrowedBook);

}

else if(choice == 8)//here we are exiting from the loop as well as from the program

{

break;

}

else

System.out.println("Please enter correct choice");

}

}

}