DamSi

Untitled

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