while (!matesStore.isAProduct(name))
//If a name has not been used, the name is added to the product
{
matesStore.addProduct(name); //!!!!I suspect the problem is this line.
}
The reason the 3rd product is infinite looping is because you should use If for this statement, while also work but you see there's an error is due to your method addProduct. If you change to If it won't be an infinite loop however your product 3 will be bugged.
public boolean isAProduct(String product)
//Returns true if a name has been found otherwise returns false
{
boolean found = false;
int counter = 0;
while (!found && (counter < MAXNUMBEROFPRODUCTS))
{
if (product.equalsIgnoreCase(product1.getName()))
{
found = true;
}
else if (product.equalsIgnoreCase(product2.getName()))
{
found = true;
}
else
{
counter++;
}
}
return found;
}//End of method
Noticed that you don't check for product3, so it will always return false when searching for your 3rd product name.
!matesStore.isAProduct(name) = !false = true
Viola!! There you goes it's while(true), an infinite loop!
Fix by implementing the product3.getName() in your isAProduct() method.
Your code is messy, you should use array for the products and you can do a better search with an array.
public boolean isAProduct(String product)
//Returns true if a name has been found otherwise returns false
{
boolean found = false;
int counter = 0;
while (!found && (counter < MAXNUMBEROFPRODUCTS))
{
if (product.equalsIgnoreCase(product1.getName()))
{
found = true;
}
else if (product.equalsIgnoreCase(product2.getName()))
{
found = true;
}
else if (product.equalsIgnoreCase(product3.getName()))
{
found = true;
}
else
{
counter++;
}
}
return found;
}//End of method