1

As stated in the title how can I remove an attribute from an arrayList, my method is like this :

public boolean removeBorrower(String libraryNumber)

I basically have to check if the parameters in this method is equals to the attribute if yes then remove the borrower according to that.

Here is my code :

import java.util.*;
/**
* Write a description of class BorrowerList here.
* 
* @author xxxxxx 
* @version 08/11/2015
*/
public class BorrowerList
{
    private ArrayList<Borrower> borrowers;

    public BorrowerList()
    {
        borrowers = new ArrayList<Borrower>();
    }

    public void addBorrowers(Borrower borrower)
    {
        borrowers.add(borrower);
    }

    public void getAllBorrowers()
    {   
        for(Borrower borrower : borrowers)
        {
            borrower.printBorrowerDetails();
        }
    } 

    public void getBorrower(int borrowerEntry)
    {
        if(borrowerEntry < 0)
        {
            System.out.println("Negative entry :" + borrowerEntry);
        }
        else if(borrowerEntry < getNumberOfBorrowers())
        {
            Borrower borrower = borrowers.get(borrowerEntry);
            borrower.printBorrowerDetails();
        }
        else
        {
            System.out.println("No such entry :" + borrowerEntry);
        }
    } 

    public int getNumberOfBorrowers()
    {
        return borrowers.size();
    }

    public void removeBorrower(int borrowerEntry)
    {
        if(borrowerEntry < 0)
        {
            System.out.println("Negative entry :" + borrowerEntry);
        }
        else if(borrowerEntry < getNumberOfBorrowers())
        {
            borrowers.remove(borrowerEntry);
        }
        else
        {
            System.out.println("No such entry :" + borrowerEntry);
        }
    }

    public boolean removeBorrower(String libraryNumber)
    {
        borrowers.remove(libraryNumber);
    }

    public int search(String libraryNumber)
    {
        int index = 0;
        boolean found = false;
        while(index < borrowers.size() && !found)
        {
            Borrower borrower = borrowers.get(index);
            if(borrower.getLibraryNumber().equals(libraryNumber))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if (index < borrowers.size())
        {
            return index;
        }
        else
        {
            return -1;
        }
    }
}

Edited :

Here is the code of the borrower class itself

/**
* Write a description of class Borrower here.
* 
* @author xxxxxx
* @version 08/11/2015
*/
public class Borrower
{
    private String firstName;
    private String lastName;
    private String libraryNumber;
    private int noOfBooks;
    private Address address;

    /**
     * Constructor for objects of class Borrower.
     * The number of books should be set to 1.
     * 
     * @param firstName The Borrower's first name 
     * @param lastName The Borrower's last name
     * @param lNumber The Borrower's library number
     * @param street The Borrower's street
     * @param town The Borrower's town
     * @param postcode The Borrower's postcode
     */
     public Borrower(String fName, String lName, String lNumber, 
                String street, String town, String postcode)
    {
        firstName = fName;
        lastName = lName;
        libraryNumber = lNumber;
        noOfBooks = 1;        
        address = new Address(street, town, postcode);
    }

    /**
     * Constructor for objects of class Borrower.
     * The number of books on loan should should be set to
     * the supplied vale.
     * 
     * @param fName The Borrower's first name 
     * @param lName The Borrower's last name
     * @param lNumber The Borrower's library number
     * @param numberOfBooks The initial book borrow
     * @param street The Borrower's street
     * @param town The Borrower's town
     * @param postcode The Borrower's postcode
     */
     public Borrower(String fName, String lName, String lNumber, int       numberOfBooks, 
                String street, String town, String postcode)
     {
        firstName = fName;
        lastName = lName;
        libraryNumber = lNumber;
        noOfBooks = numberOfBooks;         
        address = new Address(street, town, postcode);
     }


    /**
     * Get the Borrower's first name
     * 
     * @return the Borrower's first name
     */
    public String getFirstName()
    {
        return firstName;
    }

    /**
     * Get the Borrower's last name
     * 
     * @return the Borrower's last name
     */
     public String getLastName()
     {
        return lastName;
     }

    /**
     * Get the Borrower's library Number
     * 
     * @return the Borrower's library number
     */
     public String getLibraryNumber()
    {
        return libraryNumber;
    }

    /**
     * Get the number of books on loan
     * 
     * @return the number of books on loan
     */
    public int getNoOfBooks()
    {
        return noOfBooks;
    }

    /**
     * Print out the Borrower's details to the console window
     * 
     */
    public void printBorrowerDetails()
    {
        System.out.println( firstName + " " + lastName 
                       + "\n" + address.getFullAddress()
                       + "\nLibrary Number: " + libraryNumber
                       + "\nNumber of loans: " + noOfBooks);
    }     



    /**
     * Increase the bumber of books on loan by 1
     * 
     */
    public void borrowBook()
    {
        noOfBooks = noOfBooks + 1;
        System.out.println("Books on loan: " + noOfBooks);        
    }

    /**
     * Increase the bumber of books on loan by a given number
     * 
     * @param number of new loans to add to total
     */
    public void borrowBooks(int number)
    {
        noOfBooks = noOfBooks + number;
        System.out.println("Books on loan: " + noOfBooks);        
    }

    /**
     * Return a book
     * 
     */
    public void returnBook ()
    {
        noOfBooks = noOfBooks - 1 ;
        System.out.println("Books on loan: " + noOfBooks);        
    }

    /**
     * Return the Borrower's address
     * 
     * @return the Borrower's address
     */
    public String getAddress()
    {
        return address.getFullAddress();
    }

    /**
     * Change the Borrower's address
     * 
     * @param street the street
     * @param town the town
     * @param postcode the postcode
     */
    public void setAddress(String street, String town, String postcode)
    {
        address.setFullAddress(street, town, postcode);
    }

    /**
     * Print out Borrower's address
     */
    public void printAddress()
    {
        address.printAddress();
    }

} // end class
TheHunter
  • 21
  • 6

2 Answers2

2

As per the JavaDoc:

public boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Which essentially means that borrowers.remove(libraryNumber); will work if and only if you have some Borrower object which is equal to that string. Unless you have added an override to the equals method in your Borrower class, it is unlikely that this will ever work. Thus, you have two options:

  1. Override the equals (and for good practice, the hashcode()) methods within your Borrower class such that two borrower items are considered equal if they have the same libraryNumber.

  2. The second option would be to use an iterator to go through your Borrower items stored within borrower and use the iterator's remove method to delete the objects which have the same property values.

As a side note, although the first approach might be more elegant for some, caution must be taken since in your particular scenario, Borrower objects might not be equal just because they have the same libraryNumber.

npinti
  • 51,780
  • 5
  • 72
  • 96
0
public boolean removeBorrower(String libraryNumber)
{
    boolean retVal = false;
    if(borrowers.contains(libraryNumber))
    {
       retVal = borrowers.remove(libraryNumber);
    } 
    return retVal;
}

Something like this perhaps? I know it's a long round-about way of doing things, since the easiest way would be

public boolean removeBorrower(String libraryNumber)
{
       return borrowers.remove(libraryNumber);
}

But perhaps I don't quite understand what's the problem you're having?

Shark
  • 6,513
  • 3
  • 28
  • 50
  • This wont work. He has to override equals and hashcode – Knu8 Nov 10 '15 at 15:41
  • Yep, I just re-read the opening post and figured out that the custom `Borrower` class probably doesn't have a good implementation of `equals()` and subsequently `hashCode()` with special attention on `equals()` Feel free to post a better implementation @Knu8 – Shark Nov 10 '15 at 15:48
  • The borrower class has been added :) – TheHunter Nov 10 '15 at 16:05