hi all,
This loop is to check the date of the milk boxes if they are expired then delete this box and it should continue looping .. and move to the next box and compare again, to sell the amount of milk.
but, it doesn't continue the loop it just delete the first box because it is expired and that's it! I want it to move to the next boxes and check the condition and sell the amount.
here is my code, I hope you can see the problem.
public double sellMilk (double amount)
{
//reduce the amount from the stock and return any amount not available
// check the expirey date
for(int i=0;i<milkbox.size();i++)
{
mBox milk = (mBox)milkbox.elementAt(i);
double stock = milk.sellMilk(amount);
double stockvalue = milk.getStock();
Date ExpireDate = milk.getExDate();
if (ExpireDate.before(new Date()))
{
milkbox.removeElementAt(i);
System.out.println("it has expired date");
return amount; }
else if (stock > 0 && ExpireDate.after(new Date() ) || stockvalue ==0 )
{
milkbox.remove(milk);
amount = stock;
}
i++;
}
if(amount != 0)
System.out.println("The extra amount is "+amount+ " KG of milk boxes");
return amount; }
here is the other method in milkboxes class it might be wrong too, but i think the first code is wrong.
public double sellMilk(double amount)
{
// update the inventory when sell operation is occured by subtracting the amount required from the
// most old box, if it is not enough subtract from the next and so on.
//remove the batch that has zero stock
//sell returns the amount that is not available in stock
double excessAmount = 0;
if (amount < stock)
{
double newAmount = stock - amount;
setStock(newAmount);
}
else // amount >= stock
{
excessAmount = amount - stock;
setStock(0);
}
return excessAmount;
}
Thank you