164 lines
6.3 KiB
Java
164 lines
6.3 KiB
Java
package com.mycompany.asientosconcierto;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.io.*;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
|
|
|
|
public class AsientosConcierto extends JFrame {
|
|
private final int FILAS = 5;
|
|
private final int COLUMNAS = 10;
|
|
private JButton[][] asientos = new JButton[FILAS][COLUMNAS];
|
|
private List<String> asientosVendidos = new ArrayList<>();
|
|
private JButton btnConfirmarVenta;
|
|
private JButton btnGenerarReporte;
|
|
|
|
public AsientosConcierto() {
|
|
setTitle("Selección de Asientos - Concierto");
|
|
setSize(800, 600);
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
setLocationRelativeTo(null);
|
|
setLayout(new BorderLayout());
|
|
|
|
JPanel panelAsientos = new JPanel(new GridLayout(FILAS, COLUMNAS, 5, 5));
|
|
|
|
for (int i = 0; i < FILAS; i++) {
|
|
for (int j = 0; j < COLUMNAS; j++) {
|
|
JButton asiento = new JButton();
|
|
asiento.setPreferredSize(new Dimension(50, 50));
|
|
|
|
if (i == 0) {
|
|
asiento.setBackground(Color.BLUE);
|
|
asiento.setToolTipText("Primera Fila");
|
|
} else if (i == 1 || i == 2) {
|
|
asiento.setBackground(new Color(218, 165, 32));
|
|
asiento.setToolTipText("VIP");
|
|
} else {
|
|
asiento.setBackground(Color.GREEN);
|
|
asiento.setToolTipText("Normal");
|
|
}
|
|
|
|
final int fila = i;
|
|
final int columna = j;
|
|
|
|
asiento.addActionListener(new ActionListener() {
|
|
private boolean seleccionado = false;
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (!seleccionado && asiento.getBackground() != Color.GRAY) {
|
|
asiento.setBackground(Color.RED);
|
|
asientosVendidos.add("Fila " + fila + ", Columna " + columna + " - " + asiento.getToolTipText());
|
|
seleccionado = true;
|
|
} else if (asiento.getBackground() != Color.GRAY) {
|
|
restaurarColorOriginal(asiento);
|
|
asientosVendidos.remove("Fila " + fila + ", Columna " + columna + " - " + asiento.getToolTipText());
|
|
seleccionado = false;
|
|
}
|
|
}
|
|
});
|
|
|
|
asientos[i][j] = asiento;
|
|
panelAsientos.add(asiento);
|
|
}
|
|
}
|
|
|
|
JPanel panelBotones = new JPanel();
|
|
btnConfirmarVenta = new JButton("Confirmar Venta");
|
|
btnGenerarReporte = new JButton("Generar reporte de venta");
|
|
|
|
btnConfirmarVenta.addActionListener(e -> confirmarVenta());
|
|
|
|
btnGenerarReporte.addActionListener(e -> generarReporteDeVentaTXT());
|
|
|
|
panelBotones.add(btnConfirmarVenta);
|
|
panelBotones.add(btnGenerarReporte);
|
|
|
|
add(panelAsientos, BorderLayout.CENTER);
|
|
add(panelBotones, BorderLayout.SOUTH);
|
|
|
|
setVisible(true);
|
|
}
|
|
|
|
private void restaurarColorOriginal(JButton asiento) {
|
|
if (asiento.getToolTipText().equals("Primera Fila")) {
|
|
asiento.setBackground(Color.BLUE);
|
|
} else if (asiento.getToolTipText().equals("VIP")) {
|
|
asiento.setBackground(new Color(218, 165, 32));
|
|
} else {
|
|
asiento.setBackground(Color.GREEN);
|
|
}
|
|
}
|
|
|
|
private void confirmarVenta() {
|
|
if (asientosVendidos.isEmpty()) {
|
|
JOptionPane.showMessageDialog(this, "No ha seleccionado asientos.", "Error", JOptionPane.ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
StringBuilder detallesCompra = new StringBuilder("Asientos vendidos:\n");
|
|
for (String asientoInfo : asientosVendidos) {
|
|
detallesCompra.append(asientoInfo).append("\n");
|
|
}
|
|
|
|
JOptionPane.showMessageDialog(this, detallesCompra.toString(), "Comprobante de Compra", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
// Marcar los asientos como vendidos
|
|
for (int i = 0; i < FILAS; i++) {
|
|
for (int j = 0; j < COLUMNAS; j++) {
|
|
if (asientos[i][j].getBackground() == Color.RED) {
|
|
asientos[i][j].setBackground(Color.GRAY);
|
|
asientos[i][j].setEnabled(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Guardar el estado de los asientos vendidos en un archivo
|
|
guardarAsientosVendidos();
|
|
|
|
asientosVendidos.clear();
|
|
}
|
|
|
|
private void generarReporteDeVentaTXT() {
|
|
File file = new File("reporte_ventas.txt");
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
|
|
if (asientosVendidos.isEmpty()) {
|
|
JOptionPane.showMessageDialog(this, "No hay nuevas ventas para generar reporte.", "Error", JOptionPane.ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
writer.write("Reporte de Ventas:\n\n");
|
|
for (String asientoInfo : asientosVendidos) {
|
|
writer.write(asientoInfo + "\n");
|
|
}
|
|
|
|
JOptionPane.showMessageDialog(this, "Reporte generado con éxito: " + file.getAbsolutePath(), "Reporte Generado", JOptionPane.INFORMATION_MESSAGE);
|
|
} catch (IOException ex) {
|
|
JOptionPane.showMessageDialog(this, "Error al generar el reporte: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
private void guardarAsientosVendidos() {
|
|
File file = new File("asientos_vendidos.txt");
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
|
|
for (int i = 0; i < FILAS; i++) {
|
|
for (int j = 0; j < COLUMNAS; j++) {
|
|
if (asientos[i][j].getBackground() == Color.GRAY) {
|
|
writer.write(i + "," + j + "\n");
|
|
}
|
|
}
|
|
}
|
|
} catch (IOException ex) {
|
|
JOptionPane.showMessageDialog(this, "Error al guardar los asientos vendidos: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SwingUtilities.invokeLater(() -> new AsientosConcierto());
|
|
}
|
|
}
|