Andziev

Автомобили (JAVA 8)

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