package principal;
import modelo.Proyecto;
import persistencia.ProyectoRepositoryJson;
import servicio.ProyectoService;
import java.util.List;
import java.util.Scanner;
public class Principal {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
ProyectoRepositoryJson repo =
new ProyectoRepositoryJson("data/proyectos.json");
ProyectoService service = new ProyectoService(repo);
int opcion;
do {
System.out.println("\n=== GESTOR DE PROYECTOS (JSON) ===");
System.out.println("1. Agregar proyecto");
System.out.println("2. Listar proyectos");
System.out.println("3. Salir");
opcion = leerEntero(leer, "Opción: ");
switch (opcion) {
case 1 -> {
try {
Proyecto p = new Proyecto();
System.out.print("Nombre: ");
p.setNombre(leer.nextLine());
System.out.print("Cliente: ");
p.setCliente(leer.nextLine());
p.setDuracionMeses(leerEntero(leer, "Duración meses: "));
p.setId(System.currentTimeMillis()); // id simple
service.crear(p);
System.out.println(" Guardado en JSON");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
case 2 -> {
List<Proyecto> proyectos = service.listar();
if (proyectos.isEmpty()) {
System.out.println("No hay proyectos.");
} else {
proyectos.forEach(System.out::println);
}
}
case 3 -> System.out.println("Saliendo...");
}
} while (opcion != 3);
leer.close();
}
private static int leerEntero(Scanner sc, String msg) {
while (true) {
System.out.print(msg);
if (sc.hasNextInt()) {
int n = sc.nextInt();
sc.nextLine();
return n;
}
System.out.println(" Ingrese número válido");
sc.nextLine();
}
}
}