Minha resolução do desafio opcional, para praticar os conceitos em programação orientada a objetos:
public class Car { private String modelName; private double year1Price; private double year2Price; private double year3Price;
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public double getYear1Price() {
return year1Price;
}
public void setYear1Price(double year1Price) {
this.year1Price = year1Price;
}
public double getYear2Price() {
return year2Price;
}
public void setYear2Price(double year2Price) {
this.year2Price = year2Price;
}
public double getYear3Price() {
return year3Price;
}
public void setYear3Price(double year3Price) {
this.year3Price = year3Price;
}
public void calculateLowerAndHigherPrice() {
if (year1Price > year2Price && year1Price > year3Price) {
System.out.println(" The price of 1° year: " + year1Price + "\n" + " It's higher than 2° year: " + year2Price + " and 3° year: " + year3Price);
} else if (year2Price > year1Price && year2Price > year3Price) {
System.out.println(" The price of 2° year: " + year2Price + "\n" + " It's higher than 1° year: " + year1Price + " and 3° year: " + year3Price);
} else if (year3Price > year1Price && year3Price > year2Price) {
System.out.println(" The price of 3° year: " + year3Price + "\n" + " It's higher than 1° year: " + year1Price + " and 2° year: " + year2Price);
}
}
}
///////////////////////
public class CarModel { public static void main(String[] args) {
Car newCar = new Car();
newCar.setModelName("Fiat Cronos");
newCar.setYear1Price(30.000);
newCar.setYear2Price(40.000);
newCar.setYear3Price(60.000);
System.out.println("The model name is: " + newCar.getModelName());
newCar.calculateLowerAndHigherPrice();
}
}