2014-02-25 34 views
1
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() { 


} 

基本功课如何使方法我不得不做出一本书类,这些都是方法是,我不知道如何来填补一部分。我找不出如何使方法将本书标记为借阅并返回,以便我可以将它们用于布尔方法,因为我想自己找出其余部分,所以我没有发布。该标签的对象

+0

当有人叫借,你应该改变你的类的内部结构,也许表示,这本书是借来的属性, – nachokk

+1

@GlaucoNeves不要粗鲁..你不好笑,你是不是帮助,你的评论是不需要的 – nachokk

+0

你还需要一种方法来告诉书借用何时只是返回'借来的'变量的值... – MadProgrammer

回答

0

后面所有这一切的想法是,一种方法可修改一个对象的内部结构,

传递对象的状态到另一个新的状态。

例子:

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 
} 

} 

现在:

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(); 

} 

现在,如果你想提供如果一本书isRented()返回一个布尔方法发生什么事?如果你能想象自己,那么你就明白这一点。

+0

那主要部分是完全不同的。假设获得不同的输出结果,但是要感谢第一个代码。它帮助很多 – user3296193

0

尝试一个数组列表,添加/删除人员签出/退回书籍。为每本书打上号码。

这个论坛是不做功课的。如果你想在家庭作业上寻求帮助,你需要提出一个具体的问题,而不是:我不知道该怎么做,给我一些代码。请阅读你的书和学习,它只会帮助你。

+1

这应该是一个评论不是答案...你只是解释了有题 !! – zee

0
public class Book { 
    private boolean isOut; 

    ... 

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

    public isBorrowed() { 
     return isOut; 
    } 
} 

那么你可能会做

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

你应该使用带有布尔标记的数据类型的索引存储与否书创造通过数组的你有书籍数组,然后循环租用。 然后根据索引值打印消息。

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!"); 
} 
+0

我只为一本书做它,它应该教授课程。我知道如何去做大部分工作,问题在于制作一种方法,将书籍标记为返回并借阅。我的老师在这方面确实很模糊。 – user3296193