Calihog Calihog - 1 year ago 57
Java Question

Frustrating logical error while iterating through containers in Java

I'm trying to make a simple "pharmacy delivery" program in Java, but I'm stuck whole day on this logical error and I just can't wrap it around. I'm pretty sure It's something very simple, but I just don't understand it - would really appreciate some help if someone can solve this.

All in all, I have 3 classes - Drug, Supplier, and Order.

Drug holds info about drugs (name and price).
Supplier holds some other info including a Map with (Drugs, quantity).
Order holds a Map with ordered and Vector with (Suppliers).

The basic idea is to fill the 2 containers of the Order class with a .txt file. The syntax of a single line goes like:
- Ordered_drugs_name Ordered_drugs_quantity (put those 2 in the Map of orders) .. and the rest is similar parameters to initialize the Suppliers object and put it in the vector.

Now, after I've filled these 2 containers I have a method that takes (or at least I think it does) the first drug from the orders Map and iterates over the suppliers vector to find if there is any suppliers that have such quantity of this drug. Of course, when It starts to iterate the suppliers it must check if the current supplier has the drug, so it starts iterating from the supplier's Map of drugs that he has.

The problem is that with a simple example of 2 suppliers and 2 orders from which only 1 of the drugs is sold by 1 of the suppliers and he has the needed quantity, I'm receiving both suppliers to be printed out (the one doesn't even sale this drug).

Here is some code, I'm sorry for the long post. :(

public class Supplier {
//some private members goes here

private static Map <Drug, Integer> listOfDrugs = new HashMap <Drug, Integer>();

Supplier(String n, String rep, String repPhoneNum, String drugName, double drugPrice, int stock) {
this.supplierName = n;
this.representative = rep;
this.representativesPhoneNumber = repPhoneNum;
listOfDrugs.put(new Drug(drugName, drugPrice), stock);

public boolean isDrugInStock(Drug drug, int quantity) {
int stock;
for (Entry<Drug, Integer> entry : listOfDrugs.entrySet())
if(entry.getKey().getDrugsName().equalsIgnoreCase(drug.getDrugsName())) {
stock = (int) listOfDrugs.get(entry.getKey());
if(stock >= quantity) {
return true;
return false;

and the Orders class:

public class Orders {
private Map <Drug, Integer> orderedDrugs = new HashMap <Drug, Integer>();
private Vector<Supplier> suppliers = new Vector <Supplier>();

Orders(String fileName) throws IOException {
//the reading form a .txt file goes here

public String order() {
for (Entry<Drug, Integer> entry : orderedDrugs.entrySet()) {
int quantity = orderedDrugs.get(entry.getKey());
for(Supplier s : suppliers) {

if(s.isDrugInStock(entry.getKey(), quantity)) {
return "";

Answer Source

Your Supplier class has a static (and therefore global) listOfDrugs.

I think what you want to have is

public class Supplier {
    //some private members goes here 
    private Map <Drug, Integer> listOfDrugs = new HashMap <Drug, Integer>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download