Advertisement
elvirynwa

VehicleCatalogue

Mar 26th, 2019
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package VehicleCatalogue;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.  
  13.         String input = scanner.nextLine();
  14.  
  15.         List<Vehicle> vehicles = new ArrayList<>();
  16.         while (!"End".equals(input)) {
  17.  
  18.             String[] command = input.split("\\s+");
  19.  
  20.             String type = command[0];
  21.             String model = command[1];
  22.             String color = command[2];
  23.             int horsePower = Integer.parseInt(command[3]);
  24.  
  25.             Vehicle vehicle = new Vehicle();
  26.  
  27.             vehicle.setType(type);
  28.             vehicle.setModel(model);
  29.             vehicle.setColor(color);
  30.             vehicle.setHorsePower(horsePower);
  31.  
  32.             vehicles.add(vehicle);
  33.  
  34.             input = scanner.nextLine();
  35.  
  36.         }
  37.  
  38.         String model = "";
  39.         while (!"Close the Catalogue".equals(model = scanner.nextLine())) {
  40.             String finalModel = model;
  41.             vehicles
  42.                     .stream()
  43.                     .filter(v -> v.getModel().equals(finalModel))
  44.                     .forEach(System.out::println);
  45.  
  46.         }
  47.         System.out.println(String.format("Cars have average horsepower of: %.2f.", average(vehicles.stream()
  48.                 .filter(v -> v.getType().equals("car"))
  49.                 .collect(Collectors.toList()))));
  50.  
  51.         System.out.println(String.format("Trucks have average horsepower of: %.2f.", average(vehicles.stream()
  52.                 .filter(v -> v.getType().equals("truck"))
  53.                 .collect(Collectors.toList()))));
  54.  
  55.     }
  56.  
  57.     private static double average(List<Vehicle> vehicles) {
  58.         if (vehicles.size() == 0) {
  59.             return 0.0;
  60.         }
  61.         double sum = 0;
  62.  
  63.         for (Vehicle vehicle : vehicles) {
  64.             sum += vehicle.getHorsePower();
  65.         }
  66.         return sum / vehicles.size();
  67.     }
  68. }
  69.  
  70. ----------------------------------------------------------------------------------------------------------------------------------
  71.  
  72. package VehicleCatalogue;
  73.  
  74. public class Vehicle {
  75.     private String type;
  76.     private String model;
  77.     private String color;
  78.     private int horsePower;
  79.  
  80.     public String getType() {
  81.         return type;
  82.     }
  83.  
  84.     public void setType(String type) {
  85.         this.type = type;
  86.     }
  87.  
  88.     public String getModel() {
  89.         return model;
  90.     }
  91.  
  92.     public void setModel(String model) {
  93.         this.model = model;
  94.     }
  95.  
  96.     public String getColor() {
  97.         return color;
  98.     }
  99.  
  100.     public void setColor(String color) {
  101.         this.color = color;
  102.     }
  103.  
  104.     public int getHorsePower() {
  105.         return horsePower;
  106.     }
  107.  
  108.     public void setHorsePower(int horsePower) {
  109.         this.horsePower = horsePower;
  110.     }
  111.  
  112.     @Override
  113.     public String toString() {
  114.         return String.format("Type: %s\n" +
  115.                 "Model: %s\n" +
  116.                 "Color: %s\n" +
  117.                 "Horsepower: %d", getType().toUpperCase().charAt(0) + this.getType().substring(1), getModel(), getColor(), getHorsePower());
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement