Advertisement
dzocesrce

[NP] Car Collection

Apr 22nd, 2025
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9. class Car {
  10.     String manufacturer;
  11.     String model;
  12.     int price;
  13.     float power;
  14.  
  15.     public Car(String manufacturer, String model, int price, float power) {
  16.         this.manufacturer = manufacturer;
  17.         this.model = model;
  18.         this.price = price;
  19.         this.power = power;
  20.     }
  21.  
  22.     public String getManufacturer() {
  23.         return manufacturer;
  24.     }
  25.  
  26.     public String getModel() {
  27.         return model;
  28.     }
  29.  
  30.     public int getPrice() {
  31.         return price;
  32.     }
  33.  
  34.     public float getPower() {
  35.         return power;
  36.     }
  37.  
  38.     @Override
  39.     public String toString() {
  40.         return String.format("%s %s (%.0fKW) %d", manufacturer, model, power, price);
  41.     }
  42. }
  43.  
  44. class CarCollection {
  45.     List<Car> cars;
  46.  
  47.     public CarCollection() {
  48.         this.cars = new ArrayList<>();
  49.     }
  50.  
  51.     public void sortByPrice(boolean b) {
  52.         Comparator<Car> comparator = Comparator.comparing(Car::getPrice)
  53.                 .thenComparing(Car::getPower);
  54.         if(!b)
  55.             comparator=comparator.reversed();
  56.         cars=cars.stream().sorted(comparator).collect(Collectors.toList());
  57.                 //.forEach(car-> System.out.println(car));
  58.     }
  59.  
  60.     public List<Car> getList() {
  61.         return cars;
  62.     }
  63.  
  64.     public List<Car> filterByManufacturer(String manufacturer) {
  65.         return cars.stream().filter(car->car.getManufacturer().toLowerCase().equals(manufacturer.toLowerCase()))
  66.                 .sorted(Comparator.comparing(Car::getModel))
  67.                 .collect(Collectors.toList());
  68.     }
  69.  
  70.     public void addCar(Car car) {
  71.         cars.add(car);
  72.     }
  73. }
  74.  
  75. public class CarTest {
  76.     public static void main(String[] args) {
  77.         CarCollection carCollection = new CarCollection();
  78.         String manufacturer = fillCollection(carCollection);
  79.         carCollection.sortByPrice(true);
  80.         System.out.println("=== Sorted By Price ASC ===");
  81.         print(carCollection.getList());
  82.         carCollection.sortByPrice(false);
  83.         System.out.println("=== Sorted By Price DESC ===");
  84.         print(carCollection.getList());
  85.         System.out.printf("=== Filtered By Manufacturer: %s ===\n", manufacturer);
  86.         List<Car> result = carCollection.filterByManufacturer(manufacturer);
  87.         print(result);
  88.     }
  89.  
  90.     static void print(List<Car> cars) {
  91.         for (Car c : cars) {
  92.             System.out.println(c);
  93.         }
  94.     }
  95.  
  96.     static String fillCollection(CarCollection cc) {
  97.         Scanner scanner = new Scanner(System.in);
  98.         while (scanner.hasNext()) {
  99.             String line = scanner.nextLine();
  100.             String[] parts = line.split(" ");
  101.             if(parts.length < 4) return parts[0];
  102.             Car car = new Car(parts[0], parts[1], Integer.parseInt(parts[2]),
  103.                     Float.parseFloat(parts[3]));
  104.             cc.addCar(car);
  105.         }
  106.         scanner.close();
  107.         return "";
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement