desislava_topuzakova

06. Vehicle Catalogue

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