Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Comparator;
- public class CarTest {
- public static void main(String[] args) {
- CarCollection carCollection = new CarCollection();
- String manufacturer = fillCollection(carCollection);
- carCollection.sortByPrice(true);
- System.out.println("=== Sorted By Price ASC ===");
- print(carCollection.getList());
- carCollection.sortByPrice(false);
- System.out.println("=== Sorted By Price DESC ===");
- print(carCollection.getList());
- System.out.printf("=== Filtered By Manufacturer: %s ===\n", manufacturer);
- List<Car> result = carCollection.filterByManufacturer(manufacturer);
- print(result);
- }
- static void print(List<Car> cars) {
- for (Car c : cars) {
- System.out.println(c);
- }
- }
- static String fillCollection(CarCollection cc) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNext()) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- if(parts.length < 4) return parts[0];
- Car car = new Car(parts[0], parts[1], Integer.parseInt(parts[2]),
- Float.parseFloat(parts[3]));
- cc.addCar(car);
- }
- scanner.close();
- return "";
- }
- }
- // vashiot kod ovde
- class Car implements Comparable<Car> {
- private String manufacturer;
- private String model;
- private int price;
- private float power;
- public Car(String manufacturer, String model, int price, float power) {
- this.manufacturer = manufacturer;
- this.model = model;
- this.price = price;
- this.power = power;
- }
- public String getManufacturer() {
- return manufacturer;
- }
- public String getModel() {
- return model;
- }
- public int getPrice() {
- return price;
- }
- public float getPower() {
- return power;
- }
- @Override
- public String toString() {
- return String.format("%s %s (%.0fKW) %d", manufacturer, model, power, price);
- }
- @Override
- public int compareTo(Car c) {
- if (this.getPrice() == c.getPrice()) {
- return Float.compare(this.getPower(), c.getPower());
- }
- return Integer.compare(this.getPrice(), c.getPrice());
- }
- }
- class CarCollection {
- private ArrayList<Car> cars;
- public CarCollection() {
- cars = new ArrayList<Car>();
- }
- public void addCar(Car car) {
- cars.add(car);
- }
- public void sortByPrice(boolean ascending) {
- if (!ascending) {
- Collections.sort(cars, Collections.reverseOrder());
- } else {
- Collections.sort(cars);
- }
- }
- public ArrayList<Car> filterByManufacturer(String manufacturer) {
- ArrayList<Car> byManufacturer = new ArrayList<Car>();
- for (Car car : cars) {
- if (car.getManufacturer().equalsIgnoreCase(manufacturer)) {
- byManufacturer.add(car);
- }
- }
- Collections.sort(byManufacturer, new CarModelComparator());
- return byManufacturer;
- }
- public ArrayList<Car> getList() {
- return cars;
- }
- }
- class CarModelComparator implements Comparator<Car> {
- @Override
- public int compare(Car o1, Car o2) {
- return o1.getModel().compareTo(o2.getModel());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment