java - 比较两个映射的两个密钥对
问题描述
getInstance()
在注册类中创建一个函数。使用声明并初始化为 null 的寄存器变量返回寄存器类的单例实例。- 在下面的编辑器中完成
getTotalBill
功能。该函数必须说明必须返回的内容。该函数具有以下参数itemDetails
:字符串键和整数值的键/值对
登记册包含物品清单及其价格。在本练习中,物品清单及其价格为:
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();
}
}
解决方案
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();
}
}
推荐阅读
- java - IntelliJ 中的 Maven 测试结果
- jsf - JSF 在一个数据表中显示两个列表
- laravel - 在请求中检查 Laravel 中的验证
- python - 如何在 Python 中编写一个函数来打印一个月的一周?
- flutter - 如果我单击设备后退按钮,它应该重定向到上一页,但它直接转到应用程序的主页
- c++ - 如何允许转换为私有继承基类的基类?
- javascript - 在 iframe onLoad 事件中禁用 window.print() 调用,该事件会动态添加到页面上
- php - 带有验证码的联系表格 - 如何添加文件上传部分?
- image-processing - 如何在 Python 中使用显卡生成 HDR 图像
- java - 请求处理失败;嵌套异常是 org.springframework.transaction.CannotCreateTransactionException