Question

public class Book { 
String title; 
boolean borrowed; 
// Creates a new Book 
public Book(String bookTitle){ 
    bookTitle= "The Da Vinci Code";
} 

// Marks the book as rented 
public void borrowed() { 


} 

// Marks the book as not rented 

public void returned() { 


} 

basically for homework I have to make a book class and these are the methods are part that I have no idea how to fill in. I can't figure how to make method to label the book as borrow and returned so i can use them for a boolean method that I didn't post because I want to figure out the rest by myself.

Was it helpful?

Solution

The idea behind all of this is that a method can modify internal structure of an object,

pass an object's state to another new state.

Example:

public class Book{

 private boolean isRented;

 public void borrow(){
     isRented = true; // you change your internal structure and in the new state is borrowed
 }

 public void returned(){
   isRented = false; // the same here
 }

}

And now in the main :

public static void main(String args []){
   //create a new book
   Book book = new Book();

   //rent the book
   book.borrow();
   //now i want to return
   book.returned();

}

Now what happen if you want to provide a boolean method that return if a book isRented() ? If you can figure yourself then you understand the point.

OTHER TIPS

Try an arraylist, add/remove as people check out/return books. Label each book a number.

This forum is not to do homework. If you want help on homework, you need to ask a specific question, not: I do not know how to do this, give me some code. Please read your book and study, it will only help you,.

public class Book  {
   private boolean isOut;

   ...

   public setBorrowed(boolean is_out)  {
      isOut = is_out;
   }

   public isBorrowed()  {
      return  isOut;
   }
}

Then you might do

Book bookIt = new Book("It by Stephen King");
bookIt.setBorrowed(true);    //Taken out of the library.

You should creat an array of the books you have and then loop through the array using an index with the datatype of boolean flag to store whether or not the book is rented. And then a print the message based on the index value.

 int rentedAtIndex = -1;

for(int i = 0; i < bookObj.length; i++) {
    if(bookObj[i].getName().equals(input)) {

        rentedAtIndex = i;  // Store the index for a future reference 
        break;             // break if the condition is met
    }
    }
       if(rentedAtIndex >= 0)
          System.out.println("The Book is Avavailbe for borrwoing  !");
       else
          System.out.println("The Book Is rented, Please try some other time!");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top