Results 1 to 5 of 5

Thread: ArrayLists

  1. #1
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default ArrayLists

    Hello, this is the second thread I make about this program. I made this because it involes a different problem. So, I am making a library class that has its books on 2 arraylists, one for the available books and anotherone for the ones on loan. The library can check out, check in, add, remove, and display books. But there's an extra method(checkInAll in the Library class), that let you check in all the books at once. I already made it, but it always leaves 1 book in the onLoan arraylist. I don't really know why, so I was wondering if anyone can please help me. I will display the code. If you run it; first, use options 1 and 2 to check the books i have on both arraylists. Then select option 7, and on the top of the new menu you will se that "there are 6 books on the library and 1 on loan" which should be 7 and 0.

    Library Class
    Java Code:
    /**
    * Library.java demonstrates use of ArrayLists
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    import java.util.*;
    public class Library {
    private ArrayList onShelf;              // instance variables: arraylists for books checked in and out
    private ArrayList onLoan;
    public Library() {						// Constructor for objects of class Library
    onLoan = new ArrayList();               // initialize onLoan ArrayList
    onShelf = new ArrayList();              // initialize onShelf ArrayList
    onShelf.add("Green Eggs and Ham");      
    onShelf.add("The Best Beaches in Mexico");
    onShelf.add("The fall of the House of Usher");
    onShelf.add("Rip van Winkle");
    onLoan.add("Java for Dummies");
    onLoan.add("Sleepy Hollow");
    onLoan.add("AP Java Methods");
    /* TASK #1 - ADD MORE BOOKS TO THE SHELF  */
    }
    public void checkInAll(){
    	if (booksOnLoan()>0){
    		String temp;
    		int index;
    		for (int a=0; a<booksOnLoan(); a++){
    			temp=(String)onLoan.get(a);
    			checkIn(temp);		
    		}
    		System.out.println("All books have been turned in");
    	}
    	else{
    		System.out.println("There are no books on loan");
    	}
    	
    }
    public int booksOnShelf(){
    	int num=onShelf.size();
    	return(num);
    }
    public int booksOnLoan(){
    	int num=onLoan.size();
    	return (num);
    }
    public boolean isCheckedOut(String title) {  // isCheckedOut() returns true if book is onLoan, false otherwise
    
    if (onLoan.contains(title)) {                // check the arraylist for title
       return true; 
       }                                
       else return false; // if not, test is false
    } // end of isCheckedOut()
    
    public void removeBook(String title){
    	int index = onShelf.indexOf(title);
    	onShelf.remove(index);
    }
    public void checkOut(String title) {			 //Move book to onLoan list
    
    if (isCheckedOut(title)) {						// see if already checked out
      System.out.println("We're sorry, that book is not available.\n");
      }
    else {											// not checked out
       int index = onShelf.indexOf(title); 			// get its index
       String temp = (String)onShelf.get(index);    // save the name
       onShelf.remove(index);                       // remove it from the onShelf list
       onLoan.add(temp);							// add it to the onLoan list
       System.out.println("Check out Succesfull!");
    }
    } // end of checkout()
    
    
    public void checkIn(String title) { 			// Move book to onShelf list
    
    if (isCheckedOut(title)) {  					// see if already checked out
    	int index = onLoan.indexOf(title); 			// get its index
    	String temp = (String)onLoan.get(index); 	// save the name
    	onLoan.remove(index); 						// remove it from the onShelf list
    	onShelf.add(temp); 							// add it to the onLoan list
    }
    else {
    	System.out.println("That book is not in loan");
    }
    } 
    
    public void addBook(String title) {				//Add new Book to onShelf list
    
    onShelf.add(title); 							// add it to the onLoan list
    } 	// end of addBook()
    /**
    * 
    */
    public void showBooksIn() { 					//Lists all books checked in
    	String temp;
    	System.out.println("CAT Library\nBooks checked in\n----------------\n");
    	Iterator iter = onShelf.iterator(); 		// create an iterator object
    	while (iter.hasNext()) {					// and keep going so long as there are more elements in the list
    	temp = (String)iter.next(); 				// get & convert current element
    	System.out.println(temp); 					// print it!
    	}
    } // end of showBooksIn()
    
    public void showBooksOut()	{					//Lists all books checked out
    
    	String temp;
    	System.out.println("CAT Library\nBooks checked out\n----------------\n");
    	Iterator iter = onLoan.iterator(); 			// create an iterator object
    	while (iter.hasNext()) {					// and keep going so long as there are more elements in the list
    	temp = (String)iter.next(); 				// get & convert current element
    	System.out.println(temp); // print it!
    	} // end while loop
    	
    } // end method ShowBooksOut
    
    } // end of Library class
    LibraryUser Class
    Java Code:
    /**
    * Library.java demonstrates use of ArrayLists
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    
    import java.util.*;
    import java.util.Scanner;
    
    class LibraryUser {
    	
    	public static void main(String[] args) {				    //  START MAIN METHOD
    		Scanner input=new Scanner(System.in);
    		Library biblio=new Library();
    		int option;
    		do{
    			System.out.println("\n\t\tCAT Library\n");
    			System.out.println("Currently, there are "+biblio.booksOnShelf()+" on the library and "+biblio.booksOnLoan()+" books on loan\n");
    			System.out.println("Choose one of the following options: ");
    			System.out.println("\t1.-Show books in");
    			System.out.println("\t2.-Show books out");
    			System.out.println("\t3.-Check Out");
    			System.out.println("\t4.-Check in");
    			System.out.println("\t5.-Add book");
    			System.out.println("\t6.-Remove book");
    			System.out.println("\t7.-Turn in all books");
    			System.out.println("\t8.-Exit\n");
    			System.out.print("Option: ");
    			option=input.nextInt();
    			input.nextLine();
    			switch (option){
    				case 1:
    					biblio.showBooksIn();
    					break;
    				case 2:
    					biblio.showBooksOut();
    					break;
    				case 3:
    					System.out.print("Enter name of book: ");
    					String CheckO=input.nextLine();		
    					biblio.checkOut(CheckO);
    					break;
    				case 4:
    					System.out.print("Enter name of book: ");
    					String CheckI=input.nextLine();
    					biblio.checkIn(CheckI);
    					break;
    				case 5:
    					System.out.println("Enter name of book: ");
    					String newB=input.nextLine();
    					biblio.addBook(newB);
    					break;
    				case 6:
    					System.out.println("Enter name of book: ");
    					String removeB=input.nextLine();
    					biblio.removeBook(removeB);
    					break;
    				case 7:
    					biblio.checkInAll();
    					break;
    				case 8:
    					System.out.println("Good bye!\n");
    					break;
    			}
    		}while(option!=8);
    	}
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Add some debug lines in the For loop to see what the variables are or better yet, debug through it if you are using an IDE.

    I suspect it has something to do with the way you structured your loop. I'm surprised its always leaving just one to be honest. Since you are changing the size of the loop you are stepping through inside the loop, the index in the for statement should soon be larger then what's left on loan.

    Instead switch it to a while loop
    Java Code:
         while (booksStillOnLoan()) {
              checkInBook();
         }
    This way you make sure that all the books are taken out of the List
    Last edited by StormyWaters; 11-12-2010 at 04:30 AM.

  3. #3
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    I did what u said, it gives be the same result.
    Java Code:
    public void checkInAll(){
    	if (booksOnLoan()>0){
    		String temp;
    		int index;
    		for (int a=0; a<booksOnLoan(); a++){
    			temp=(String)onLoan.get(a);
    			while (onLoan.contains(temp)){
    				checkIn(temp);
    			}
    		}
    		System.out.println("All books have been turned in");
    	}
    	else{
    		System.out.println("There are no books on loan");
    	}
    	
    }

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    You still have the for loop in there... Replace it with a while loop.

    Think it through.

    First For loop iteration: 3 books on loan, int a = 0; a < 3 = true; book removed
    Second For Loop iteration: 2 books on loan, int a = 1; a < 2 = true; book removed
    Third For Loop iteration: 1 book on loan, int a = 2; a < 1 = false; book NOT removed

  5. #5
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    I already solve it, it requires a do-while loop so that it worked. Thx a lot for ur help

Similar Threads

  1. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 04:15 AM
  2. Problem with ArrayLists
    By g2beastie in forum New To Java
    Replies: 7
    Last Post: 04-08-2010, 03:46 AM
  3. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 10:07 AM
  4. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 PM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •