gpt4 book ai didi

java - 从 Library Collection 类调用主类的参数化方法

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:13 25 4
gpt4 key购买 nike

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

我一直在尝试按照下面的方法进行调用,但我在控制台中没有得到任何值。

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

谢谢;

//LibraryCollection类 公共(public)类LibraryCollection { 私有(private) int 集合最大大小; 私有(private) Material []库集合;

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

// Material 类

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 = 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());

关于java - 从 Library Collection 类调用主类的参数化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50495489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com