Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class SpeedRacing {
  8.     static List<Car> carList = new ArrayList<>();
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         int carModels = Integer.parseInt(bf.readLine());
  13.  
  14.         for(int i=0;i<carModels;i++){
  15.             String[] input = bf.readLine().split("\\s");
  16.             addCars(input);
  17.         }
  18.         String line;
  19.         while(!(line = bf.readLine()).equals("End")){
  20.             String[] input = line.split("\\s");
  21.             tryToDriveCars(input);
  22.         }
  23.         carList.stream().forEach(System.out::println);
  24.     }
  25.  
  26.     private static void tryToDriveCars(String[] input) {
  27.         String currentCarModel = input[1];
  28.         int wantedDistance = Integer.parseInt(input[2]);
  29.         for(Car car : carList){
  30.             if(car.getCarModel().equals(currentCarModel)){
  31.                 car.moveCar(wantedDistance);
  32.             }
  33.         }
  34.     }
  35.  
  36.     private static void addCars(String[] input){
  37.         String carModel = input[0];
  38.         double fuelAmount = Double.parseDouble(input[1]);
  39.         double fuelCost = Double.parseDouble(input[2]);
  40.         Car currentCar = new Car(carModel,fuelAmount,fuelCost);
  41.         carList.add(currentCar);
  42.     }
  43. }
  44.  
  45. class Car {
  46.     private double fuelAmount;
  47.     private double fuelCost;
  48.     private int distanceTraveled;
  49.     private String carModel;
  50.  
  51.  
  52.     Car(String name, double fuelAmount, double fuelCost) {
  53.         this.setCarModel(name);
  54.         this.setFuelAmount(fuelAmount);
  55.         this.setFuelCost(fuelCost);
  56.         this.setDistanceTraveled(0);
  57.     }
  58.  
  59.     public int getDistanceTraveled() {
  60.         return this.distanceTraveled;
  61.     }
  62.  
  63.     public void setDistanceTraveled(int distanceTraveled) {
  64.         this.distanceTraveled = distanceTraveled;
  65.     }
  66.  
  67.     public String getCarModel() {
  68.         return this.carModel;
  69.     }
  70.  
  71.     private void setCarModel(String carModel) {
  72.         this.carModel = carModel;
  73.     }
  74.  
  75.     public double getFuelAmount() {
  76.         return this.fuelAmount;
  77.     }
  78.  
  79.     private void setFuelAmount(double fuelAmount) {
  80.         this.fuelAmount = fuelAmount;
  81.     }
  82.  
  83.     public double getFuelCost() {
  84.         return this.fuelCost;
  85.     }
  86.  
  87.     private void setFuelCost(double fuelCost) {
  88.         this.fuelCost = fuelCost;
  89.     }
  90.  
  91.     public  void moveCar(int wantedDistance) {
  92.         double fuelNeeded = wantedDistance * this.getFuelCost();
  93.         if (fuelNeeded <= this.getFuelAmount()) {
  94.             this.setFuelAmount(this.getFuelAmount() - fuelNeeded);
  95.             this.setDistanceTraveled(this.getDistanceTraveled() + wantedDistance);
  96.         }else{
  97.             System.out.println("Insufficient fuel for the drive");
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     public String toString() {
  103.         return String.format("%s %.2f %d", this.getCarModel(),this.getFuelAmount(),this.getDistanceTraveled());
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement