Java Theater Simulation Source Code Using Collections
Classified in Computers
Written on in
English with a size of 5.06 KB
Java Theater Management Source Code
Below is the complete, fixed Java source code for managing theater attendees using various data structures like lists, queues, and stacks.
public class Teatro {
private static File archivo;
private static FileReader fr;
private static BufferedReader br;
private static List<Espectador> listaEspectadores = new ArrayList<>();
private static Queue<Espectador> colaEspectadores = new ArrayDeque<>();
private static Stack<Espectador> teatro = new Stack<>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
archivo = new File("C:\\Users\\Criscriscris\\Documents\\NetBeansProjects\\Teatro\\datosEntrada.csv");
fr = new FileReader(archivo);
br = new BufferedReader(fr);
String linea;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df = DateFormat.getDateInstance();
int cntLinea = 0;
while ((linea = br.readLine()) != null) {
if (cntLinea != 0) {
String[] espCsv = linea.split(";");
String nombres = espCsv[0];
String apellidos = espCsv[1];
char sexo = espCsv[2].charAt(0);
Date fechaDeNacimiento = df.parse(espCsv[3]);
int númeroDeBoleto = Integer.parseInt(espCsv[4]);
Espectador esp = new Espectador(nombres, apellidos, sexo, fechaDeNacimiento, númeroDeBoleto);
try {
listaEspectadores.add(esp);
} catch (Exception e) {
System.err.println("Línea de datosEntrada no ingresada. " + e.getMessage());
}
}
++cntLinea;
}
System.out.println("extraer1000");
extraer1000EspectadoresAleatoriamente(listaEspectadores);
System.out.println("ingresar");
ingresarEspectadoresAlTeatro(colaEspectadores);
System.out.println("grabar1");
grabarLayoutTeatro(teatro);
System.out.println("grabar2");
grabarSalidaEspectadores(teatro);
} catch (IOException | ParseException | NumberFormatException e) {
System.err.println(e.getMessage());
} finally {
try {
if (null != fr) {
fr.close();
}
} catch (Exception e2) {
System.err.println(e2.getMessage());
}
}
}
public static void extraer1000EspectadoresAleatoriamente(List<Espectador> listaEspectadores) {
Collections.shuffle(listaEspectadores);
listaEspectadores.subList(0, 1000);
for (Espectador esp : listaEspectadores) {
colaEspectadores.offer(esp);
}
}
public static void ingresarEspectadoresAlTeatro(Queue<Espectador> colaEspectadores) {
for (Espectador esp : colaEspectadores) {
teatro.push(esp);
}
}
public static void grabarLayoutTeatro(Stack<Espectador> teatro) {
FileWriter fichero = null;
PrintWriter pw = null;
try {
fichero = new FileWriter("layoutTeatro.txt");
pw = new PrintWriter(fichero);
int cntAsientos = 0;
char cntFilas = 'A';
Stack<Espectador> teatro2 = (Stack<Espectador>) teatro.clone();
while (!teatro2.empty()) {
++cntAsientos;
if (cntAsientos == 25) {
cntAsientos = 0;
++cntFilas;
pw.println();
}
Espectador esp = teatro2.pop();
pw.print(cntFilas + " " + esp.infoEspectador() + ";");
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
try {
if (null != fichero) {
fichero.close();
}
} catch (Exception e2) {
System.err.println(e2.getMessage());
}
}
}
public static void grabarSalidaEspectadores(Stack<Espectador> teatro) {
FileWriter fichero = null;
PrintWriter pw;
try {
fichero = new FileWriter("datosSalida.csv");
pw = new PrintWriter(fichero);
while (!teatro.empty()) {
Espectador esp = teatro.pop();
pw.println(esp.getNombres() + ";" + esp.getApellidos() + ";" + esp.getSexo() + ";" + esp.getFechaDeNacimiento() + ";" + esp.getNúmeroDeBoleto());
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
try {
if (null != fichero) {
fichero.close();
}
} catch (Exception e2) {
System.err.println(e2.getMessage());
}
}
}
}