Results 1 to 3 of 3
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default I need a help in solving this method using vectors

    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)"

    Java Code:
    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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.util.Vector;
    
    public class CoffeeRx
    { 
        private String CoffeeName;
        private Vector batches = new Vector();
    //    private Vector<Batch> batches = new Vector<Batch>();
    
        public double getTotalStock()
        {	
            //calculate the total stock
            double total = 0;
            for(int i = 0; i < batches.size(); i++)
            {
                 Batch batch = (Batch)batches.elementAt(i);
                 total += batch.getStock();
            }
            return total;
        }
    
        /**
         * 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
         */
        public double sell(double amount)
        { 
            //reduce the amount from the stock and return any amount not available
            double totalStock = getTotalStock();
            if (amount > totalStock)
            {
                System.out.println("We only have " + totalStock + " available");
            }
    
            // Try to fill the order
            double attemptToFill = amount;
            int count = 0;
            while(attemptToFill > 0 && count < batches.size())
            {
                Batch batch = (Batch)batches.elementAt(count);
                double stock = batch.get(attemptToFill);
                if (stock > 0)
                {
                    // We have used up the rest of this batch
                    // so remove it from batches Vector.
                    batches.remove(batch);
                    // Removing this element at count will move
                    // the others up, ie, their indices in batches
                    // will decrease by one, so decrement count.
                    count--;
                }
                attemptToFill = stock;
                count++;
            }
            // Return amount we could not provide.
            return attemptToFill;
        }
    
        public void addBatch(double  amount) {
            batches.add(new Batch(amount));
        }
    
        public static void main(String[] args)
        {
            // Test implementation of classes.
            CoffeeRx test = new CoffeeRx();
            // Build up inventory.
            double[] amounts = { 4.5, 6.2, 3.9, 4 };
            for(int j = 0; j < amounts.length; j++)
                test.addBatch(amounts[j]);
            System.out.println("totalStock = " + test.getTotalStock());
            test.printBatches("after initialization");
            // Sell some coffee.
            double[] orders = { 2.3, 3.7, 2.1, 2.5 };
            for(int j = 0; j < orders.length; j++) {
                double unavailable = test.sell(orders[j]);
                System.out.printf("unavailable = %.1f  total remaining = %.1f%n",
                                   unavailable, test.getTotalStock());
                test.printBatches("after selling " + orders[j]);
            }
            // Clean 'em out.
            double unavailable = test.sell(9.0);
            System.out.printf("unavailable = %.1f  total remaining = %.1f%n",
                               unavailable, test.getTotalStock());
            test.printBatches("after selling " + 9.0);
        }
    
        private void printBatches(String s)
        {
            System.out.println("batches " + s);
            for(int j = 0; j < batches.size(); j++)
            {
                System.out.printf("%19s%n", (Batch)batches.get(j));
            }
            System.out.println("-------------------");
        }
    }
    
    class Batch
    {
        private double stock;
    
        Batch(double amount)
        {
            setStock(amount);
        }
    
        public double get(double amount)
        {
            double excessAmount = 0;
            if (amount < stock)
            {
                double newAmount = stock - amount;
                setStock(newAmount);
            }
            else    // amount >= stock
            {
                excessAmount = amount - stock;
                setStock(0);
            }
            return excessAmount;
        }
    
        public double getStock() { return stock; }
    
        public void setStock(double amt)
        {
            this.stock = amt;
        }
    
        public String toString()
        {
            return "Batch[stock: " + String.format("%.1f", stock) + "]";
        }
    }

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

Similar Threads

  1. Suggestions required for solving a Java problem
    By bilal_ali_java in forum Advanced Java
    Replies: 3
    Last Post: 08-16-2008, 01:11 AM
  2. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM
  3. Help with Vectors and Strings...
    By kaban in forum New To Java
    Replies: 2
    Last Post: 12-09-2007, 09:04 AM
  4. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •