Advertisement
Guest User

Untitled

a guest
Dec 13th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package demo;
  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 VehicleCatalogue {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String line = scanner.nextLine();
  14.         List<Vehicle> allVehicles = new ArrayList<>();
  15.         while (!line.equals("End")) {
  16.             String[] dataParts = line.split(" ");
  17.             Vehicle vehicle = new Vehicle(dataParts[0], dataParts[1], dataParts[2], Integer.parseInt(dataParts[3]));
  18.             allVehicles.add(vehicle);
  19.  
  20.             line = scanner.nextLine();
  21.         }
  22.         line = scanner.nextLine();
  23.  
  24.         while (!line.equals("Close the Catalogue")) {
  25.             String model = line;
  26.  
  27.             allVehicles.stream().filter(vehicle -> vehicle.getModel().equals(model))
  28.                     .forEach(vehicle -> System.out.println(vehicle.toString()));
  29.  
  30.  
  31.             line = scanner.nextLine();
  32.         }
  33.         List<Vehicle> cars = allVehicles.stream().filter(vehicle -> vehicle.getType().equals("car"))
  34.                 .collect(Collectors.toList());
  35.         List<Vehicle> trucks = allVehicles.stream().filter(vehicle -> vehicle.getType().equals("truck"))
  36.                 .collect(Collectors.toList());
  37.  
  38.         System.out.printf("Cars have average horsepower of: %.2f.%n", averageHorsepower(cars));
  39.         System.out.printf("Trucks have average horsepower of: %.2f.", averageHorsepower(trucks));
  40.  
  41.     }
  42.  
  43.     public static Double averageHorsepower(List<Vehicle> vehicles) {
  44.         if (vehicles.size() == 0) {
  45.             return 0.0;
  46.         } else {
  47.             return vehicles.stream().mapToDouble(Vehicle::getHorsePower).sum() / vehicles.size();
  48.         }
  49.     }
  50.  
  51.     static class Vehicle {
  52.         String type;
  53.         String model;
  54.         String color;
  55.         int horsePower;
  56.  
  57.         public Vehicle(String type, String model, String color, int horsePower) {
  58.             this.type = type;
  59.             this.model = model;
  60.             this.color = color;
  61.             this.horsePower = horsePower;
  62.         }
  63.  
  64.         public String toString() {
  65.             return String.format("Type: %s%n"
  66.                             + "Model: %s%n" + "Color: %s%n" + "Horsepower: %d"
  67.                     , getType().toUpperCase().charAt(0) + getType().substring(1), getModel(), getColor(), getHorsePower());
  68.         }
  69.  
  70.         public String getType() {
  71.             return type;
  72.         }
  73.  
  74.         public String getModel() {
  75.             return model;
  76.         }
  77.  
  78.         public String getColor() {
  79.             return color;
  80.         }
  81.  
  82.         public int getHorsePower() {
  83.             return horsePower;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement