Advertisement
Martina312

[НП] - Автомобили

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