Equals方法对于对象的副本

时间:2018-02-13 20:54:41

标签: java class arraylist junit equals

我是Java的新手,想知道为什么我写的这个方法不起作用:

public boolean checkOut(Person p, Book b, String dueDate){
    ArrayList<Book> removeBooks = new ArrayList<Book>();
    if (libraryBooks.contains(b) && patrons.contains(p)){
        for (Book book1 : libraryBooks){
            if (book1.equals(b)){
                p.addBook(book1);
                removeBooks.add(book1);
                book1.setDueDate(dueDate);
                break;
            }}
        for (Book b2 : removeBooks){
            libraryBooks.remove(b2);    
        }
        return true;
    }
    else{
        return false;
    }
    }

该方法包含person,book和dueDate。 libraryBooks是库类具有的所有书对象的列表。图书馆有顾客,是图书馆成员的人员名单。

此方法应检查Book对象b和Person b是否分别位于libraryBooks和patrons列表中。接下来,它遍历libraryBooks中的每个books对象,并使用重写的equals方法来比较对象是否相同。如果是,它会将书籍分配给人员书籍列表,并在书籍检出时将其从图书馆书籍中删除,而不再在图书馆内删除。

提交要评分的类文件后,我收到以下错误: 。一个人能够查看已经签出的书。

。签出图书时,您应该更新图书馆中的图书,而不是传入的图书。

我很困惑一个人如何能够签出已经签出的书籍对象,因为这个方法应该1.检查被传递的书籍对象是否在libraryBooks中2.如果得到则从libraryBooks中删除它签出了!

