PHASE 1 You will implement a virtual bookstore in this time and next time homewo
ID: 3836074 • Letter: P
Question
PHASE 1 You will implement a virtual bookstore in this time and next time homework assignments. Your task is to write a Java program to keep track of books in a bookstore. The program should have a command line menu-driven interface that allows a user to insert, view, modify or remove a book item. A virtual bookstore keeps a book list which contains all the books in the store. Whenever a book is added to the store, it will get a unique id , ISBN , price, quantity and other book information will be registered. The quantity of a given book decreases when some copies are sold and increases if more copies are added. A book item consists of: title, author, ISBN, price and quantity. For this phase, you have to complete the following tasks: 1. Implement the Book class represented in the following UML diagram. Book -id: int -title: String -author: String -isbn: String -price: float -quantity: int +Book() +Book(String,String,String,float,int) +setTitle(String) +setAuthor(String) +setISBN(String) +setPrice(float) +setQuanitity(int) +getBookID(): int +getTitle(): String +getAuthor(): String +getISBN(): String +getPrice(): float +getQuantity(): int 2. Use the following piece of code which allows you to display a command-line menu to the user. Write the required java methodsstatements to make use of the code. For each of the options you have to implement the correspondent task through a method, i.e., addBook(Book book) which creats a new book object, displayBookInfo(Book book) which displays the info of a given book object, …etc. The program should run until the user chooses option 4, Quit. do { //display menu to user and ask user for her choice System.out.println(); System.out.println("1) Add a book"); System.out.println("2) Display all information about a book"); System.out.println("3) Change the available quantity of a book "); System.out.println("4) Quit"); System.out.println(); System.out.print("Enter choice [1-4]: "); user_choice = inputScanner.nextInt(); switch (user_choice) { //write the required code to identify user’s choice and accomplish the required task } } while (!quit); PHASE 2In this phase you will continue the work in the virtual book store program.The book store keeps a book list. The book list should be implemented using an array in Java. You will add a new data field, totalNumOfBooks, which represents the total number of books in the bookstore. totalNumOfBooks is incremented every time a book is added.You have to implement the following tasks:1. Create an array named booklist of length 10. Booklist contains objects of the class Book.2. Add the totalNumOfBooks data field to the class Book3. Change the signature of the methods responsible for realizing the menu options so that the book list is passed to the methods. I.e., addBook(Book book) will be addBook(Book[] bookList) and so on.4. The addBook(Book[] bookList) method checks if the booklist is full before adding a new book to it. In case the booklist array is full, ita. copies the contents of the array to a new temp arrayb. creates a new array with double the current lengthc. copies back temp to the new booklist array5. You will add a new option to the menu “Display all books”, which uses System.out.printf() to display the information of all the books in the book store in a table format.6. Display error messages to the user in case of entering wrong input.Useful tips:1. Read all of the required input as string objects using nextLine(); avoid using nextInt(),nextFloat().2. Remember! The id data field is updated automatically (is not set by the user). It represents the serial number of the book in the store. E.g., the id of the first book added is 1, second book is 2, third book is 3, …etc. I need this put with an arraylist.
Explanation / Answer
As u directed to above problem statement in ArrayList i have did same.
please find below the required code, if u expecting the solution un another way please let me know:
import java.util.ArrayList;
import java.util.Scanner;
public class Book {
private static int id=0;
private String isbn;
private float price;
private int quantity;
private String title;
private String author;
//declared list that will contains book
private static ArrayList<Book> bookList = new ArrayList<>();
//default constructor
public Book(){
//incrementing the id
++id;
}
//parameterized constructor
public Book(String title, String author,String isbn, float price, int quantity ) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.price = price;
this.quantity = quantity;
++id;
}
//setter and getter
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
//addbook method
public static void addBook(Book book){
bookList.add(book);
}
//display book method
public static void displayBookInfo(Book book){
System.out.println("Book Title : "+book.getTitle());
System.out.println("Book Author : "+book.getAuthor());
System.out.println("Book ISBN : "+book.getIsbn());
System.out.println("Book Quantity : "+book.getQuantity());
System.out.println("Book Price : "+book.getPrice());
}
//change quantity method
public static void changeQuantity(Book book){
Scanner sc = new Scanner(System.in);
System.out.println("Current Quantity : "+book.getQuantity());
System.out.print("Enter the new Quantity : ");
int newQuant = sc.nextInt();
book.setQuantity(newQuant);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag=false;
//user menu
while(!flag){
System.out.println("1) Add a book");
System.out.println("2) Display all information about a book");
System.out.println("3) Change the available quantity of a book ");
System.out.println("4) Quit"); System.out.println();
System.out.print("Enter choice [1-4]: ");
int user_choice=sc.nextInt();
switch(user_choice){
case 1: System.out.print("Enter the Book Tile :");
String title = sc.next();
System.out.print("Enter the book author : ");
String author = sc.next();
System.out.print("Enter the book ISBN : ");
String isbn = sc.next();
System.out.print("Enter the book quantity : ");
String quantity = sc.next();
System.out.print("Enter the book price : ");
String price = sc.next();
Book newBook = new Book(title,author,isbn,Float.valueOf(price),Integer.valueOf(quantity));
addBook(newBook);
break;
case 2: System.out.print("Enter the book ID :");
int bookid = sc.nextInt();
if(bookid>id){
System.out.println("Book not found");
break;
}
else{
displayBookInfo(bookList.get(bookid-1));
}
break;
case 3: System.out.print("Enter the book ID :");
int bookid1 = sc.nextInt();
if(bookid1>id){
System.out.println("Book not found");
break;
}
else{
changeQuantity(bookList.get(bookid1-1));
}
break;
case 4:flag=true;
break;
default : System.out.println("WARN: wrong user input");
}
}
}
}
CONSOLE OUTPUT:
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 1
Enter the Book Tile :Java
Enter the book author : Franklin
Enter the book ISBN : pierson
Enter the book quantity : 2
Enter the book price : 123
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 2
Enter the book ID :1
Book Title : Java
Book Author : Franklin
Book ISBN : pierson
Book Quantity : 2
Book Price : 123.0
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 3
Enter the book ID :1
Current Quantity : 2
Enter the new Quantity : 13
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 2
Enter the book ID :1
Book Title : Java
Book Author : Franklin
Book ISBN : pierson
Book Quantity : 13
Book Price : 123.0
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 1
Enter the Book Tile :C++
Enter the book author : Joseph
Enter the book ISBN : tata
Enter the book quantity : 3
Enter the book price : 234.67
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 2
Enter the book ID :2
Book Title : C++
Book Author : Joseph
Book ISBN : tata
Book Quantity : 3
Book Price : 234.67
1) Add a book
2) Display all information about a book
3) Change the available quantity of a book
4) Quit
Enter choice [1-4]: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.