Question: Create a method named printBookOptions that has an array of objects as parameter and returns an integer. This method will help you select a book
Create a method named printBookOptions that has an array of objects as parameter and returns an integer. This method will help you select a book and return its id and will be useful in selecting a book. .
Using a loop, print Enter index for bookname for each of the 4 books, where index is the id of the book and bookname is the name of the book.
If a user enters an invalid integer, you must ask the question again.
*Do not use nextInt(), use nextLine() and convert it to integer using Integer.parseInt(input.nextLine()).
Finally return the valid choice of the user, which is the id of the selected book.
Create a method rentABook which has a parameter of a book object and returns nothing.
This method will try to rent a book using the rentBook method of Class Book. But make sure that you check if the book is available to rent, if the book is not available print Book not available. Hint check if object.rentBook() returns -1 or not.
The book will be rented for a certain number of days. Ask the user how many days they want to rent. If the number of days is less than 1, you need to keep asking. (Hint: use a while loop)
After getting a valid date, use setRentDays method from Class Book to set days rented.
At the same time, update your balance, multiply the number of days with rent amount of that book.
Code-
Scanner input = new Scanner(System.in); public class BookMarket{ private double balance; public Book [] setupMarket(){ setupMarket[0] = new Book("Computer Science", 0, 50); setupMarket[1] = new Book("Maths", 1, 40); setupMarket[2] = new Book("Biology", 2, 70); setupMarket[3] = new Book("Computer Science", 3, 60); return setupMarket; } public void printInventory(Book[]){ System.out.println("Here is the current inventory"); System.out.println("Book: " + setupMarket[0] + "rented for " + book.getDays); System.out.println("Book: " + setupMarket[1] + "rented for " + book.getDays); System.out.println("Book: " + setupMarket[2] + "rented for " + book.getDays); System.out.println("Book: " + setupMarket[3] + "rented for " + book.getDays); System.out.println("Available no of books: " + book.count); System.out.println("Total balance: $" + balance); }
public static void main(String [] args){ Book [] books; setupMarket() = books[]; System.out.println("Welcome to Book Market."); printInventory(); }
public class Book{ private String name; private int id; static int count; private int rentDays; private double rentAmt; private boolean rented; Book(String name, int id, double rentAmt){ this.name = name; this.id = id; this.rentAmt = rentAmt; } Book(String name, int id){ this.(name, id); rentAmt = 0.0; } public String getName(){ return this.name; } public int getID(){ return this.id; } public static int getCount(){ return this.count; } public int getrentDays(){ return this.rentDays; } public boolean getrented(){ return this.rented; } public void setName(String name){ this.name = name; } public void setID(int id){ this.id = id; } public static void setCount(int count){ this.count = count; } public void setDays(int rentDays){ this.rentDays = rentDays; } public void setRented(boolean rented){ this.rented = rented; } //Step 8 Create rentBook method public boolean rentBook(boolean rented){ if(rented==false){ rented = true; return count--; } else{ return -1; } } public boolean returnBook(boolean rented){ if(rented==false){ System.out.println("This book has not been rented." ); rented = true; return -1; } else{ rented = false; setRentDays = 0; count++; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
