Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. package com.scalefocus;
  2.  
  3. import javax.sound.midi.SysexMessage;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.OptionalDouble;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. String vehicleInput = sc.nextLine();
  15.  
  16. HashMap<String, Vehicle> vehicles = new HashMap<>();
  17.  
  18. while(!vehicleInput.equals("End")) {
  19. Vehicle currentVehicle = new Vehicle(vehicleInput);
  20. if(!vehicles.containsKey(currentVehicle.getModel())) {
  21. vehicles.put(currentVehicle.getModel(),
  22. currentVehicle);
  23. }
  24. vehicleInput = sc.nextLine();
  25. }
  26.  
  27. String modelInput = sc.nextLine();
  28. while(!modelInput.equals("Close the Catalogue")) {
  29.  
  30. if(vehicles.containsKey(modelInput)) {
  31. System.out.println(vehicles.get(modelInput));
  32. }
  33. modelInput = sc.nextLine();
  34. }
  35.  
  36.  
  37.  
  38. OptionalDouble averageCar = calculateAverage("car",
  39. vehicles);
  40. System.out.printf("Cars have average horsepower of: %.2f.%n",
  41. (averageCar.isPresent()) ?
  42. averageCar.getAsDouble()
  43. : 0);
  44.  
  45. OptionalDouble averageTruck = calculateAverage("truck",
  46. vehicles);
  47.  
  48. System.out.printf("Trucks have average horsepower of: %.2f.%n",
  49. (averageTruck.isPresent()) ?
  50. averageTruck.getAsDouble()
  51. : 0
  52. );
  53.  
  54. }
  55.  
  56. private static OptionalDouble calculateAverage(String vehicleType,
  57. Map<String, Vehicle> vehicles) {
  58. return vehicles.entrySet()
  59. .stream()
  60. .filter(item -> item.getValue().getType().equals(vehicleType))
  61. .mapToDouble(item -> item.getValue().getHorsepower())
  62. .average();
  63. }
  64.  
  65. public static class Vehicle {
  66. private String type;
  67. private String model;
  68. private String color;
  69. private int horsepower;
  70.  
  71. public Vehicle(String vehicleInput) {
  72. String[] vehicleData = vehicleInput.split(" ");
  73. this.type = vehicleData[0];
  74. this.model = vehicleData[1];
  75. this.color = vehicleData[2];
  76. this.horsepower = Integer.parseInt(vehicleData[3]);
  77. }
  78.  
  79. public String getType() {
  80. return type;
  81. }
  82.  
  83. public void setType(String type) {
  84. this.type = type;
  85. }
  86.  
  87. public String getModel() {
  88. return model;
  89. }
  90.  
  91. public void setModel(String model) {
  92. this.model = model;
  93. }
  94.  
  95. public String getColor() {
  96. return color;
  97. }
  98.  
  99. public void setColor(String color) {
  100. this.color = color;
  101. }
  102.  
  103. public int getHorsepower() {
  104. return horsepower;
  105. }
  106.  
  107. public void setHorsepower(int horsepower) {
  108. this.horsepower = horsepower;
  109. }
  110.  
  111. public Vehicle(String type, String model, String color, int horsepower) {
  112. this.type = type;
  113. this.model = model;
  114. this.color = color;
  115. this.horsepower = horsepower;
  116. }
  117.  
  118. @Override
  119. public String toString() {
  120. return "Type: " + capitalize(type) + System.lineSeparator() +
  121. "Model: " + model + System.lineSeparator() +
  122. "Color: " + color + System.lineSeparator() +
  123. "Horsepower: " + horsepower;
  124. }
  125.  
  126. private String capitalize(String type) {
  127. return type.substring(0, 1).toUpperCase() + type.substring(1).toLowerCase();
  128. }
  129.  
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement