Didart

Vehicle Catalogue

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