Searching for an object and deleting it

Stuck on an exercise where i have two classes, Book and Library.

Page 1 of 1

3 Replies - 552 Views - Last Post: 10 May 2009 - 06:33 AM Rate Topic: -----

#1 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Searching for an object and deleting it

Post icon  Posted 09 May 2009 - 04:33 PM

Ok i've created a Book class which contains fields such as Author/title/reference number. It also has accessor methods to return info and mutators to change the objects.

I have now been trying to create a library class for the past 6 hours, I've spent a few hours searching the forums for what to do but i still cant even work it out.

1. the library class needs to be able to add books to it (which i think I've succeeding in doing).
2. Then i need to be able to search for these books by the String reference number.

This is what I've done for part 1. I've no idea if it's right, i also know it's very simple.
public class Library
{
	private ArrayList<Book>books;
	public Library()
	{
		books = new ArrayList<Book>();
	}	
	//********
	//ADD ITEM
	//********   
	public void addBook(Book theBook)
	{
		books.add(theBook);
	}



Ok then i'm completely stuck on how to actually then search the reference numbers for a string then delete that book.
I'm not looking for any code(Unless it's just an example - i don't wanna copy and paste) as i wanna be able to write it myself. Just looking for i should be using to achieve this e.g. what packages to be using.

Should i be using an arrayList?
how do i get the users input?
i'm guessing i need some kind of loop to search through the elements, what kind?
Any other useful information would be very grateful. This is also my first time using the forums so i'm sorry if i posted this in the wrong place :)

From Mark.

Is This A Good Question/Topic? 0
  • +

Replies To: Searching for an object and deleting it

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Searching for an object and deleting it

Posted 09 May 2009 - 05:39 PM

ArrayList is good to use as you dont have to specify the size. User input you can get through a gui interface, or if your new, just use a JInputDialog or command line. The way you add a book to your ArrayList should be fine. To search, the best thing to do is create a search method.
private int searchMethod(String reference) {
		
		String reference= reference;

			for(int x=0; x<books.size(); x++) {
				
				Book oneBook= books.get(x); //you want to do a check on one book at a time
				
				 	if(oneBook.getReference().equalsIgnoreCase(reference)) {
			 		return x;
		 		}
			}
			
		return -1;	 	
			 		
	}


Now when you use this method, if it returns a number, thats the position in the ArrayList it found your reference, if it returns -1, it didnt find it. To use this method, you might do somthing like
System.out.println("Enter Reference");
String reference = input.next();

int result = searchMethod(reference);
if(result==-1) {
		System.out.println("Not found");
		 } 
else{
books.remove(result)
}

Was This Post Helpful? 1
  • +
  • -

#3 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Searching for an object and deleting it

Posted 09 May 2009 - 05:44 PM

thanks loads, exactly what i needed to get me going again. Got some other stuff to do but it involved searching so i can get on with that now as well. :^:

EDIT*
I've got the search part working nicely, when i try and add the second part in i'm confused about the " String reference = input.next();"
Do i need to define input somewhere? As it says "cannot find symbol - variable input".

This post has been edited by markeatworld: 09 May 2009 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Searching for an object and deleting it

Posted 10 May 2009 - 06:33 AM

Ok i slept on it, now i got the searchMethods working for reference number / title/ author. Now I need some help understanding the second piece of code. when i try and add the second part in I'm confused about the " String reference = input.next();"
Do i need to define input somewhere? As it says "cannot find symbol - variable input"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1