0% found this document useful (1 vote)
154 views1 page

This Study Resource Was

The document defines a Register class with a getTotalBill method that calculates the total bill for items in a map. The getTotalBill method iterates through the itemDetails map, looks up the price per unit for each item from a stocks map, multiplies the price by the quantity, and returns the sum of these calculations as a string. It uses singleton design pattern with a private constructor and public static getInstance method to return a single instance of Register.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
154 views1 page

This Study Resource Was

The document defines a Register class with a getTotalBill method that calculates the total bill for items in a map. The getTotalBill method iterates through the itemDetails map, looks up the price per unit for each item from a stocks map, multiplies the price by the quantity, and returns the sum of these calculations as a string. It uses singleton design pattern with a private constructor and public static getInstance method to return a single instance of Register.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.io.

*;
import java.util.*;
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 String 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);

m
// Write your code here

er as
double sum = 0;

co
eH w
for (Map.Entry<String, Integer> entry : itemDetails.entrySet()) {
sum += stocks.get(entry.getKey().trim().toLowerCase())

o.
*entry.getValue(); rs e
}
ou urc
return sum+"";
}
o

}
aC s
v i y re

public class Solution {


public static void main(String[] args) throws IOException {

Scanner readInput = new Scanner(System.in);


String[] input=readInput.nextLine().split(" ");
ed d

Map<String,Integer> myItems=new HashMap<String,Integer>();


ar stu

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));
sh is

readInput.close();
Th

}
}

This study source was downloaded by 100000825999107 from CourseHero.com on 06-06-2021 01:38:34 GMT -05:00

https://www.coursehero.com/file/75165940/class-answertxt/
Powered by TCPDF (www.tcpdf.org)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy