Filip_Markoski

[NP] Automobiles

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