Hi all,
I don't know how to write this logic in solving sell() method.
I have two classes Coffee and Batch.
Coffee has a vector of Batch.
and they have aggregation relationship.
I solved it like this but it doesn't seem to work, can you help me please?
(here are some codes from my program).
there are comments in both sell() methods to describe what I want to do to solve it.
And I got an error in batchtemp.removeElementAt(0);
what's the problem ??
it says: "cannot find symbol method removeElementAt(int)"
Class Coffee
{ private String CoffeeName;//coffee name
private Vector mybatch;//vector of batches
public double getTotalStock()
{
//calculate the total stock
double total = 0;
for(int i =0;i<mybatch.size();i++) {
Batch batch = (Batch)mybatch.elementAt(i);
total += batch.getStock();
}
return total; }
public double sell (double amount)
{
//reduce the amount from the stock and return any amount not available
if (amount > getTotalStock())
{System.out.println("The amount " +amount+" is not available, please reduce the amount");}
////////////////
Batch batchtemp = (Batch)mybatch.elementAt(0);
if (amount<batchtemp.getStock())
{batchtemp.sell(amount);}
//////////////
if (amount>batchtemp.getStock())
{ double newValue=0;
Batch batchtemp1 = (Batch)mybatch.elementAt(1);
newValue = batchtemp.getStock() + batchtemp1.getStock();
batchtemp.removeElementAt(0);
Batch anotherBatch = (Batch)mybatch.elementAt(0);
anotherBatch.setStock(newValue);
if (amount<anotherBatch.getStock())
{batchtemp.sell(amount);}
}
return amount; }
class Batch
{
private double stock;
public double sell(double amount)
{
// update the inventory when sell operation is occured by subtracting the amount required from the
// most old batch, 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 subamount=0;
if (amount<stock)
{ subamount = stock - amount;
System.out.println(+amount+"Amount Sold");
setStock(subamount);}
else
{return amount;}}
}
Thank you