Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 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.getAsDouble());
  42.  
  43. OptionalDouble averageTruck = calculateAverage("truck",
  44. vehicles);
  45. System.out.printf("Trucks have average horsepower of: %.2f.%n",
  46. averageTruck.getAsDouble());
  47.  
  48. }
  49.  
  50. private static OptionalDouble calculateAverage(String vehicleType,
  51. Map<String, Vehicle> vehicles) {
  52. return vehicles.entrySet()
  53. .stream()
  54. .filter(item -> item.getValue().getType().equals(vehicleType))
  55. .mapToDouble(item -> item.getValue().getHorsepower())
  56. .average();
  57. }
  58.  
  59.  
  60. public static class Vehicle {
  61. private String type;
  62. private String model;
  63. private String color;
  64. private int horsepower;
  65.  
  66. public Vehicle(String vehicleInput) {
  67. String[] vehicleData = vehicleInput.split(" ");
  68. this.type = vehicleData[0];
  69. this.model = vehicleData[1];
  70. this.color = vehicleData[2];
  71. this.horsepower = Integer.parseInt(vehicleData[3]);
  72. }
  73.  
  74. public String getType() {
  75. return type;
  76. }
  77.  
  78. public void setType(String type) {
  79. this.type = type;
  80. }
  81.  
  82. public String getModel() {
  83. return model;
  84. }
  85.  
  86. public void setModel(String model) {
  87. this.model = model;
  88. }
  89.  
  90. public String getColor() {
  91. return color;
  92. }
  93.  
  94. public void setColor(String color) {
  95. this.color = color;
  96. }
  97.  
  98. public int getHorsepower() {
  99. return horsepower;
  100. }
  101.  
  102. public void setHorsepower(int horsepower) {
  103. this.horsepower = horsepower;
  104. }
  105.  
  106. public Vehicle(String type, String model, String color, int horsepower) {
  107. this.type = type;
  108. this.model = model;
  109. this.color = color;
  110. this.horsepower = horsepower;
  111. }
  112.  
  113. @Override
  114. public String toString() {
  115. return "Type: " + capitalize(type) + System.lineSeparator() +
  116. "Model: " + model + System.lineSeparator() +
  117. "Color: " + color + System.lineSeparator() +
  118. "Horsepower: " + horsepower;
  119. }
  120.  
  121. private String capitalize(String type) {
  122. return type.substring(0, 1).toUpperCase() + type.substring(1).toLowerCase();
  123. }
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement