Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.scalefocus;
- import javax.sound.midi.SysexMessage;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.OptionalDouble;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String vehicleInput = sc.nextLine();
- HashMap<String, Vehicle> vehicles = new HashMap<>();
- while(!vehicleInput.equals("End")) {
- Vehicle currentVehicle = new Vehicle(vehicleInput);
- if(!vehicles.containsKey(currentVehicle.getModel())) {
- vehicles.put(currentVehicle.getModel(),
- currentVehicle);
- }
- vehicleInput = sc.nextLine();
- }
- String modelInput = sc.nextLine();
- while(!modelInput.equals("Close the Catalogue")) {
- if(vehicles.containsKey(modelInput)) {
- System.out.println(vehicles.get(modelInput));
- }
- modelInput = sc.nextLine();
- }
- OptionalDouble averageCar = calculateAverage("car",
- vehicles);
- System.out.printf("Cars have average horsepower of: %.2f.%n",
- (averageCar.isPresent()) ?
- averageCar.getAsDouble()
- : 0);
- OptionalDouble averageTruck = calculateAverage("truck",
- vehicles);
- System.out.printf("Trucks have average horsepower of: %.2f.%n",
- (averageTruck.isPresent()) ?
- averageTruck.getAsDouble()
- : 0
- );
- }
- private static OptionalDouble calculateAverage(String vehicleType,
- Map<String, Vehicle> vehicles) {
- return vehicles.entrySet()
- .stream()
- .filter(item -> item.getValue().getType().equals(vehicleType))
- .mapToDouble(item -> item.getValue().getHorsepower())
- .average();
- }
- public static class Vehicle {
- private String type;
- private String model;
- private String color;
- private int horsepower;
- public Vehicle(String vehicleInput) {
- String[] vehicleData = vehicleInput.split(" ");
- this.type = vehicleData[0];
- this.model = vehicleData[1];
- this.color = vehicleData[2];
- this.horsepower = Integer.parseInt(vehicleData[3]);
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getModel() {
- return model;
- }
- public void setModel(String model) {
- this.model = model;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public int getHorsepower() {
- return horsepower;
- }
- public void setHorsepower(int horsepower) {
- this.horsepower = horsepower;
- }
- public Vehicle(String type, String model, String color, int horsepower) {
- this.type = type;
- this.model = model;
- this.color = color;
- this.horsepower = horsepower;
- }
- @Override
- public String toString() {
- return "Type: " + capitalize(type) + System.lineSeparator() +
- "Model: " + model + System.lineSeparator() +
- "Color: " + color + System.lineSeparator() +
- "Horsepower: " + horsepower;
- }
- private String capitalize(String type) {
- return type.substring(0, 1).toUpperCase() + type.substring(1).toLowerCase();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement