如何将值从哈希图解析为变量

时间:2018-12-04 14:47:17

标签: java class hashmap

因此,我正在尝试创建一个仅存储哈希图及其值的类,因为我将需要在某个时候从另一个类访问它们,并且它的值可能随时更改。以下是我正在尝试的示例

PriceInfo.java

public class PriceInfo {
   public static HashMap PriceInformation() {
       HashMap<String, Double> trainerPrice = new HashMap<>();
       trainerPrice.put("Nike", 199.99);
       trainerPrice.put("Adidas", 150.99);
       return trainerPrice;
   }
}

DiscountChecker.java

public class DiscountChecker {
    public boolean AllowDiscount(String discountCode, String tBrand) {
        if (discountCode.equals("Hello")) {
            double tPrice = PriceInfo.PriceInformation().get(tBrand);
            double discountedPrice = 0.8 * tPrice;
            return true;
        } else {    
            return false;
        }
    }
}

此刻,我不断收到错误消息,指出类型不兼容,并且需要double。

此行上的错误是double tPrice = PriceInfo.PriceInformation()。get(tBrand);

1 个答案:

答案 0 :(得分:2)

更改方法的签名
public static double PriceInformation ()

public static Map<String, Double> PriceInformation ()

在旁边:请遵循正确的命名约定,并更改方法名称以小写字母开头。