Advertisement
jwrbg

asd

Mar 26th, 2019
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package TechModuleObjectVehicleCatalogue;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         List<Vehicle> car = new ArrayList<>();
  13.         List<Vehicle> truck = new ArrayList<>();
  14.  
  15.         double carsHorsePower = 0;
  16.         double truckHorsePower = 0;
  17.         double countCar = 0.0;
  18.         double countTruck = 0.0;
  19.  
  20.         List<String> inputInformation = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  21.  
  22.         while (!inputInformation.get(0).equals("End")) {
  23.             Vehicle vehicle = new Vehicle(inputInformation.get(0), inputInformation.get(1), inputInformation.get(2), Double.parseDouble(inputInformation.get(3)));
  24.             if (inputInformation.get(0).equalsIgnoreCase("car")) {
  25.                 car.add(vehicle);
  26.                 countCar++;
  27.                 carsHorsePower += Double.parseDouble(inputInformation.get(3));
  28.             } else {
  29.                 truck.add(vehicle);
  30.                 countTruck++;
  31.                 truckHorsePower += Double.parseDouble(inputInformation.get(3));
  32.             }
  33.             inputInformation = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  34.         }
  35.  
  36.  
  37.         String modelInformation = scanner.nextLine();
  38.  
  39.  
  40.         while (!modelInformation.equals("Close the Catalogue")) {
  41.             for (Vehicle model : car) {
  42.                 if (model.getModel().equals(modelInformation)) {
  43.                     System.out.println(model);
  44.                     modelInformation = scanner.nextLine();
  45.  
  46.                 } else {
  47.                     for (Vehicle model1 : truck) {
  48.                         if (model1.getModel().equals(modelInformation)) {
  49.                             System.out.println(model1);
  50.                             modelInformation = scanner.nextLine();
  51.  
  52.                         }
  53.  
  54.                     }
  55.                 }
  56.  
  57.             }
  58.         }
  59.         double averageCars = carsHorsePower / countCar;
  60.  
  61.         if (countCar == 0) {
  62.             System.out.print(String.format("Trucks have average horsepower of: %.2f.", truckHorsePower / countTruck));
  63.         } else if (countTruck == 0) {
  64.             System.out.println(String.format("Cars have average horsepower of: %.2f.", averageCars));
  65.         } else {
  66.  
  67.             System.out.println(String.format("Cars have average horsepower of: %.2f.", averageCars));
  68.             System.out.print(String.format("Trucks have average horsepower of: %.2f.", truckHorsePower / countTruck));
  69.  
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75.  
  76. ------------------------------------------------------------------------------------------------------------------
  77.  
  78.  
  79. package TechModuleObjectVehicleCatalogue;
  80.  
  81. public class Vehicle {
  82.     private String type;
  83.     private String model;
  84.     private String color;
  85.     private Double horsepower;
  86.  
  87. public Vehicle(String type,String model,String color,Double horsepower){
  88.     this.type=type;
  89.     this.model=model;
  90.     this.color=color;
  91.     this.horsepower=horsepower;
  92. }
  93. public String getType(){
  94.     return this.type;
  95. }
  96. public String getModel(){
  97.     return  this.model;
  98. }
  99. public String getColor(){
  100.     return this.color;
  101. }
  102. public Double getHorsepower(){
  103.     return this.horsepower;
  104. }
  105.  
  106.     @Override
  107.     public String toString() {
  108.         return String.format("Type: %s%nModel: %s%nColor: %s%nHorsepower: %.0f%n",(this.type.substring(0,1).toUpperCase()+this.type.substring(1)),
  109.                 this.model,this.color,this.horsepower);
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement