首页 > 解决方案 > 比较两个映射的两个密钥对

问题描述

登记册包含物品清单及其价格。在本练习中,物品清单及其价格为:

Item   - Price
apple  - 2.0
orange - 1.5
mango  - 1.2
grapes - 1.0

输入:它包含具有购买项目(水果)列表及其数量的字符串。注意:水果详细信息的顺序可能会有所不同。

样本输入:苹果 30 橙 10 芒果 20 输出: 99.0

样本输入:橙 10 葡萄 52 苹果 14 输出: 95.0

我已经尝试过,但它没有给出正确的输出任何建议..

class Register {
    private static final Register register = new Register();
    /**
     * Complete the 'getTotalBill' function below.
     * The function is expected to return a STRING.
     * The function accepts MAP itemDetails as parameter.
     */
    public Register() {
    }

    public static Register getInstance() {
        return register;
    }

    public Double getTotalBill(Map<String, Integer> itemDetails) {
        Map<String, Double> stocks = new HashMap<>();
        stocks.put("apple", 2.0);
        stocks.put("orange", 1.5);
        stocks.put("mango", 1.2);
        stocks.put("grape", 1.0);
        // Write your code here
        double sum = 0;
        for (Map.Entry<String, Integer> entry : itemDetails.entrySet()) {
            for (Map.Entry<String, Double> entry1 : stocks.entrySet()) {
                if (entry.getKey() == entry1.getKey()) {
                    sum += entry.getValue() * entry1.getValue();
                }
            }
        }
        return sum;
    }
}
public class Solution {
    public static void main(String[] args) throws IOException {
        Scanner readInput = new Scanner(System.in);
        String[] input = readInput.nextLine().split(" ");
        Map<String, Integer> myItems = new HashMap<String, Integer>();
        for (int i = 0; i < input.length; i += 2) {
            myItems.put(input[i], Integer.parseInt(input[i + 1]));
        }
        Register regObj = Register.getInstance();
        System.out.println(regObj.getTotalBill(myItems));
        readInput.close();
    }
}

标签: javaarraysmultithreadingcollectionshashmap

解决方案


import java.io.*;
import java.util.*;
class Register {
    
    private static Register register = new Register();
    /*
     * Complete the 'getTotalBill' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts MAP itemDetails as parameter.
     */
     public Register(){
         
     }
     public static Register getInstance(){
         return register;
     }

    public Double getTotalBill(Map<String,Integer> itemDetails) {

        // Write your code here
        Map<String,Double> map = new HashMap<>();
        map.put("apple",2.0);
        map.put("orange",1.5);
        map.put("mango",1.2);
        map.put("grape",1.0);
        double sum  = 0.0;
        for(Map.Entry<String,Integer> entry: itemDetails.entrySet()){
            Double d = map.get(entry.getKey());
            if( d != null){
                sum += entry.getValue() * d;
            }
        }
    return sum;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        
        Scanner readInput = new Scanner(System.in);        
        String[] input=readInput.nextLine().split(" ");                
        Map<String,Integer> myItems=new HashMap<String,Integer>();
        for(int i=0;i<input.length;i+=2){
          myItems.put(input[i],Integer.parseInt(input[i+1]));   
        }
        Register regObj = Register.getInstance();        
        System.out.println(regObj.getTotalBill(myItems));
        readInput.close();
        
    }
}

推荐阅读