Advertisement
desislava_topuzakova

03.SpeedRacing_Car

Oct 5th, 2020
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. package SpeedRacing_03;
  2.  
  3. public class Car {
  4.  
  5.     //Model, fuel amount, fuel cost for 1 kilometer and distance traveled.
  6.     private String model;
  7.     private double fuel;
  8.     private double consumption;
  9.     private int distance;
  10.  
  11.     public Car(String model, double fuel, double consumption) {
  12.         this.model = model;
  13.         this.fuel = fuel;
  14.         this.consumption = consumption;
  15.         this.distance = 0;
  16.     }
  17.  
  18.     public boolean drive(int distanceToDrive) {
  19.         //разстояние -> колко гориво ще ни отнеме
  20.         double needFuel = distanceToDrive * this.consumption;
  21.         if(needFuel <= this.fuel) {
  22.             //изминаваме разстоянието
  23.             this.distance += distanceToDrive;
  24.             this.fuel -= needFuel;
  25.             return true;
  26.         }
  27.         return false;
  28.     }
  29.  
  30.     @Override
  31.     public String toString() {
  32.         //AudiA4 1.00 50
  33.         return String.format("%s %.2f %d", this.model, this.fuel, this.distance);
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement