import java.util.List;
interface serviciotransporte {
double calcularcosto();
String getnombre();
}
class ciudad implements serviciotransporte {
private static final double tarifabase = 4000;
private static final double valorkm = 1500;
private final double kilometros;
public ciudad(double kilometros) {
if (kilometros < 0) {
throw new IllegalArgumentException("Los kilómetros no pueden ser negativos");
}
this.kilometros = kilometros;
}
@Override
public double calcularcosto() {
return tarifabase + (kilometros * valorkm);
}
@Override
public String getnombre() {
return "Ciudad";
}
}
class viajeintermunicipal implements serviciotransporte {
private static final double tarifabase = 10000;
private static final double valorkm = 1200;
private final double kilometros;
private final double peaje;
public viajeintermunicipal(double kilometros, double peaje) {
if (kilometros < 0 || peaje < 0) {
throw new IllegalArgumentException("Kilómetros y peaje no pueden ser negativos");
}
this.kilometros = kilometros;
this.peaje = peaje;
}
@Override
public double calcularcosto() {
return tarifabase + (kilometros * valorkm) + peaje;
}
@Override
public String getnombre() {
return "Intermunicipal";
}
}
class transporteespecial implements serviciotransporte {
private static final double tarifabase = 30000;
private static final double valorhora = 25000;
private final double horas;
public transporteespecial(double horas) {
if (horas < 0) {
throw new IllegalArgumentException("Las horas no pueden ser negativas");
}
this.horas = horas;
}
@Override
public double calcularcosto() {
return tarifabase + (horas * valorhora);
}
@Override
public String getnombre() {
return "Especial";
}
}
class calculadoracostos {
public double procesar(List<serviciotransporte> servicios) {
double total = 0;
for (serviciotransporte servicio : servicios) {
double costo = servicio.calcularcosto();
System.out.printf(
"%-20s $%,.0f%n",
servicio.getnombre(),
costo
);
total += costo;
}
return total;
}
}
public class Main {
public static void main(String[] args) {
List<serviciotransporte> servicios = List.of(
new ciudad(8),
new viajeintermunicipal(120, 15000),
new transporteespecial(3)
);
System.out.println(" costo de servicio de trsanportes \n");
calculadoracostos calculadora = new calculadoracostos();
double total = calculadora.procesar(servicios);
System.out.printf("%-20s $%,.0f%n", "TOTAL:", total);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: