Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on library management system project. Below is my LibraryCollection class. I would like to call my checkOutMaterial() method in the main class.

I need help on how to call the method.

public class LibraryCollection {

    private int collectionMaxSize;
    private Material[] libraryCollection;

    public LibraryCollection(int theMaxSize) {
        collectionMaxSize = theMaxSize;
        libraryCollection = new Material[collectionMaxSize];
    }

    public LibraryCollection(int theCollectSize, Material[] theArray) {
        collectionMaxSize = theCollectSize;
        libraryCollection = theArray;
    }

//Material ID & checkedOutPtron ID; 
    public boolean checkOutMaterial(String matrlID, String patronId) {
        Material thisMaterial = findMaterial(matrlID);
        if (thisMaterial == null) {
            System.out.println("The material doesn't exist");
            return false;
        }
        if (thisMaterial.checkedOut()) {
            System.out.println("The material has been already checked out ");
            return false;
        }
        thisMaterial.setCheckedOut(true);
        thisMaterial.setPatronCheckout(Integer.parseInt(patronId));//Convert string value into int

        return true;
    }
}

Material Class

public class Material {

    private static int materialID = 0;
    private int mtrId;
    private String title;
    private boolean checkedOut;
    private int checkedOutPatron;

    public Material() {
        mtrId = 0;
        title = "";
        checkedOut = false;
        checkedOutPatron = 0;
    }

    public Material(int theId, String theTitle) {
        mtrId = theId;
        title = theTitle;
    }

//Getter Method 
    public String getMaterialId() {
        return mtrId + "";
    }

    public String getTitle() {
        return title;
    }

    public void setCheckedOut(boolean theCheckout) {
        checkedOut = theCheckout;
    }

    public void setPatronCheckout(int patronCheckout) {
        checkedOutPatron = patronCheckout;
    }

    public boolean checkedOut() {
        return checkedOut;
    }

    public int getCheckedOutPatron() {
        return checkedOutPatron;
    }

//ToString Method 
    public String toString() {
        return " \nMaterial ID: " + mtrId + " \nMaterial Title: " + title + " \nChecked Out: "
                + checkedOut + " \nPatron check out: " + checkedOutPatron;
    }

    public static int getNextID() {
        materialID++;
        return materialID;
    }
}


What I have tried:

public static LibraryCollection librarycollectObj1 = new LibraryCollection(10);
Posted
Updated 24-May-18 5:36am
v2
Comments
wseng 24-May-18 11:38am    
Where is your main method ?
Member 13841462 25-May-18 1:17am    
import java.util.Scanner;

public class LibraryCollectionManager
{
public static Scanner keyboard = new Scanner(System.in);
public static PatronCollection patronObj = new PatronCollection(10);
public static LibraryCollection librarycollectObj = new LibraryCollection(10);
public static FileManager file = new FileManager();


public static void main(String[]args)
{
printMainMenu();
}


public static void printMainMenu()
{
while(true)
{
System.out.println("----------MAIN MENU----------");
System.out.println("1.Manage Material");
System.out.println("2.Manage Patrons");
System.out.println("3.Load/Save Data");
System.out.println("4.Exit");
System.out.println("------------------------");

System.out.println("Enter your choice :"); //read user choice
int userChoice = keyboard.nextInt();//returns the userChoice value


switch(userChoice)

{
case 1:
materialSubMenu();
break;

case 2:
patronSubMenu();
break;

case 3:
dataSubMenu();
break;

case 4:
System.exit(0);
break;

default:
System.out.println("This is not a valid entry, try again please");
break;

}//End switch
}//End While

}//End printMainMenu

public static void materialSubMenu()
{


while(true)
{
System.out.println("----------MATERIAL MENU----------");
System.out.println("1.Display all materials");
System.out.println("2.Find materials");
System.out.println("3.Checkout a material");
System.out.println("4.Return a checked out material");
System.out.println("5.Add a new material");
System.out.println("6.Remove a material");
System.out.println("7.Return to main menu");
System.out.println("------------------------");

System.out.println("Enter your choice :"); //read user choice
int userChoice = keyboard.nextInt();//returns the userChoice value


switch(userChoice)
{
case 1:
librarycollectObj.displayMaterials();
break;

case 2:
String test = "search";
Material materialObj = new Material();
materialObj = librarycollectObj.findMaterial(test);
break;

case 3:
boolean check;
String str1 = "test";
String str2 = "test";
check = librarycollectObj.checkOutMaterial(str1, str2);
break;

case 4:
break;

case 5:
addMaterialSubMenu();
break;

case 6:
removeMaterialSubMenu();
break;

case 7:
printMainMenu();
return;

default:
System.out.println("This is not a valid option, try again");
break;
}//End switch
}//End while
}//materialSubMenu

//Add Material Sub-Menu
public static void addMaterialSubMenu()
{
while(true)
{
System.out.println("----------ADD MATERIAL SUB-MENU----------");
System.out.println("1.Add book");
System.out.println("2.Add magazine");
System.out.println("3.Add video");
System.out.println("4.Return to main menu");
System.out.println("------------------------");

System.out.println("Enter your choice :"); //read user choice
int userChoice = keyboard.nextInt();//returns the userChoice value


switch(userChoice)
{
case 1:
Book book1 = new Book();
boolean successfullyAdded = librarycollectObj.addBook(book1);
if(successfullyAdded == true)
{
System.out.println("> You have successfully added the book to the library!\n");
}
else {
System.out.println("The library is full and book was not added! " );
}
break;

case 2:
Magazine mag1 = new Magazine();
successfullyAdded = librarycollectObj.addMagazine(mag1);
if(successfullyAdded == true)
{
System.out.println("> You have successfully added the magazine to the library!\n");
}
else {
System.out.println("The library is full and magazine was not added! " );
}
break;


case 3:
Video video1 = new Video();
successfullyAdded = libra
wseng 25-May-18 2:51am    
the code looked fine to me . Can you call that method now ?
Member 13841462 25-May-18 2:52am    
oh actually, I figured it out. Thanks. I will mark solved this question.
wseng 25-May-18 2:54am    
Alright.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900