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);
}
}
来源:CSDN
作者:fylive
链接:https://blog.csdn.net/qq_39472229/article/details/103802917