//se creo clase Compra y Clase TarjetaDeCredito aparte import java.util.*;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
double valorTarjeta = leerDoubleConLimite(sc, "Escriba el límite de la tarjeta:", 0);
TarjetaDeCredito tarjeta = new TarjetaDeCredito(valorTarjeta);
boolean continuar = true;
while (continuar) {
String descripcion = leerTexto(sc, "Escribe la descripción de la compra:");
double valor = leerDoubleConLimite(sc, "Escriba el valor de la compra:", 0);
if (tarjeta.realizarCompra(descripcion, valor)) {
System.out.println("\nCompra realizada con éxito!");
} else {
System.out.println("\nSaldo insuficiente. Disponible: $" + tarjeta.getSaldo());
}
if(tarjeta.getSaldo()==0){
continuar=false;
}else {
continuar = leerOpcionContinuar(sc);
}
}
// Mostrar resumen de compras
System.out.println("\n--- Resumen de compras ---");
tarjeta.getListaDeCompras().stream()
.sorted(Comparator.comparing(Compra::getNombre))
.forEach(c -> System.out.println("- " + c.getNombre() + ": $" + c.getValor()));
System.out.println("Saldo de tarjeta: $" + tarjeta.getSaldo());
}
// Métodos auxiliares
private static double leerDoubleConLimite(Scanner sc, String mensaje, double limiteMinimo) {
double valor;
while (true) {
System.out.println(mensaje);
try {
valor = sc.nextDouble();
sc.nextLine();
if (valor <= limiteMinimo) {
System.out.println("El valor debe ser mayor que " + limiteMinimo + ".");
} else {
return valor;
}
} catch (InputMismatchException e) {
System.out.println("Error: Ingresa un número válido.");
sc.nextLine();
}
}
}
private static String leerTexto(Scanner sc, String mensaje) {
String texto;
while (true) {
System.out.println(mensaje);
texto = sc.nextLine().trim();
if (texto.isEmpty()) {
System.out.println("El texto no puede estar vacío.");
} else {
return texto;
}
}
}
private static boolean leerOpcionContinuar(Scanner sc) {
while (true) {
System.out.println("\n¿Desea registrar otra compra? (0 para salir / 1 para continuar):");
try {
int opcion = sc.nextInt();
sc.nextLine();
if (opcion == 0) return false;
if (opcion == 1) return true;
System.out.println("Opción no válida. Ingresa 0 o 1.");
} catch (InputMismatchException e) {
System.out.println("Error: Ingresa un número entero válido.");
sc.nextLine();
}
}
}
}