1
  • Create a function getInstance() in register Class. Return the singleton instance of a register class using the register variable which was declared and initiated to null.
  • Complete getTotalBill function in the editor below. The function must state what must be returned.The function has following parameter itemDetails: a key/value pair of string key and integer value

The register contains the list of items and their prices. In this exercise, the list of items and their prices are:

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

Input : It contains the string which have the list of purchased items (fruits) and their quantity .Note : The order of the fruit's details may vary.

Sample input: apple 30 orange 10 mango 20 Output: 99.0

Sample input: orange 10 grape 52 apple 14 Output: 95.0

I have tried but it is not giving proper output any suggestions..

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();
    }
}
Dante1501
  • 21
  • 3
  • 5
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Apr 26 '21 at 09:48
  • The issue is here `if(entry.getKey()==entry1.getKey()) {`, where you incorrectly compare `String`s. Check out the duplicate link for more info. – maloomeister Apr 26 '21 at 09:52
  • if(entry.getKey().equal(entry1.getKey())){ sum+=(entry.getValue()*entry1.getValue()); } – Dante1501 Apr 26 '21 at 10:13

2 Answers2

0

As maloomeister suggests, you are doing the comparison wrong. This checks if the two variables are the same object, or, in other terms, if they are stored at the same location in your RAM:

entry.getKey()==entry1.getKey()

What you want instead, is to check if the two variables contain the same value, and this must be done using equals:

entry.getKey().equals(getKey())

I also suggest you some other improvement:

  • compare keys, not entries;
  • use map.containsKey(key) instead of the inner loop, to improve performance

Put in code would be more less:

for (String key: itemDetails.keySet()){
  if (stocks.containsKey(key)){
    sum += itemDetails.get(key) + stocks.get(key);
  }
}

Looks cleaner, right?

  • 1
    Avoiding the nested loop is a big improvement, but there is no need to perform three hash lookups per iteration. `for(Map.Entry entry: itemDetails.entrySet()) { Double d = stocks.get(entry.getKey()); if(d != null) sum += entry.getValue() * d; }` performs only one lookup per iteration. Alternatively, you can use `for(Map.Entry entry: itemDetails.entrySet()) sum += entry.getValue() * stocks.getOrDefault(entry.getKey(), 0.0);` – Holger Apr 26 '21 at 15:42
0
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();
        
    }
}