Untitled
user_6122548
plain_text
2 years ago
3.2 kB
3
Indexable
import java.util.ArrayList; import java.util.Scanner; public class Ja_Exercise_05 { public static class Vehicle { private String typeOfVehicle; private String model; private String color; private int horsepower; public Vehicle(String red) { String[] redZaVozilo = red.split(" "); this.typeOfVehicle = redZaVozilo[0].strip(); this.model = redZaVozilo[1].strip(); this.color = redZaVozilo[2].strip().strip(); this.horsepower = Integer.parseInt(redZaVozilo[3].strip()); if ( this.typeOfVehicle.equals("car")) this.typeOfVehicle = "Car"; if ( this.typeOfVehicle.equals("truck")) this.typeOfVehicle = "Truck"; } public int getHorsepower () { return this.horsepower; } @Override public String toString() { String rez = String.format("Type: %s\n", this.typeOfVehicle); rez = rez + String.format("Model: %s\n", this.model); rez = rez + String.format("Color: %s\n", this.color); rez = rez + String.format("Horsepower: %d", this.horsepower); return rez; } public static double prosek (ArrayList<Vehicle> vozila, String typeOfVehicle) { int brojac = 0; int zbir = 0; for (Vehicle v : vozila) { if (v.typeOfVehicle.equals(typeOfVehicle)) { brojac++; zbir = zbir + v.horsepower; } } double prosek; if (zbir==0) prosek = 0; else prosek = zbir/brojac; return prosek; } public static void ispisiPoModelu(ArrayList<Vehicle> vozila, String model) { for (Vehicle v : vozila) { /// prolazimo kroz sva vozila if (v.model.equals(model)) { // pitamo da li je model trenutnog vozila jednak modelu System.out.println(v); /// koji se trazi, ako jeste ispisemo podatke break; /// prekidamo unos } } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Vehicle> vozila = new ArrayList<>(); //Until you receive the command "End" you will receive lines of input in the format: //{typeOfVehicle} {model} {color} {horsepower} int brojac = 0; int zbir = 0; while (true) { String unos = sc.nextLine(); if (unos.equals("End")) break; Vehicle trenutno = new Vehicle(unos); vozila.add(trenutno); } while (true) { String unos2 = sc.nextLine(); if (unos2.equals("Close the Catalogue")) break; Vehicle.ispisiPoModelu(vozila, unos2); } double prosekcar = Vehicle.prosek(vozila, "Car"); double prosekTruck = Vehicle.prosek(vozila, "Truck"); System.out.printf("Cars have average horsepower of: %.2f.\n", prosekcar); System.out.printf("Trucks have average horsepower of: %.2f.\n", prosekTruck); } }
Editor is loading...