I am currently working on a project that involves creating an array of objects(in this case, hardware tools from a created ToolItem
class), and then creating a class file manipulate this data. The class I am creating now, named HardwareStore
, has methods to search, insert and delete private data members for items in my array. Using a method named assign()
from the previously mentioned ToolItem
class, I call the set methods for each data member and assign them to a spot in the array. Assign looks like:
public void assign(int quality, String name, int id, int numInStock, double price)
{
setQuality(quality);
setToolName(name);
setID(id);
setNumberInStock(numInStock);
setPrice(price);
}
My insert method currently looks like:
public int insert(int quality, String name, int id, int numInStock, double price)
{
//testing the unique ID passed by the user,
//making sure there isn't an object in the
//array with the same ID
testArray = searchArray(id);
//array holds a max of 10 objects
if (numberOfItems == 10)
{
System.out.println("Array is full");
return 0;
}
//-1 is sentinel value from search method,
//telling me there isn't an object with the
//same specified ID
else if (testArray == -1)
{
for (index = 0; index < toolArray.length; index++)
{
if (toolArray[index].getToolID() == 0)
{
toolArray[index].assign(quality, name, id, numInStock, price);
numberOfItems++; //counter for array
return 1;
}
}//end for loop
}
return -1; //sentinel value telling me there was a dupe ID
}//end insert
I am supposed to validate the toolArray[index].assign(quality, name, id, numInStock, price);
using a boolean variable in this manner, though:
boolean oK = toolArray[index].assign(quality, id, numInStock, price);
If oK == true
, I then increment the number of items in the array. In order for this to work, I would need assign()
to have a return type of boolean
. This is how it was explained to me:
Yes you will want an Assign method. All that goes into it are calls to "set" values to there appointed places. The assign method will return a value depending on whether or not the value was assigned/inserted. You will need to check the value of oK to make sure it is true or false.
My issue is, I do not know how to change the assign()
return type to boolean
and make the method work properly. My first thought was something like:
if (setQuality(quality) == true)
{
return true;
}
else if (setToolName(name) == true)
{
return true;
}
else
return false;
but this obviously doesn't work and results in several compiler errors :/ I just don't understand the logic behind this kind of data checking. If someone understands this and could help me, I would greatly appreciate it!