如何访问从另一个 class 中的文本文件导入的地图?

How do I access a Map that's imported from a text file in another class?

我有三个 classes(主要、产品和计数器)。 Product 创建一个名为 mProductMap 的新 TreeMap。 Product 还具有将 TreeMap 写入文本文件的导出方法。使用 import 方法从中读取。当我 运行 Main 时,文本文件已经创建。计数器 class 使用 TreeMap 对键的值进行计算。

Product 和 Counter 中的所有方法过去都在同一个 class 中,直到我决定将某些方法分开在它们自己的 class 中是更好的做法。我正在尝试使用 Product 中的 getter 方法访问 Counter class 中的 TreeMap,然后在其上使用 Counter 中的方法。

问题: 当一切都在同一个 class 时,一切正常。现在我将方法放在单独的 类 中,Counter 方法无法读取 TreeMap 中的内容。它returns else 语句:"Key1 does not exist. Please try again."

任何人都可以向我解释这是怎么回事以及如何解决这个问题吗? 我不知道这是否是重要信息,但是:classes 产品和计数器在同一个包中。主要导入该包。

主要class:

public class Main {
    public static void main(String[] args) {
        Product product = new Product();
        Counter counter = new Counter();
        product.importFrom("prices.txt");
        counter.add("Key1");
        counter.add("Key2");
        counter.add("Key3");
        product.exportTo("prices.txt");
    }
}

产品class:

public class Product {
    private TreeMap<String, BigDecimal> mProductMap = new TreeMap<>();

    public TreeMap<String, BigDecimal> getProductMap() {
        return mProductMap;
    }

    public void setProductMap(TreeMap<String, BigDecimal> productMap) {
        mProductMap = productMap;
    }

    public void addProduct(String product, BigDecimal price) {
        mProductMap.put(product, price);
    }

    public void exportTo(String fileName) {
        try (
                FileOutputStream fos = new FileOutputStream(fileName);
                PrintWriter writer = new PrintWriter(fos);
        ) {
            for(Map.Entry<String, BigDecimal> entry : mProductMap.entrySet()) {
                writer.printf("%s|%s%n",
                        entry.getKey(),
                        entry.getValue());
            }
        } catch(IOException ioe) {
            System.out.printf("Problem saving %s %n", fileName);
            ioe.printStackTrace();
        }
    }

    public void importFrom(String fileName) {
        try (
                FileInputStream fis = new FileInputStream(fileName);
                BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        ) {
            String line;
            while((line = reader.readLine()) != null) {
                String[] args = line.split("\|");
                addProduct(args[0], new BigDecimal(args[1]));
            }
        } catch(IOException ioe) {
            System.out.printf("Problems loading %s %n", fileName);
            ioe.printStackTrace();
        }
    }
}

计数器class:

public class Counter {
    private BigDecimal total = new BigDecimal("0.00");
    Product p = new Product();

    private TreeMap<String, BigDecimal> newMap = p.getProductMap();

    public void add(String product) {
        if (newMap.containsKey(product)) {
            BigDecimal price = newMap.get(product);
            total = total.add(price);
        } else {
            System.out.printf("%s does not exist. Please try again.%n", product);
        }
        System.out.printf("Adding %s. Total is %s%n", product, total);
    }
}

问题在这里(在 Counter 内):

Product p = new Product();

private TreeMap<String, BigDecimal> newMap = p.getProductMap();

Product 的实例(变量 p)与在 main 方法中加载文本文件的实例不同。 Product 的每个实例都有自己的 mProductMap 集合。

修复建议

您可以将 Product 的实例传递给 Counter 的构造函数,而不是让 Counter 创建 Product 的私有副本...

public class Counter {
    private BigDecimal total = new BigDecimal("0.00");
    private final Product p;

    public Counter(Product p) {
        this.p = p;
    }

    public void add(String product) {
        TreeMap<String, BigDecimal> pMap = p.getProductMap();
        if (pMap.containsKey(product)) {
            BigDecimal price = pMap.get(product);
            total = total.add(price);
        } else {
            System.out.printf("%s does not exist. Please try again.%n", product);
        }
        System.out.printf("Adding %s. Total is %s%n", product, total);
    }
}

然后在您的 main 方法中,像这样创建您的 Counter...

Counter counter = new Counter(product);