java Double Map HashMap Integer Null

Complete the function, which calculates how much you need to tip based on the total amount of the bill and the service.
完成一个函数,基于总金额和服务水平评级,计算出你需要付出多少小费
You need to consider the following ratings:
你需要参照以下关于评级的比例:
Terrible: tip 0%
Poor: tip 5%
Good: tip 10%
Great: tip 15%
Excellent: tip 20%
The rating is case insensitive (so “great” = “GREAT”). If an unrecognised rating is received, then you need to return:
评级不区分大小写,如果接收到无法识别的评分,则需要返回:
“Rating not recognised” in Javascript, Python and Ruby…
…or null in Java
或 java里的 null
…or -1 in C#
Because you’re a nice person, you always round up the tip, regardless of the service.
由于你是个好人,不管服务如何,你通常会把小费凑整.

1:
public static Integer  method(double amount,String rating ){
         Double mid= count(rating);
         if(mid==null)return null;
         Double rate=amount * mid;
        return (int) Math.ceil(rate);
    }
    public static Double count(String rat)
    {
        switch (rat.toLowerCase()){
            case "terrible":
                return 0.0;
            case "poor":
                return 0.05;
            case "good":
                return 0.1;
            case "great":
                return 0.15;
            case "excellent":
                return 0.20;
            default:
                return null;
        }
    }
 2:
import java.util.*;
public class TipCalculator {
  public static Integer calculateTip(double amount, String rating) {
    switch(rating.toUpperCase()) {
    case "TERRIBLE": return 0;
    case "POOR": return (int)Math.ceil(0.05*amount);
    case "GOOD": return (int)Math.ceil(0.1*amount);
    case "GREAT": return (int)Math.ceil(0.15*amount);
    case "EXCELLENT": return (int)Math.ceil(0.2*amount);
    default: return null;
    }
   }
}
3:
 import java.util.Map;
 class MM{
	static Integer method(double amount,String rating){
	var tips=Map.of("terrible",0.0,"poor",0.05,"good",0.1,"great",0.15,"excellent",0.20);
	String rate=rating.toLowerCase();
	return tips.containsKey(rate)?(int)(Math.ceil(amount*tips.get(rate))):null;
}
}
4:
import java.util.*;

public class TipCalculator {
  
  private static Map<String, Double> ratings = Map.of("terrible", 0.0, "poor", 0.05, "good", 0.1, "great", 0.15, "excellent", 0.2);
  public static Integer calculateTip(double a, String r) {
    return ratings.get(r.toLowerCase()) == null ? null : (int)Math.ceil(a*ratings.get(r.toLowerCase()));
  }
}
5:
import java.util.HashMap;
import java.util.Map;

public class TipCalculator {

    private static Map<String, Integer> tips = new HashMap<>();

    static {
        tips.put("terrible", 0);
        tips.put("poor", 5);
        tips.put("good", 10);
        tips.put("great", 15);
        tips.put("excellent", 20);
    }

    public static Integer calculateTip(double amount, String rating) {
        Integer tipRating = tips.get(rating.toLowerCase());
        if (tipRating == null) return null;
        return (int) Math.ceil(tipRating * amount / 100);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值