这是Book.java,它包含书籍对象:

    public class Book {
    //Initializing variables
    private String title;
    private String author;
    private String dueDate;
    private boolean checkedOut;
    private double bookId;
    private double bookValue;

//Constructor
public Book(String t, String a, double id, double bv){
    title = t;
    author = a;
    bookId = id;
    bookValue = bv;
}

//Accessor
public String getTitle(){
    return title;
}

//Accessor
public String getAuthor(){
    return author;
}

//Accessor
public String getDueDate(){
    return dueDate;
}

//Accessor
public boolean isCheckedOut(){
    return checkedOut;
}

//Accessor
public double getBookId(){
    return bookId;
}

//Accessor
public double getBookValue(){
    return bookValue;
}

//Mutator
public void setDueDate(String dd){
    dueDate = dd;
}

//Mutator
public void setBookValue(double bv){
    bookValue = bv;
}

//Mutator
public void setCheckedOut(boolean b){
    checkedOut = b;
}
@Override
public boolean equals(Object o){
    //Testing to see if value isnt null
    if(o == null){
        return false;
    }
    //Testing to see if value is getting compared to itself
    if (this == o){
        return true;
    }
    //Checking if the values are of the same class
    if (getClass() != o.getClass()){
        return false;
    }
    Book book = (Book)o;
    return (book.bookId == this.bookId);
}

这是Person.java:

public Person(String n, String a, int lcn){
    name = n;
    address = a;
    libraryCardNum = lcn;
}

//Accessor
public String getName(){
    return name;
}

//Accessor
public int getLibraryCardNumber(){
    return libraryCardNum;
}

//Accessor
public ArrayList<Book> getCheckedOut(){
    return checkedOut;
}

//Accessor
public String getAddress(){
    return address;
}

//Accessor
public int getLibraryCardNum(){
    return libraryCardNum;
}

//Mutator
public void setAddress(String a){
    address = a;
}

//Mutator
public void setLibraryCardNum(int lcn){
    libraryCardNum = lcn;
}

//Mutator Method
public void setName(String n){
    name = n;
}

public boolean addBook(Book b){
    //Checks to see if the book is already in the AL
    if (checkedOut.contains(b)){
        return false;
    }
    else{
        checkedOut.add(b);
        return true;
    }
}

public boolean hasRead(Book b){
    if (checkedOut.contains(b)){
        return true;
    }
    else{
        return false;
    }
}

public boolean forgetBook(Book b){
    if (checkedOut.contains(b)){
        checkedOut.remove(b);
        return true;
    }
    else{
        return false;
    }
}

public int numBooksRead(){
    return checkedOut.size(); //Returns the amount of book objects in AL
}
@Override
public boolean equals(Object o){
    //Checks to make sure the object isnt null
    if (o == null){
        return false;
    }
    //Checks to see if objects are of the same class
    if (getClass() != o.getClass()){
        return false;
    }
    //Casts the object to the person class
    Person person = (Person)o;
    //Checks to see if the name and ID are the same
    return (this.libraryCardNum == person.libraryCardNum);
}

这是完整的Library.java:

import java.util.ArrayList;
import java.util.GregorianCalendar;

import org.junit.Test;
public class Library {
     private ArrayList<Book> libraryBooks = new ArrayList<Book>();
     private ArrayList<Person> patrons = new ArrayList<Person>();
     private String name;
     private int numBooks;
    private int numPeople;
private static String currentDate;

//Constructor
public Library(String n){
    name = n;
}

//Accessor
public ArrayList<Book> getLibraryBooks(){
    return libraryBooks;
}

//Accessor
public ArrayList<Person> getPatrons(){
    return patrons;
    }

//Accesor 
public String getName(){
    return name;
}

//Accesor 
public int getNumBooks(){
    int checkedOutBooks = totalNumBooks() - libraryBooks.size();
    numBooks = libraryBooks.size() - checkedOutBooks;
    return numBooks;
    }

//Accesor 
public int getNumPeople(){
    numPeople = patrons.size();
    return numPeople;
}

//Accesor 
public String getCurrentDate(){ 
    return currentDate;
        }

//Mutator
public void setName(String n){
    name = n;
}

//Mutator
public void setCurrentDate(String d){
    currentDate = d;
    }

//Mutator
public void setLibraryBooks(ArrayList<Book> b){
    libraryBooks = b;
    }

//Mutator
public void setPatrons(ArrayList<Person> p){
    patrons = p;
    }
@Test
public int checkNumCopies(String title, String author){
    int numCopies = 0;
    for (Book b1 : libraryBooks){
        String t = b1.getTitle();
        String a = b1.getAuthor();
        if (t == title && a == author){
            numCopies++;
        }
    }
    for (Person p1 : patrons){
        ArrayList<Book> books = p1.getCheckedOut();
        for (Book b1: books){
            String t = b1.getTitle();
            String a = b1.getAuthor();
            if (t == title && a == author){
                numCopies++;
        }
        }
        }
    return numCopies;
   }


public int totalNumBooks(){
    int numCopies = 0;
    for (@SuppressWarnings("unused") Book b1 : libraryBooks){
            numCopies++;
        }
    for (Person p1 : patrons){
        ArrayList<Book> books = p1.getCheckedOut();
        for (Book b1: books){
            numCopies++;
        }
        }
    return numCopies;
}

public boolean checkOut(Person p, Book b, String dueDate){
    ArrayList<Book> removeBooks = new ArrayList<Book>();
    if (libraryBooks.contains(b) && patrons.contains(p) && b.isCheckedOut()== false){
        for (Book book1 : libraryBooks){
            if (book1.equals(b)){
                p.addBook(book1);
                removeBooks.add(book1);
                book1.setDueDate(dueDate);
                b.setCheckedOut(true);
                System.out.println("Checked out!");
                break;
            }}
        for (Book b2 : removeBooks){
            libraryBooks.remove(b2);    
        }
        return true;
    }
    else{
        System.out.println("Not checked out!");
        return false;
    }
    }

1 个答案:

答案 0 :(得分:0)

我不认为这会解决问题,但值得一试。

if (book1.equals(b)){
        p.addBook(book1);
        removeBooks.add(book1);
        book1.setDueDate(dueDate);
        b.setCheckedOut(true);
        System.out.println("Checked out!");
        break;
    }

看看那一行

b.setCheckedOut(true);

它将输入书籍的状态从false更改为true,但不会将库中书籍的状态从false更改为true。

更正后的内衬应为

book1.setCheckedOut(true);

如果该修复无效,您是否也可以添加测试用例?我无法复制您的问题,因为您的代码似乎对我来说很合适。