Advertisement
Kancho

Vehicle_Catalogue

May 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. package OpinionPoll.Vehicle_Catalogue;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9.  
  10. public class Main {
  11. public static void main(String[] args) throws IOException {
  12.  
  13. BufferedReader reader =
  14. new BufferedReader(
  15. new InputStreamReader(
  16. System.in));
  17.  
  18. List<Vehicle> vehiclesList = new ArrayList<>();
  19.  
  20. System.out.println("Enter vehicle");
  21. String input = reader.readLine();
  22.  
  23. while (!"End".equalsIgnoreCase(input)) {
  24. String[] data = input.split("\\s+");
  25. String type = data[0];
  26. String model = data[1];
  27. String colour = data[2];
  28. Integer price = Integer.parseInt(data[3]);
  29.  
  30. Vehicle vehicle = new Vehicle(type, model, colour, price);
  31.  
  32. vehiclesList.add(vehicle);
  33. System.out.println("Enter vehicle");
  34. input = reader.readLine();
  35. }
  36.  
  37. System.out.println("Enter model");
  38. String model = reader.readLine();
  39.  
  40. while (!"Close catalogue".equalsIgnoreCase(model)) {
  41.  
  42. String finalModel = model;
  43. vehiclesList.stream()
  44. .filter(v -> v.getModel().equalsIgnoreCase(finalModel))
  45. .forEach(System.out::println);
  46.  
  47. System.out.println("Enter model");
  48. model = reader.readLine();
  49. }
  50. System.out.println(
  51. String.format("The average price of the cars is %.2f",
  52. averagePrice(vehiclesList.stream()
  53. .filter(v -> v.getType()
  54. .equalsIgnoreCase("car"))
  55. .collect(Collectors.toList()))));
  56.  
  57. System.out.println(
  58. String.format("The average price of our trucks is %.2f.",
  59. averagePrice(vehiclesList.stream()
  60. .filter(v -> v.getType()
  61. .equalsIgnoreCase("truck"))
  62. .collect(Collectors.toList()))));
  63. }
  64.  
  65. public static double averagePrice(List<Vehicle> vehicles) {
  66. double sum = 0;
  67. if (vehicles.size() == 0) {
  68. return 0;
  69. }
  70. for (Vehicle vehicle : vehicles) {
  71. sum += vehicle.getPrice();
  72. }
  73. return sum / vehicles.size();
  74.  
  75. }
  76. }
  77. package OpinionPoll.Vehicle_Catalogue;
  78.  
  79. public class Vehicle {
  80.  
  81. private String type;
  82. private String model;
  83. private String colour;
  84. private Integer price;
  85.  
  86. public Vehicle(String type, String model, String colour, Integer price) {
  87. this.type = type;
  88. this.model = model;
  89. this.colour = colour;
  90. this.price = price;
  91. }
  92.  
  93. public String getType() {
  94. return type;
  95. }
  96.  
  97. public String getModel() {
  98. return model;
  99. }
  100.  
  101. public String getColour() {
  102. return colour;
  103. }
  104.  
  105. public Integer getPrice() {
  106. return price;
  107. }
  108.  
  109. @Override
  110. public String toString() {
  111. return String.format("Type %s: \nModel: %s\ncolour: %s\nPrice: %d",
  112. this.getType(),
  113. this.getModel().toUpperCase().charAt(0)
  114. + getModel().substring(1) ,
  115. this.getColour(),
  116. this.getPrice());
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement