logic - Frustrating logical error while iterating through containers in Java -


i'm trying make simple "pharmacy delivery" program in java, i'm stuck whole day on logical error , can't wrap around. i'm pretty sure it's simple, don't understand - appreciate if can solve this.

all in all, have 3 classes - drug, supplier, , order.

drug holds info drugs (name , price). supplier holds other info including map (drugs, quantity). order holds map ordered , vector (suppliers).

the basic idea fill 2 containers of order class .txt file. syntax of single line goes like: - ordered_drugs_name ordered_drugs_quantity (put 2 in map of orders) .. , rest similar parameters initialize suppliers object , put in vector.

now, after i've filled these 2 containers have method takes (or @ least think does) first drug orders map , iterates on suppliers vector find if there suppliers have such quantity of drug. of course, when starts iterate suppliers must check if current supplier has drug, starts iterating supplier's map of drugs has.

the problem simple example of 2 suppliers , 2 orders 1 of drugs sold 1 of suppliers , has needed quantity, i'm receiving both suppliers printed out (the 1 doesn't sale drug).

here code, i'm sorry 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;     (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 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 .txt file goes here }  public string order() {     (entry<drug, integer> entry : ordereddrugs.entryset()) {         int quantity = ordereddrugs.get(entry.getkey());         for(supplier s : suppliers) {                  if(s.isdruginstock(entry.getkey(), quantity)) {                     system.out.println(s.tostring());                 }             }         }     return ""; } 

your supplier class has static (and therefore global) listofdrugs.

i think want have is

public class supplier {     //some private members goes here      private map <drug, integer> listofdrugs = new hashmap <drug, integer>();     ... } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -