Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2007, 09:47 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
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)"

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-25-2007, 12:29 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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) + "]"; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-26-2007, 09:51 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Thanks a lot hardwired I managed to solve my problem
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Suggestions required for solving a Java problem bilal_ali_java Advanced Java 3 08-16-2008 03:11 AM
Vectors of Vectors or hash-somethings? mindwarp New To Java 3 03-10-2008 04:57 PM
Help with Vectors and Strings... kaban New To Java 2 12-09-2007 11:04 AM
Understanding Vectors cbrown08 New To Java 7 11-05-2007 08:56 PM
Using Vectors in Java JavaForums Java Blogs 0 11-04-2007 09:31 PM


All times are GMT +3. The time now is 03:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org