【问题标题】:How can I map a string and compare with another string?如何映射一个字符串并与另一个字符串进行比较?
【发布时间】:2016-12-18 07:26:26
【问题描述】:

这个问题可能很愚蠢,但我无法做到这一点。我有一个通过蓝牙连接到我的应用程序的支付设备。为了让设备显示货币代码,我需要传递一个字符串,如下所示:

String codeForPaymendDevice = "978";

这个“978”基本上发送“EUR”货币代码并显示在支付设备的屏幕上。 (设备的库映射并处理这个)。在我的应用程序中,当用户以欧元货币进行购买时,它应该与“978(EUR)”进行比较,如果两者都匹配,它应该解析 codeForPaymentDevice。我无法做到这一点,因为我无法将“EUR”与 978 进行比较(因为我的代码不知道 978 是 EUR,只有支付设备知道 978 是 EUR)。

我需要做的是,将“978”映射到“EUR”代码,然后将transaction.getCurrencyCode()与映射的变量进行比较,然后解析它。

private SaleRequest buildTransactionRequest(Transaction transaction) {
        final SaleRequest tr = new SaleRequest();
        BigDecimal amount = getAmountPaid();
        String codeForPaymentDevice = "978";
        String formattedAmount;
        try {
            if (!transaction.getCurrencyCode().equalsIgnoreCase(CREW_CURRENCY)) {
                formattedAmount = AirFiApplication
                        .getPriceFormatter(AirFiApplication.getCurrency(transaction.getCurrencyCode())).format(amount);

                // transaction.getCurrencyCode = EUR 

                tr.setCurrency(codeForPaymentDevice); // TODO remove hardcoding
            } 

        } catch (MPosValueException e) {
            LOG.error("Error while building transaction request due to {}", e.getLocalizedMessage());

        }
        return tr;
    }

【问题讨论】:

    标签: android string bluetooth compare


    【解决方案1】:

    您可以创建一个Map,并带有一些您需要作为值传递的值(目前我使用货币代码)。

    Map<String, String> map = new HashMap<>();
    map.put("EUR", "978");
    map.put("INR", "979");
    map.put("USD", "980");
    map.put("AED", "981");
    ...
    

    在您的代码中,

    tr.setCurrency(map.get(transaction.getCurrencyCode()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2011-08-24
      • 2013-01-20
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多