Sofe1204

Untitled

Nov 11th, 2022 (edited)
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. class CAR {
  4.     String name;
  5.     float consumption;
  6.     String fuel;
  7.     CAR(String name, float consumption,String fuel)
  8.     {
  9.         this.name=name;
  10.         this.consumption=consumption;
  11.         this.fuel=fuel;
  12.     }
  13.     public String getName()
  14.     {
  15.         return name;
  16.     }
  17.     public float getConsumption()
  18.     {
  19.         return consumption;
  20.     }
  21.     public String getFuel()
  22.     {
  23.         return fuel;
  24.     }
  25.     public double COST(int km)
  26.     {
  27.         double price=0;
  28.         double cost=0;
  29.         double x=0;
  30.         if(fuel=="GASOLINE")
  31.         {
  32.             price=69.50;
  33.             x=(consumption*km)/100;
  34.             return cost=x*price;
  35.         }
  36.         else if(fuel=="DIESEL")
  37.         {
  38.             price=57.50;
  39.              x=(consumption*km)/100;
  40.             return cost=x*price;
  41.         }
  42.         else
  43.         {
  44.             price=37;
  45.              x=(consumption*km)/100;
  46.             return cost=x*price;
  47.         }
  48.     }
  49.    
  50.     public static void main(String[] args) {
  51.        
  52.         CAR[] cars = new CAR[5];
  53.         cars[0] = new CAR("Ford", 6, "Diesel");
  54.         cars[1] = new CAR("Mercedes", 12, "Gasoline");
  55.         cars[2] = new CAR("Toyota", 5, "GAS");
  56.         cars[3] = new CAR("Mazda", 8, "Diesel");
  57.         cars[4] = new CAR("Range Rover", 60, "Gasoline");
  58.         for (int i = 0; i < 5; i++) {
  59.             System.out.println("Car name: " +cars[i].getName()+ " Consumption: " +cars[i].getConsumption()+ " Fuel type: " +cars[i].getFuel());
  60.             System.out.println("-----------------------------------------");
  61.    
  62.         }   double minValue = 10000000;
  63.             int indexOfminValue = -1;
  64.             for (int i = 0; i < 5; i++) {
  65.                 if(cars[i].COST(800)<minValue)
  66.                 {
  67.                     indexOfminValue=i;
  68.                  
  69.                 }
  70.                  System.out.println("You will pass 800km with least money if you drive "  +cars[indexOfminValue].getName() + ". It will cost you" + cars[indexOfminValue].COST(800) + "den.");
  71.             }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment