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

标签 java

我正在从事图书馆管理系统项目。下面是我的 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/

相关文章:

java - 第二次设置JPanel对象的背景

java - WeakHashMap 和 WeakReference

java - 使用来自 hadoop reduce 的复合主键插入到 cassandra 表

java - Java 的艺术与科学第 4 章,练习 8

java - 如何使用 java 在 linux 中从/dev 端口捕获视频

java - Android-利用Canvas优化画线

java - 在 servlet 外部访问 session 变量

java - 我的 java 应用程序是否存在内存泄漏

java - 运行 Java 解释器以将控制台输出作为字符串返回

java - 如何设置透明背景?