Home > Net >  How do I use HashMap values from other class?
How do I use HashMap values from other class?

Time:12-29

I need to access the values from one class within another class. In this example, I need to use the method .viewSupplies(); to list out all the values from the HashMap from the other class, Stock.java. Here's the code so far:

import java.util.HashMap;
import java.util.Map;

public class Employee {
    private int ID;
    private String name;
    private String phoneNumber;
    private String username;
    private String password;
    private Stock stock;
    HashMap<String, Integer> pro;

public Employee(int id, String n, String pho, String u, String pa, int sId){
    this.ID=id;
    this.name = n;
    this.phoneNumber=pho;
    this.username=u;
    this.password=pa;
    this.stock = new Stock(sId);
    this.pro =  stock.products;
    for(String k : stock.getProducts().keySet()){
        pro.put(k, stock.getProducts().get(k));
    }
}


public void viewSupplies(){
    for (String key : pro.keySet()){
        System.out.println(key   ": "   pro.get(key));
    }
}

}

And here's Stock.java:

import java.util.HashMap;
import java.util.Map;

public class Stock {
private int id;
public HashMap<String, Integer> products;

public Stock(int i){
    this.id = i;
    this.products = new HashMap<String, Integer>();
}

public void addProduct(String pName, int quantity){
    if(!products.containsKey(pName)){
        System.out.println("Error! Product not found.");
    }
    else{
        products.put(pName, quantity );
    }
}

public void deleteProduct(String pName) {
    if (!products.containsKey(pName)) {
        System.out.println("Error! Product not found.");
    } else {
        products.remove(pName);
    }
}

public void regProduct(String pName){
    products.put(pName, 0);
}

public void takeProduct(String pName, int quantity) {
    if(!products.containsKey(pName)){
        System.out.println("Error! Product not found.");
    }
    else {
        products.put(pName, products.get(pName) - quantity);
    }
}

public void listProducts(){
    for (Map.Entry<String, Integer> p : products.entrySet()){
        System.out.println(p.getKey()   ": "   p.getValue());
    }
}

public void findProduct(String pName){
    if(products.containsKey(pName)){
        System.out.println(pName   ": "   products.get(pName));
    }
    else{
        System.out.println("Error! Product not found.");
    }
}

public HashMap<String, Integer> getProducts(){
    return products;
}

As you can see, I do not know how to get keys and values from the public HashMap products from Stock in Employee.

CodePudding user response:

You already have the reference to the stock instance in the Employee class. You can get the stock's product and use it.

public void viewSupplies() {
    Map<String, Integer> products = stock.getProducts();
    for (String key : products.keySet()){
        System.out.println(key   ": "   products.get(key));
    }
}

With this, I don't think you would have the need to maintain a HashMap in the Employee class. Also, it is recommended to make the products field private in the Stock class.

  • Related