Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TechModuleObjectVehicleCatalogue;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Vehicle> car = new ArrayList<>();
- List<Vehicle> truck = new ArrayList<>();
- double carsHorsePower = 0;
- double truckHorsePower = 0;
- double countCar = 0.0;
- double countTruck = 0.0;
- List<String> inputInformation = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
- while (!inputInformation.get(0).equals("End")) {
- Vehicle vehicle = new Vehicle(inputInformation.get(0), inputInformation.get(1), inputInformation.get(2), Double.parseDouble(inputInformation.get(3)));
- if (inputInformation.get(0).equalsIgnoreCase("car")) {
- car.add(vehicle);
- countCar++;
- carsHorsePower += Double.parseDouble(inputInformation.get(3));
- } else {
- truck.add(vehicle);
- countTruck++;
- truckHorsePower += Double.parseDouble(inputInformation.get(3));
- }
- inputInformation = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
- }
- String modelInformation = scanner.nextLine();
- while (!modelInformation.equals("Close the Catalogue")) {
- for (Vehicle model : car) {
- if (model.getModel().equals(modelInformation)) {
- System.out.println(model);
- modelInformation = scanner.nextLine();
- } else {
- for (Vehicle model1 : truck) {
- if (model1.getModel().equals(modelInformation)) {
- System.out.println(model1);
- modelInformation = scanner.nextLine();
- }
- }
- }
- }
- }
- double averageCars = carsHorsePower / countCar;
- if (countCar == 0) {
- System.out.print(String.format("Trucks have average horsepower of: %.2f.", truckHorsePower / countTruck));
- } else if (countTruck == 0) {
- System.out.println(String.format("Cars have average horsepower of: %.2f.", averageCars));
- } else {
- System.out.println(String.format("Cars have average horsepower of: %.2f.", averageCars));
- System.out.print(String.format("Trucks have average horsepower of: %.2f.", truckHorsePower / countTruck));
- }
- }
- }
- ------------------------------------------------------------------------------------------------------------------
- package TechModuleObjectVehicleCatalogue;
- public class Vehicle {
- private String type;
- private String model;
- private String color;
- private Double horsepower;
- public Vehicle(String type,String model,String color,Double horsepower){
- this.type=type;
- this.model=model;
- this.color=color;
- this.horsepower=horsepower;
- }
- public String getType(){
- return this.type;
- }
- public String getModel(){
- return this.model;
- }
- public String getColor(){
- return this.color;
- }
- public Double getHorsepower(){
- return this.horsepower;
- }
- @Override
- public String toString() {
- return String.format("Type: %s%nModel: %s%nColor: %s%nHorsepower: %.0f%n",(this.type.substring(0,1).toUpperCase()+this.type.substring(1)),
- this.model,this.color,this.horsepower);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement