库模型

时间:2017-03-28 13:29:56

标签: java oop inheritance

我是Java的新手,我需要一些帮助,为类似于Library模型(租车模型)的项目实施OOP规则。

我有2个班级:

  • public class Book
  • public class Customer

简单地说,我的课程看起来像这样:

public class Customer
{
    public String fullName;
    public String address;

    /* Constructor */
    public Customer(String fullName, String address) {
        this.fullName = fullName;
        this.address = address;
    }}

public class Book
{
    private String bookName;
    private int pageNumber;
    public Customer customer;   //I think this needs to be fixed

    /* Constructor */
    public Book(String bookName, int pageNumber, Customer customer) {
        this.bookName = bookName;
        this.pageNumber = pageNumber;
        // ... There I am not sure how to do something like: this.customer = customer ...

    // there should be another constructor without customer as a parameter but i can deal with that cuz its easy
    }}

Customer类的每个实例代表借用该书的人。现在我正在努力实现的是:如何将Customer类的实例作为Book类的构造函数的参数传递?我想要这样的东西:

// Constructor in Class Book:
public Book(String bookName, int pageNumber, Customer customer) {
    this.bookName = bookName;
    this.pageNumber = pageNumber;
    this.customer = customer;  //I think this needs to be fixed
}

另一个问题是如何创建一张住在伦敦的Sarah Connor博客的记录?这个实现是否正确Book book1 = new Book(“The Hobbit”,300,“Sarah Connor”,“London”)?

2 个答案:

答案 0 :(得分:1)

基本解决方案

基本上,您的方法完全正确:

 public Book(String bookName, int pageNumber, Customer customer) {
        this.bookName = bookName;
        this.pageNumber = pageNumber;
        this.customer = customer;
 }

但是,正如您的评论所示,您仍然缺乏对Java的非常基本理解。让我们回顾一下您的尝试:

Book book1 = new Book("Harry Potter", 500, "Mike", "London");

为什么这不起作用?
首先,您传入4个参数而不是3个。但是我们假设这是一个拼写错误并忽略"London"。但仍存在一个问题:"Mike"String,但您的构造函数需要Customer。因此,您的代码应如下所示:

Customer mike = new Customer("Mike Malony", "N13BJ London");
Book book1 = new Book("Harry Potter", 500, mike);

由于您在评论中提到,这也可以在一行中完成:

Book book1 = new Book("Harry Potter", 500, new Customer("Mike", "London"));

进一步的问题和建议

现在你的问题似乎已经解决了,但你的代码仍然不是很通用的。原因是您的模型受到严格限制:如果您传入Book,则只能实例化Customer。但我认识的大多数图书馆都有可以借阅的图书。此外,无论你的pageNumber似乎代表什么(书签,可能?),我都假设一本未经修改过的书并不需要这些信息。因此我建议:

public Book(String bookName) {
    this.bookName = bookName;
    this.pageNumber = 0;      // 0    => no page bookmarked
    this.customer = null;     // null => book is available
}

现在您需要一种方法向客户分发一本书并检查其状态。我建议:

public void lend(Customer customer) {
    this.customer = customer;
}

public void receiveBack() {
    customer = null;
    pageNumber = 0;
}

public boolean isAvailable() {
    return (customer == null);
}

并为网页添加书签:

public setBookmark(int pageNumber) {
     this.pageNumber = pageNumber;
}

当然,这还远非最佳。如果客户想借几本书怎么办?书籍真的应该引用顾客吗?有几种方法可以解决这个问题。一个是拥有一个专门的课程,其中包含客户与书籍的关联。另一种方法是在BookCustomer中使用某种集合。但是,这可能超出了你的任务范围,所以我会把它留在上面。

答案 1 :(得分:0)

如果您想声明您的实例

Book book1 = new Book("The Hobbit", 300, "Sarah Connor", "London")

您需要像这样定义Book Class的构造函数:

public Book(String bookName, int pageNumber, String fullname, String address) {
    this.bookName = bookName;
    this.pageNumber = pageNumber;
    this.customer = new Customer(fullname, address);  //I think this needs to be fixed
}

或者其他选择是声明它像这样

Book book1 = new Book("The Hobbit", 300, new Customer("Sarah Connor", "London"));