我正在从事图书馆管理系统项目。下面是我的LibraryCollection类。我想在主类中调用我的findMaterials()和checkOutMaterial()方法。
我一直在尝试按以下方法调用,但是在控制台中没有任何值。
public static LibraryCollection librarycollectObj1 = new LibraryCollection(10);
String search = null;
librarycollectObj1.findMaterial(search);
谢谢;
// LibraryCollection类
公共类LibraryCollection
{
private int collectionMaxSize;
私有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;
}
}
最佳答案
运行时:
String search = null
librarycollectObj1.findMaterial(search);
您执行
public Material findMaterial(String theFindMaterial)
{
if(theFindMaterial == null)
{
return null;
}
从
theFindMaterial = search
和search = 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());