desislava_topuzakova

6. Vehicle Catalogue

Nov 5th, 2021
1,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package VehicleCatalogue_06;
  2.  
  3. public class Vehicle {
  4.     //полета -> характеристики
  5.     private String type;
  6.     private String model;
  7.     private String color;
  8.     private int hp;
  9.  
  10.     //конструктор
  11.     public Vehicle(String type, String model, String color, int hp) {
  12.         this.type = type;
  13.         this.model = model;
  14.         this.color = color;
  15.         this.hp = hp;
  16.     }
  17.  
  18.     public String getModel() {
  19.         return this.model;
  20.     }
  21.  
  22.     public String getType() {
  23.         return this.type;
  24.     }
  25.  
  26.     public int getHp() {
  27.         return hp;
  28.     }
  29.  
  30.  
  31.     @Override
  32.     public String toString() {
  33.         //Type: {typeOfVehicle}
  34.         //Model: {modelOfVehicle}
  35.         //Color: {colorOfVehicle}
  36.         //Horsepower: {horsepowerOfVehicle}
  37.         String formattedType = "";
  38.         if (this.type.equals("car")) {
  39.             formattedType = "Car";
  40.         } else if (this.type.equals("truck")) {
  41.             formattedType = "Truck";
  42.         }
  43.         return String.format("Type: %s%nModel: %s%nColor: %s%nHorsepower: %d", formattedType, this.model, this.color, this.hp);
  44.     }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51. package VehicleCatalogue_06;
  52.  
  53. import java.util.ArrayList;
  54. import java.util.List;
  55. import java.util.Scanner;
  56.  
  57. public class Main {
  58.     public static void main(String[] args) {
  59.         Scanner scanner = new Scanner(System.in);
  60.         String input = scanner.nextLine();
  61.         List<Vehicle> vehiclesList = new ArrayList<>();
  62.         while (!input.equals("End")) {
  63.             //input = "truck Man red 200".split(" ") -> ["truck", "Man", "red", "200"]
  64.             String[] inputData = input.split(" ");//["truck", "Man", "red", "200"]
  65.             String type = inputData[0];
  66.             String model = inputData[1];
  67.             String color = inputData[2];
  68.             int hp = Integer.parseInt(inputData[3]);
  69.  
  70.             Vehicle vehicle = new Vehicle(type, model, color, hp);
  71.             vehiclesList.add(vehicle);
  72.  
  73.  
  74.             input = scanner.nextLine();
  75.         }
  76.  
  77.         String enteredModel = scanner.nextLine();
  78.         while (!enteredModel.equals("Close the Catalogue")) {
  79.  
  80.             for (Vehicle vehicle : vehiclesList) {
  81.                 if (enteredModel.equals(vehicle.getModel())) {
  82.                     System.out.println(vehicle);
  83.                     break;
  84.                 }
  85.             }
  86.  
  87.             enteredModel = scanner.nextLine();
  88.         }
  89.  
  90.         System.out.printf("Cars have average horsepower of: %.2f.%n", getAverageHP(vehiclesList, "cars"));
  91.         System.out.printf("Trucks have average horsepower of: %.2f.", getAverageHP(vehiclesList, "trucks"));
  92.     }
  93.  
  94.     private static double getAverageHP(List<Vehicle> vehiclesList, String type) {
  95.         double sum = 0;
  96.         int count = 0;
  97.         if (type.equals("cars")) {
  98.             for (Vehicle vehicle : vehiclesList) {
  99.                 if (vehicle.getType().equals("car")) {
  100.                     sum += vehicle.getHp();
  101.                     count++;
  102.                 }
  103.             }
  104.         } else if (type.equals("trucks")) {
  105.             for (Vehicle vehicle : vehiclesList) {
  106.                 if (vehicle.getType().equals("truck")) {
  107.                     sum += vehicle.getHp();
  108.                     count++;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         if (sum == 0) {
  114.             return 0;
  115.         }
  116.         return sum / count;
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment