Kun Silva
Kun Silva

Reputation: 29

Calling class methods from another class

I have created a class for a book with various field. I have then created a an array class for the library to store the book details. However I am not sure how to link them. I am looking to ultimately be able to search my array for all books with the same author surname for example. Should I somehow be calling methods from the book code to the library code?

This is my object class

public class Bookrecord
{
private int idnumber;
private String author;
private String title;
private String fiction;

public Bookrecord( int newidnumber, String newauthorname, String newtitlename, String newfictionname)
{
   idnumber = newidnumber;
   author = newauthorname; 
   title = newtitlename;
   fiction = newfictionname;
}

public int getidnumber()
{
    return idnumber;
}

public String getauthorname()
{
    return author;
}

public String getfictianname()
{
    return fiction;
}

public String gettitlename()
{
    return title;
}

public void setidnumber(int insertidnumber)
{
    idnumber = insertidnumber;
}

public void setauthor(String insertauthorname)
{
    author = insertauthorname;
}

public void setfictian(String insertfictionname)
{
    fiction = insertfictionname;
}

public void settitle(String inserttitlename)
{
    title = inserttitlename;
}

public void printBookrecord()
{
 System.out.println("The idNumber is " + idnumber + " The authorname is " + author + " The fictionname is " + fiction + " The titlename is " + title);
}
}

This is my array class

import java.util.ArrayList;

public class Libraryclass
{
// instance variables - replace the example below with your own
private ArrayList<String> member;
private ArrayList<String> bookrecord;
private ArrayList<String> libraryloan;

/**
 * Constructor for objects of class Loan
 */
public Libraryclass()
{
    // initialise instance variables
   member = new ArrayList<String>();
   bookrecord = new ArrayList<String>();
   libraryloan = new ArrayList<String>();
}

public void addMember(String newMember)
{
    member.add(newMember); 
}

public void bookrecord(String newrecord)
{
    bookrecord.add(newrecord);
}    

public void libraryloan(String newloan)
{
    libraryloan.add(newloan);
}   
}

Upvotes: 0

Views: 79

Answers (1)

Menelaos
Menelaos

Reputation: 26609

This reminds of an assignment for my first object oriented course. I get the feeling you also just started development and need some tips to get started.

Keep in mind we won't spoonfeed the answer to you since it is frowned upon to use stackoverflow to get complete answers to college assignments.

  1. Your library or (arrayclass) should store an Array of BookRecord objects. Currently your arraylists hold String Objects. You should re-read what objects and classes are, to better understand the underlying concepts. You want your Library to hold an arraylist of bookrecords.
  2. FYI: An Arraylists is a class available in java that allows the addition and removal of elements. It has some advantage related to arrays but also has some cons.
  3. Your library object will use 'for loops' or 'iterators' to go through your arraylist of records. In order to implement features such as search for book you need to learn how to iterate over elements in an arraylist to search for strings. Google is your friend here. Here is an example what someone in your position should search for:

-searching through an arraylist, java

-iterating over arraylist, java

-how to use indexof java stack overflow

Finally, it makes more sense to call methods of the BookRecords from the Libraryclass. A bookRecord has one and only one Library. A library has many books. Hence, the library will hold references (contain) the books and will call getters and setters on the books.

Upvotes: 1

Related Questions