I'm trying to check whether an array of objects contain a specific string.
This is the constructor I have for Product object:
public Product()
{
name = "No name yet";
demandRate = 0;
setupCost = 0;
unitCost = 0;
inventoryCost = 0;
sellingPrice = 0;
}
This is the initialisation of the array:
Product[] product = new Product[3];
I found similar questions here Checking if long is in array and here Look if an array has an specified object. So I tried this code:
public boolean isAProduct(String nameOfProduct)
//Returns true if a name has been found otherwise returns false
{
boolean found = false;
int counter = 0;
while (!found && (counter < MAXNUMBEROFPRODUCTS))
{
if (Arrays.asList(product).contains(nameOfProduct))
{
found = true;
}
else
{
counter++;
}
}
return found;
}
But this doesn't work as it allows me to enter the same name for a product twice. So my question is, is what I'm attempting even possible? If not, how could I go about solving the problem?
Any advice would be greatly appreciated.