将一个参数化方法从LibraryCollection类调用到主类

时间:2018-05-23 18:50:05

标签: java

我正在从事图书馆管理系统项目。下面是我的LibraryCollection类。我想在主类中调用我的findMaterials()和checkOutMaterial()方法。

我一直试图按照以下方法调用,但我在控制台中没有得到任何值。

public static LibraryCollection librarycollectObj1 = new LibraryCollection(10); 
String search = null; 
librarycollectObj1.findMaterial(search); 

感谢;

// LibraryCollection类     公共类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; 
    }


    //(1)----------------Find MATERIAL-----------------
    public Material findMaterial(String theFindMaterial)
    {
        if(theFindMaterial == null) 
        {
            return null; 
        }
        for(int i = 0; i < libraryCollection.length; i++)
        {
            if(libraryCollection[i] !=null && theFindMaterial.equals(libraryCollection[i].getMaterialId()))
            {
            return libraryCollection[i]; 
            }
        }
        return null;
    }

    //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; 
    }

//材料类

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;
        }
}

1 个答案:

答案 0 :(得分:0)

当你跑步时:

String search = null
librarycollectObj1.findMaterial(search);

你执行

public Material findMaterial(String theFindMaterial)
{
    if(theFindMaterial == null) 
    {
        return null; 
    }

theFindMaterial = searchsearch = null后,您退出方法而不做任何事情,因为theFindMaterial = null

你可以这样做:

public static LibraryCollection librarycollectObj1 = new LibraryCollection(10);

// Initialize the object somehow
for (int i = 0; i < 10; i++) {
    librarycollectObj1.libraryCollection[i] = new Material(i, "");
}

String search = "1"; 
// Do some null checking in production code
System.out.println(librarycollectObj1.findMaterial(search). getMaterialId());