public class Transporte
{
protected int capacidad;
protected String tipo;
protected int velocidadMax;
protected int boletosVendidos;
protected double ingresosTotales;
public Transporte(int capacidad, String tipo, int velocidadMax)
{
this.setCapacidad(capacidad);
this.setTipo(tipo);
this.setVelocidadMax(velocidadMax);
this.boletosVendidos = 0;
this.ingresosTotales = 0;
}
public int getCapacidad()
{
return capacidad;
}
public void setCapacidad(int capacidad)
{
this.capacidad = capacidad;
}
public String getTipo()
{
return tipo;
}
public void setTipo(String tipo)
{
this.tipo = tipo;
}
public int getVelocidadMax()
{
return velocidadMax;
}
public void setVelocidadMax(int velocidadMax)
{
this.velocidadMax = velocidadMax;
}
public int getBoletosVendidos()
{
return boletosVendidos;
}
public double getIngresosTotales()
{
return ingresosTotales;
}
public double calcularTarifa(int edad, boolean discapacidad)
{
if (edad < 12)
{
return 5.0;
} else
if (edad >= 65 || discapacidad)
{
return 7.0;
} else
{
return 12.0;
}
}
public void venderBoletos(int edad, boolean discapacidad)
{
if (this.boletosVendidos + 1 > this.capacidad)
{
System.out.println("No hay boletos disponibles");
} else
{
double tarifa = this.calcularTarifa(edad, discapacidad);
this.boletosVendidos += 1;
this.ingresosTotales += tarifa;
System.out.println("Boleto vendido: $" + tarifa);
}
}
@Override
public String toString()
{
return "Transporte{" + "tipo=" + this.getTipo() + "\ncapacidad=" + this.getCapacidad() + "\nvelocidadMax=" + this.getVelocidadMax() +
"\nboletosVendidos=" + this.getBoletosVendidos() + "\ningresosTotales=$" + this.getIngresosTotales() + "}";
}
}
class Tren extends Transporte
{
public Tren(int capacidad, int velocidadMax)
{
super(capacidad, "Tren", velocidadMax);
}
@Override
public String toString()
{
return super.toString();
}
}
class Autobus extends Transporte
{
public Autobus(int capacidad, int velocidadMax)
{
super(capacidad, "Autobus", velocidadMax);
}
@Override
public String toString()
{
return super.toString();
}
}
import java.util.Scanner;
public class AppTransporte
{
private static Transporte[] transportes =
{
new Autobus(40, 60),
new Tren(200, 120)
};
public static void corteDeCaja()
{
double totalIngresos = 0;
for (Transporte t : transportes)
{
totalIngresos += t.getIngresosTotales();
}
System.out.println("Total de ingresos: $" + totalIngresos);
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
boolean salir = false;
while (!salir)
{
System.out.println("\nMenu:");
System.out.println("1. Vender boletos");
System.out.println("2. Ver estado de los transportes");
System.out.println("3. Corte de caja");
System.out.println("4. Salir");
System.out.print("Seleccione una opcion: ");
int opcion = scanner.nextInt();
switch (opcion)
{
case 1:
System.out.println("Seleccione el transporte:");
for (int i = 0; i < transportes.length; i++)
{
System.out.println(i + ". " + transportes[i].getClass().getSimpleName());
}
int transporteSeleccionado = scanner.nextInt();
if (transporteSeleccionado < 0 || transporteSeleccionado >= transportes.length)
{
System.out.println("Opcion invalida.");
break;
}
System.out.print("Ingrese la cantidad de boletos: ");
int cantidad = scanner.nextInt();
for (int i = 0; i < cantidad; i++)
{
System.out.println("Boleto " + (i + 1) + ":");
System.out.print("Ingrese la edad del pasajero: ");
int edad = scanner.nextInt();
System.out.print("¿Tiene discapacidad? (true/false): ");
boolean discapacidad = scanner.nextBoolean();
transportes[transporteSeleccionado].venderBoletos(edad, discapacidad);
}
break;
case 2:
System.out.println("\nEstado actual de los transportes:");
for (Transporte t : transportes)
{
System.out.println(t);
}
break;
case 3:
corteDeCaja();
break;
case 4:
System.out.println("Saliendo del sistema...");
salir = true;
break;
default:
System.out.println("Opcion invalida");
}
}
scanner.close();
}
}