Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class CAR {
- String name;
- float consumption;
- String fuel;
- CAR(String name, float consumption,String fuel)
- {
- this.name=name;
- this.consumption=consumption;
- this.fuel=fuel;
- }
- public String getName()
- {
- return name;
- }
- public float getConsumption()
- {
- return consumption;
- }
- public String getFuel()
- {
- return fuel;
- }
- public double COST(int km)
- {
- double price=0;
- double cost=0;
- double x=0;
- if(fuel=="GASOLINE")
- {
- price=69.50;
- x=(consumption*km)/100;
- return cost=x*price;
- }
- else if(fuel=="DIESEL")
- {
- price=57.50;
- x=(consumption*km)/100;
- return cost=x*price;
- }
- else
- {
- price=37;
- x=(consumption*km)/100;
- return cost=x*price;
- }
- }
- public static void main(String[] args) {
- CAR[] cars = new CAR[5];
- cars[0] = new CAR("Ford", 6, "Diesel");
- cars[1] = new CAR("Mercedes", 12, "Gasoline");
- cars[2] = new CAR("Toyota", 5, "GAS");
- cars[3] = new CAR("Mazda", 8, "Diesel");
- cars[4] = new CAR("Range Rover", 60, "Gasoline");
- for (int i = 0; i < 5; i++) {
- System.out.println("Car name: " +cars[i].getName()+ " Consumption: " +cars[i].getConsumption()+ " Fuel type: " +cars[i].getFuel());
- System.out.println("-----------------------------------------");
- } double minValue = 10000000;
- int indexOfminValue = -1;
- for (int i = 0; i < 5; i++) {
- if(cars[i].COST(800)<minValue)
- {
- indexOfminValue=i;
- }
- 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.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment