Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.text.DecimalFormat;
  4.  
  5. public class Car {
  6. private String carModel;
  7. private long fuelAmount;
  8. private double fuelForOneKm;
  9. private int distance;
  10.  
  11. public Car(String carModel, long fuelAmount, double fuelForOneKm) {
  12. this.carModel = carModel;
  13. this.fuelAmount = fuelAmount;
  14. this.fuelForOneKm = fuelForOneKm;
  15. this.distance = 0;
  16. }
  17.  
  18. public String getCarModel() {
  19. return carModel;
  20. }
  21.  
  22. public void Drive(int kmToDrive){
  23. if(kmToDrive * fuelForOneKm <= fuelAmount){
  24.  
  25. this.distance += kmToDrive;
  26. this.fuelAmount -= (kmToDrive * fuelForOneKm);
  27. }
  28. else
  29. {
  30. System.out.println("Insufficient fuel for the drive");
  31. }
  32. }
  33.  
  34. @Override
  35. public String toString() {
  36. String fuelInTheTank = new DecimalFormat("#.00").format(fuelAmount);
  37.  
  38. return carModel + " " + fuelInTheTank + " " + distance;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement