Guest User

Untitled

a guest
Jan 20th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import java.util.Comparator;
  2. import java.util.concurrent.CompletableFuture;
  3. import java.util.stream.Stream;
  4.  
  5. public class RideTest {
  6.  
  7. interface RideProvider {
  8. long getFareEstimate(String start_lat, String start_lng, String end_lat, String end_lng, String type);
  9. }
  10.  
  11. public static void main(String[] args) {
  12.  
  13. var start_lat = "37.7749295";//San Francisco
  14. var start_lng = "-122.41941550000001";//San Francisco
  15.  
  16. var end_lat = "37.3382082";//San Jose
  17. var end_lng = "-121.88632860000001";//San Jose
  18.  
  19. var lyft = new RideProvider() {
  20. @Override
  21. public long getFareEstimate(String start_lat, String start_lng, String end_lat, String end_lng, String type) {
  22. var estimated_cost_cents_max = 0;
  23. switch (type) {
  24. case "Line":
  25. estimated_cost_cents_max = 3307;
  26. break;
  27.  
  28. case "Lyft":
  29. estimated_cost_cents_max = 7306;
  30. break;
  31.  
  32. case "Plus":
  33. estimated_cost_cents_max = 12451;
  34. break;
  35.  
  36.  
  37. case "Premier":
  38. estimated_cost_cents_max = 17562;
  39. break;
  40.  
  41. case "Lux":
  42. estimated_cost_cents_max = 23624;
  43. break;
  44.  
  45. case "Lux SUV":
  46. estimated_cost_cents_max = 25879;
  47. break;
  48.  
  49. }
  50.  
  51. return estimated_cost_cents_max;
  52. }
  53. };
  54.  
  55. var lyftFuture = CompletableFuture.supplyAsync(() -> lyft.getFareEstimate(start_lat, start_lng, end_lat, end_lng, "Lyft"));
  56.  
  57. var uber = new RideProvider() {
  58. @Override
  59. public long getFareEstimate(String start_lat, String start_lng, String end_lat, String end_lng, String type) {
  60. var estimated_cost_cents_max = 0;
  61. switch (type) {
  62. case "POOL":
  63. estimated_cost_cents_max = 7000;
  64. break;
  65.  
  66. case "EXPRESS POOL":
  67. estimated_cost_cents_max = 7000;
  68. break;
  69.  
  70. case "uberX":
  71. estimated_cost_cents_max = 9200;
  72. break;
  73.  
  74. case "WAV":
  75. estimated_cost_cents_max = 9200;
  76. break;
  77.  
  78. case "ASSIST":
  79. estimated_cost_cents_max = 9200;
  80. break;
  81.  
  82. case "uberXL":
  83. estimated_cost_cents_max = 14800;
  84. break;
  85.  
  86. case "SELECT":
  87. estimated_cost_cents_max = 20900;
  88. break;
  89.  
  90. case "SUV":
  91. estimated_cost_cents_max = 30600;
  92. break;
  93. }
  94.  
  95. return estimated_cost_cents_max;
  96. }
  97. };
  98.  
  99. var uberFuture = CompletableFuture.supplyAsync(() -> uber.getFareEstimate(start_lat, start_lng, end_lat, end_lng, "uberX"));
  100.  
  101. // compare the futures for best fare estimate
  102. Stream.of(lyftFuture, uberFuture)
  103. .map(CompletableFuture::join)
  104. .min(Comparator.comparing(i -> i))
  105. .ifPresent(estimated_cost_cents_max -> System.out.println("$" + estimated_cost_cents_max / 100));
  106. }
  107. }
Add Comment
Please, Sign In to add comment