Advertisement
Valeri173

Untitled

Oct 30th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. abstract class Vehicle {
  2.     protected int speed;
  3.     protected int fuel;
  4.  
  5.     public Vehicle(int speed, int fuel) {
  6.         this.speed = speed;
  7.         this.fuel = fuel;
  8.     }
  9.  
  10.     public abstract void accelerate();
  11.     public abstract void brake();
  12. }
  13.  
  14. class SimpleCar extends Vehicle implements Refuable{
  15.  
  16.     public SimpleCar(int speed, int fuel) {
  17.         super(speed, fuel);
  18.     }
  19.  
  20.     @Override
  21.     public void accelerate() {
  22.         if (fuel > 0) {
  23.             speed += 10;
  24.             fuel--;
  25.             System.out.println("Speed after accelerate: " + speed + ", Fuel: " + fuel);
  26.             if (fuel == 0) {
  27.                 System.out.println("Out of fuel!");
  28.             }
  29.         } else {
  30.             System.out.println("Out of fuel!");
  31.         }
  32.     }
  33.  
  34.     @Override
  35.     public void brake() {
  36.         if (speed >= 10) {
  37.             speed -= 10;
  38.             System.out.println("Speed after brake: " + speed);
  39.         } else {
  40.             System.out.println("Car is already at minimum speed.");
  41.         }
  42.     }
  43.  
  44.     @Override
  45.     public void refuel() {
  46.         fuel += 10;
  47.         System.out.println("Car refueled, Fuel left: " + fuel);
  48.     }
  49. }
  50.  
  51. class ElectricCar extends Vehicle implements Refuable{
  52.  
  53.     public ElectricCar(int speed, int battery) {
  54.         super(speed, battery);
  55.     }
  56.  
  57.     @Override
  58.     public void accelerate() {
  59.         if (fuel > 0) {
  60.             speed += 15;
  61.             fuel--;
  62.             System.out.println("Speed after accelerate: " + speed + ", Battery: " + fuel);
  63.             if (fuel == 0) {
  64.                 System.out.println("Battery empty!");
  65.             }
  66.         } else {
  67.             System.out.println("Battery empty!");
  68.         }
  69.     }
  70.  
  71.     @Override
  72.     public void brake() {
  73.         if (speed >= 10) {
  74.             speed -= 10;
  75.             System.out.println("Speed after brake: " + speed);
  76.         } else {
  77.             System.out.println("Car is already at minimum speed.");
  78.         }
  79.     }
  80.    
  81.     @Override
  82.     public void refuel() {
  83.         fuel += 20;
  84.         System.out.println("Electric car recharged, Battery left: " + fuel);
  85.     }
  86. }
  87.  
  88.  
  89. public interface Refuable {
  90.     void refuel();
  91. }
  92.  
  93.  
  94. public class Main {
  95.     public static void main(String[] args) {
  96.         int initialSpeedCar = 20;
  97.         int initialFuelCar = 2;
  98.         int initialSpeedElectricCar = 30;
  99.         int initialBatteryElectricCar = 2;
  100.  
  101.         if (args.length >= 4) {
  102.             initialSpeedCar = Integer.parseInt(args[0]);
  103.             initialFuelCar = Integer.parseInt(args[1]);
  104.             initialSpeedElectricCar = Integer.parseInt(args[2]);
  105.             initialBatteryElectricCar = Integer.parseInt(args[3]);
  106.         } else {
  107.             System.out.println("Default values will be used for vehicles.");
  108.         }
  109.  
  110.         SimpleCar simpleCar = new SimpleCar(initialSpeedCar, initialFuelCar);
  111.         ElectricCar electricCar = new ElectricCar(initialSpeedElectricCar, initialBatteryElectricCar);
  112.  
  113.         System.out.println("SimpleCar:");
  114.         simpleCar.accelerate();
  115.         simpleCar.accelerate();
  116.         simpleCar.refuel();
  117.  
  118.         System.out.println("\nElectricCar:");
  119.         electricCar.accelerate();
  120.         electricCar.accelerate();
  121.         electricCar.refuel();
  122.     }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement