Advertisement
vkarakolev

objectsEx6

Feb 25th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class E06_VehicleCatalogue {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         List<Vehicle> vehicles = new ArrayList<>();
  10.         int carsHpSum = 0;
  11.         int trucksHpSum = 0;
  12.         int carsCount = 0;
  13.         int trucksCount = 0;
  14.  
  15.         String command = scanner.nextLine();
  16.         while(!command.equals("End")){
  17.             String[] vehicleData = command.split(" ");
  18.             Vehicle vehicle = new Vehicle(vehicleData);
  19.  
  20.             if(vehicle.type.equals("car")){
  21.                 carsHpSum += vehicle.getHorsepower();
  22.                 carsCount++;
  23.             } else {
  24.                 trucksHpSum += vehicle.getHorsepower();
  25.                 trucksCount++;
  26.             }
  27.  
  28.             vehicles.add(vehicle);
  29.  
  30.             command = scanner.nextLine();
  31.         }
  32.  
  33.         command = scanner.nextLine();
  34.         while(!command.equals("Close the Catalogue")){
  35.  
  36.             for (Vehicle vehicle : vehicles) {
  37.                 if(vehicle.getModel().equals(command)){
  38.                     vehicle.printVehicleData(vehicle);
  39.                 }
  40.             }
  41.  
  42.             command = scanner.nextLine();
  43.         }
  44.  
  45.         double carsAvgHp = 0;
  46.         if(carsCount != 0){
  47.             carsAvgHp = 1.0 * carsHpSum / carsCount;
  48.         } else {
  49.             carsAvgHp = 0;
  50.         }
  51.  
  52.         double trucksAvgHp = 0;
  53.         if(trucksCount != 0){
  54.             trucksAvgHp = 1.0 * trucksHpSum / trucksCount;
  55.         } else {
  56.             trucksAvgHp = 0;
  57.         }
  58.  
  59.         String output = String.format("Cars have average horsepower of: %.2f.%n" +
  60.                 "Trucks have average horsepower of: %.2f.", carsAvgHp, trucksAvgHp);
  61.  
  62.         System.out.println(output);
  63.     }
  64.  
  65.     static class Vehicle {
  66.         String type;
  67.         String model;
  68.         String color;
  69.         int horsepower;
  70.  
  71.         public Vehicle (String[] input){
  72.             this.type = input[0];
  73.             this.model = input[1];
  74.             this.color = input[2];
  75.             this.horsepower = Integer.parseInt(input[3]);
  76.         }
  77.  
  78.         public int getHorsepower(){
  79.             return horsepower;
  80.         }
  81.  
  82.         public String getModel() {
  83.             return model;
  84.         }
  85.  
  86.         static void printVehicleData(Vehicle vehicle){
  87.             String output = String.format("Type: %s%n" +
  88.                     "Model: %s%n" +
  89.                     "Color: %s%n" +
  90.                     "Horsepower: %d%n", capitalize(vehicle.type), capitalize(vehicle.model),
  91.                     vehicle.color, vehicle.horsepower);
  92.             System.out.print(output);
  93.         }
  94.     }
  95.  
  96.     static String capitalize (String str){
  97.         if(str == null || str.isEmpty()){
  98.             return str;
  99.         }
  100.  
  101.         return str.substring(0, 1).toUpperCase() + str.substring(1);
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement