Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: taking information from one array into another

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default taking information from one array into another

    I have 2 arrays that store members and books in a library, i want to use a 3rd array to take information out of those arrays. E.g take one member and one book and put them in one "Loan" slot to represent a member borrowing a book.

    Here is all my code

    Any help would be much appreciated.

    Thanks

    import java.util.ArrayList;
    /**
     * Write a description of class Library here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Library
    {
     ArrayList<Membership> member;
     ArrayList<Books> book;
     ArrayList<Loans>loan;
     
     private int nextMemberID;
     private int nextBookId;
     private int nextLoanId;
     
        /**
         * Constructor for objects of class Library
         */
        public Library() 
        {
     
           nextMemberID = 1;
           nextBookId = 1; 
           nextLoanId = 1;
     
           member = new ArrayList<Membership>();
           book = new ArrayList<Books>();
           loan = new ArrayList<Loans>();
     
        }
     
         /**
         * Return the lot with the given number. Return null
         * if a lot with this number does not exist.
         * @param lotNumber The number of the lot to return.
         */
        public void addnewMember( String first, String last, String telephone)
        {
     
        member.add(new Membership(nextMemberID,first,last,telephone));
        nextMemberID++; 
    }
     
    [SIZE="3"][B]public void addnewLoan(  )
    {
        loans.add(new Loans();[/B][/SIZE]   
     
    }
     
    public void listMembers()
    {
         for(Membership membership : member) {
     
             System.out.println(membership.toString());
     
            }
     
     
    }
     
     public void getMemberDetails(int ID)
        {
            for(Membership membership : member) {
                if(membership.getID() == ID){
                    System.out.println (membership);
     
            }
            }
            // if ID number does not exist then do nothing
        }
     
        public void getBookDetails(int bookid)
        {
            for(Books books : book) {
                if(books.getBookid() == bookid){
                    System.out.println (books);
     
            }
            }
            // if ID number does not exist then do nothing
        }
     
     
     
     
     
            /**
         * Return the lot with the given number. Return null
         * if a lot with this number does not exist.
         * @param lotNumber The number of the lot to return.
         */
        public Membership getMember(int ID)
        {
            for(Membership membership : member) {
                if(membership.getID() == ID){
                    return membership;
                } else if (membership.getID() > ID) {
                    System.out.println("Lot number: " + ID +
                                                "does not exist.");
     
     
                }
     
            }
                return null; //if there are no lots
        }
     
        /**     * Remove the lot with the given lot number. 
       * @param number The number of the lot to be removed   
       * @return The Lot with the given number, or null if 
       * there is no such lot.  
       */  
     
     
        public void removeMember(int ID) {
     
        //First we find the lot with the given number
        Membership membership = getMember(ID);
        if (membership != null) {
            //Then we can use the method remove with lot as argument
           member.remove(membership);
     
     
        }
     
    }
     
     
     
     
     
         /**
         * Return the lot with the given number. Return null
         * if a lot with this number does not exist.
         * @param boolean fiction IS the book fiction true or false.
         */
         public void addnewBook( String title, String author, boolean fiction)
        {
     
        book.add(new Books(nextBookId,title,author,fiction));
        nextBookId++; 
    }
     
     
    public void listBooks()
    {
         for(Books books : book) {
     
             System.out.println(books.toString());
     
            }
     
     
        }
     
     
     
     
        public Books getBookid(int bookid)
        {
            for(Books books : book) {
                if(books.getBookid() == bookid){
                    return books;
                } else if (books.getBookid() > bookid) {
                    System.out.println("Lot number: " + bookid +
                                                "does not exist.");
     
     
                }
     
            }
                return null; //if there are no lots
        }
     
     
          public void removeBook(int bookid) {
     
        //First we find the lot with the given number
        Books books = getBookid(bookid);
        if (books != null) {
            //Then we can use the method remove with lot as argument
           book.remove(books);
     
     
        }
     
    }
     
     
     
    public void searchForLastName(String lastname) { 
            int index = 0;   
            boolean found = false;    
         for(Membership membership : member) {  
        if(membership.getlastName().contains(lastname)) {     
     
     
            found = true;        }        
         else {     
             index++;        }  
     
     
        if(found) {    
     
        System.out.println (membership); 
     
          } else {   
     
          // if no matches are found then do nothing 
     
        }
     
    }
     
     
    }
     
    public void searchForBookTitle(String booktitle) { 
            int index = 0;   
            boolean found = false;    
         for(Books books : book) {  
        if(books.gettitle().contains(booktitle)) {     
     
     
            found = true;        }        
         else {     
             index++;        }  
     
     
        if(found) {    
     
        System.out.println (books); 
     
          } else {   
     
          // if no matches are found then do nothing 
     
        }
     
    }
     
    }
     
    }

    public class Books
    {
        // instance variables - replace the example below with your own
        private final int bookid;
        private String title;
        private String author;
        private boolean fiction;
     
        /**
         * Constructor for objects of class Booklist
         */
        public Books(int Id, String title, String author, boolean fiction)
        {
           this.bookid = Id;
           this.title = title;
           this.author = author;
           this.fiction = fiction; 
     
        }
     
         /**
         * @return The lot's number.
         */
        public int getBookid()
        {
            return bookid;
        }
     
        public String gettitle()
        {
           return title;
     
        }
     
     
     
        public String toString()
        {
            String details = bookid + ":   <<<Book ID      :Title " + title + "    :Author " + author + "       :Is the book fiction - True or False?    "+ fiction;
     
           return details;
        }
    }

    public class Membership
    {
     
        private String firstName;
        private String lastName;
        private String telephoneNumber;
        private int ID;
     
     
        /**
         * Constructor for objects of class Member
         */
        public Membership(int ID, String first, String last, String telephone)
        {
           this.ID = ID;
           this.firstName = first;
           this.lastName = last;
           this.telephoneNumber = telephone;
     
     
        }
     
        /**
         * @return The lot's number.
         */
        public int getID()
        {
            return ID;
        }
     
        public String getlastName()
        {
           return lastName;
     
        }
     
     
     
     
        public String toString()
        {
            String details = ID + ":   <<<Membership ID      :First Name " + firstName + "    :Second Name " + lastName + "    :Telephone Number " + telephoneNumber;
     
           return details;
        }
     
    }


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: taking information from one array into another

    Do you mean something like a HashMap where you could have the member as the key and the book as the value or vice versa like this?
    HashMap<Books,Membership> loans = new HashMap<Books,Membership>();
    loans.put(member.get(0),book.get(0);

    Or whichever members and books you want in there. Not sure if I've missed the point here.

Similar Threads

  1. can't get all the information out of my array at once... please help
    By Tate in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 06:22 AM
  2. taking JFrame title into account when calling pack()
    By gib65 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2010, 09:19 PM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  4. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 04:13 PM
  5. Passing Information between classes
    By SKhoujinian91 in forum Object Oriented Programming
    Replies: 4
    Last Post: December 8th, 2009, 03:40 PM