Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package L06_VehicleCatalogue;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input;
- List<Vehicle> trucks = new ArrayList<>();
- List<Vehicle> cars = new ArrayList<>();
- List<Vehicle> vehicles = new ArrayList<>();
- while (!"End".equals(input = scanner.nextLine())) {
- String[] tokens = input.split(" ");
- String type = tokens[0];
- String model = tokens[1];
- String color = tokens[2];
- int horsePower = Integer.parseInt(tokens[3]);
- Vehicle vehicle = new Vehicle(type, model, color, horsePower);
- if (type.equals("car")){
- cars.add(vehicle);
- }else {
- trucks.add(vehicle);
- }
- vehicles.add(vehicle);
- }
- while (!"Close the Catalogue".equals(input = scanner.nextLine())){
- String model = input;
- for (Vehicle vehicle : vehicles) {
- if (vehicle.model.equals(model)){
- System.out.print(vehicle);
- break;
- }
- }
- }
- int avgCars = 0;
- for (Vehicle car : cars) {
- avgCars += car.horsePower;
- }
- System.out.printf("Cars have average horsepower of: %.2f.", avgCars * 1.0 / cars.size());
- int avgTrucks = 0;
- for (Vehicle truck : trucks) {
- avgTrucks += truck.horsePower;
- }
- System.out.println();
- System.out.printf("Trucks have average horsepower of: %.2f.", avgTrucks * 1.0 / trucks.size());
- }
- }
- -----------------------------------------------------------------------
- package L06_VehicleCatalogue;
- public class Vehicle {
- String type;
- String model;
- String color;
- int horsePower;
- public Vehicle(String type, String model, String color, int horsePower) {
- this.type = type;
- this.model = model;
- this.color = color;
- this.horsePower = horsePower;
- }
- @Override
- public String toString() {
- return String.format("Type: %s%nModel: %s%nColor: %s%nHorsepower: %d%n", (this.type.substring(0,1).toUpperCase()+this.type.substring(1)), model, color, horsePower);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment