238025681

Raw data

Jun 23rd, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1.  
  2. package RawData;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.StringJoiner;
  8.  
  9. /**
  10.  *
  11.  * @author chobi
  12.  */
  13.  
  14. class Car{
  15.     private static final int NUMBER_OF_TIRES = 4;
  16.    
  17.     private String model;
  18.     private Engine engine;
  19.     private Cargo cargo;
  20.     private Tire[] tire;
  21.  
  22.     public Car(String model, Engine engine, Cargo cargo, Tire[] tire) {
  23.         this.model = model;
  24.         this.engine = engine;
  25.         this.cargo = cargo;
  26.         this.tire = tire;
  27.     }
  28.  
  29.     public static int getNUMBER_OF_TIRES() {
  30.         return NUMBER_OF_TIRES;
  31.     }
  32.  
  33.     public String getModel() {
  34.         return model;
  35.     }
  36.  
  37.     public Engine getEngine() {
  38.         return engine;
  39.     }
  40.  
  41.     public Cargo getCargo() {
  42.         return cargo;
  43.     }
  44.  
  45.     public Tire[] getTire() {
  46.         return tire;
  47.     }
  48.    
  49.    
  50.  
  51.     @Override
  52.     public String toString() {
  53.        
  54.         StringJoiner str = new StringJoiner(" ");
  55.         for (int i = 0; i < tire.length; i++) {
  56.             str.add(this.tire[i].toString());
  57.            
  58.         }
  59.         return "Car{" + "model=" + model + ", engine=" + engine + ", cargo=" + cargo + ", tire=" + str + '}';
  60.     }
  61.    
  62.    
  63.    
  64.  
  65. }
  66.  
  67. class Engine{
  68.    
  69.     private int speed;
  70.     private int power;
  71.  
  72.     public Engine(int speed, int power) {
  73.         this.speed = speed;
  74.         this.power = power;
  75.     }
  76.  
  77.     public int getPower() {
  78.         return power;
  79.     }
  80.    
  81.    
  82.  
  83.     @Override
  84.     public String toString() {
  85.         return "Engine{" + "speed=" + speed + ", power=" + power + '}';
  86.     }
  87.    
  88.    
  89. }
  90.  
  91. class Cargo{
  92.     private int weight;
  93.     private String type;
  94.  
  95.     public Cargo(int weight, String type) {
  96.         this.weight = weight;
  97.         this.type = type;
  98.     }
  99.  
  100.     public String getType() {
  101.         return type;
  102.     }
  103.  
  104.    
  105.    
  106.    
  107.  
  108.     @Override
  109.     public String toString() {
  110.         return "Cargo{" + "weight=" + weight + ", type=" + type + '}';
  111.     }
  112.    
  113.    
  114. }
  115.  
  116. class Tire{
  117.     private double pressure;
  118.     private int age;
  119.  
  120.     public Tire(double pressure, int age) {
  121.         this.pressure = pressure;
  122.         this.age = age;
  123.     }
  124.  
  125.     public double getPressure() {
  126.         return pressure;
  127.     }
  128.  
  129.    
  130.    
  131.  
  132.     @Override
  133.     public String toString() {
  134.         return "Tire{" + "pressure=" + pressure + ", age=" + age + '}';
  135.     }
  136.    
  137.    
  138. }
  139.  
  140.  
  141.  
  142. public class RawData {
  143.     public static void main(String[] args) {
  144.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
  145.            
  146.             int numLine = Integer.parseInt(reader.readLine());
  147.             ArrayList<Car> cars = new ArrayList<Car>();
  148.            
  149.             //ChevroletAstro 200 180 1000 fragile 1.3 1 1.5 2 1.4 2 1.7 4
  150.  
  151.             for (int i = 0; i < numLine; i++) {
  152.                 String[] inputCars = reader.readLine().split("\\s+");
  153.                 String model = inputCars[0];
  154.                 Engine engine = new Engine(Integer.parseInt(inputCars[1]), Integer.parseInt(inputCars[2]));
  155.                 Cargo cargo = new Cargo(Integer.parseInt(inputCars[3]), inputCars[4]);
  156.                 Tire[] tire = createTires(inputCars);
  157.                
  158.                 cars.add(new Car(model, engine, cargo, tire));
  159.                
  160.             }
  161.            
  162.            
  163.             String getCargoType = reader.readLine();
  164.            
  165.             if (getCargoType.equals("fragile")) {
  166.                 cars.stream()
  167.                         .filter(c -> c.getCargo().getType().equals(getCargoType))
  168.                         .filter(s -> averagePressure(s) < 1 )
  169.                         .forEach(s -> System.out.println(s.getModel()));
  170.             }else if(getCargoType.equals("flamable")){
  171.                 cars.stream()
  172.                         .filter(c -> c.getCargo().getType().equals(getCargoType))
  173.                         .filter(s -> s.getEngine().getPower() > 250)
  174.                         .forEach(s -> System.out.println(s.getModel()));
  175.             }
  176.            
  177.            
  178.            
  179.         } catch (Exception e) {
  180.             e.printStackTrace();
  181.         }
  182.     }
  183.  
  184.     private static Tire[] createTires(String[] inputCars) {
  185.         Tire[] tires = new Tire[Car.getNUMBER_OF_TIRES()];
  186.         int wheel = 0;
  187.        
  188.         for (int i = 0; i < tires.length; i++) {
  189.             tires[i] = new Tire(Double.parseDouble(inputCars[5 + wheel++]), Integer.parseInt(inputCars[5+ wheel++]));
  190.            
  191.         }
  192.        
  193.         return tires;
  194.        
  195.     }
  196.  
  197.     private static double averagePressure(Car s) {
  198.         int wheel = s.getTire().length;
  199.         double average = 0L;
  200.         for (int i = 0; i < wheel; i++) {
  201.             average += s.getTire()[i].getPressure();            
  202.         }
  203.         return average / wheel;
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment