Advertisement
ShSh99

VehicleCatalogue

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