diff --git a/NetBeansProjects/ticketCine/pom.xml b/NetBeansProjects/ticketCine/pom.xml new file mode 100644 index 0000000..17bd344 --- /dev/null +++ b/NetBeansProjects/ticketCine/pom.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.mycompany</groupId> + <artifactId>ticketCine</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.release>23</maven.compiler.release> + </properties> + <dependencies> + <!-- MySQL Connector --> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>8.0.26</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>3.1.0</version> + <configuration> + <mainClass>com.mycompany.ticketcine.MainApp</mainClass> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Asiento.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Asiento.java new file mode 100644 index 0000000..1550da7 --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Asiento.java @@ -0,0 +1,53 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.ticketcine; + +public class Asiento { + private int id; + private int idSala; + private int numeroAsiento; + private String estado; + + // Constructor + public Asiento(int id, int idSala, int numeroAsiento, String estado) { + this.id = id; + this.idSala = idSala; + this.numeroAsiento = numeroAsiento; + this.estado = estado; + } + + // Getters y Setters + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getIdSala() { + return idSala; + } + + public void setIdSala(int idSala) { + this.idSala = idSala; + } + + public int getNumeroAsiento() { + return numeroAsiento; + } + + public void setNumeroAsiento(int numeroAsiento) { + this.numeroAsiento = numeroAsiento; + } + + public String getEstado() { + return estado; + } + + public void setEstado(String estado) { + this.estado = estado; + } +} diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/CompraBoletosApp.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/CompraBoletosApp.java new file mode 100644 index 0000000..390c84c --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/CompraBoletosApp.java @@ -0,0 +1,165 @@ +package com.mycompany.ticketcine; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.*; +import java.util.HashMap; +import java.util.Map; + +public class CompraBoletosApp { + private JFrame frame; + private JComboBox<String> peliculasComboBox; + private JComboBox<String> salasComboBox; + private JPanel asientosPanel; + private Map<Integer, JButton> asientosBotones; + + public CompraBoletosApp() { + frame = new JFrame("Compra de Boletos"); + frame.setSize(500, 500); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLayout(new BorderLayout()); + + JPanel topPanel = new JPanel(); + peliculasComboBox = new JComboBox<>(); + salasComboBox = new JComboBox<>(); + topPanel.add(peliculasComboBox); + topPanel.add(salasComboBox); + frame.add(topPanel, BorderLayout.NORTH); + + asientosPanel = new JPanel(new GridLayout(5, 5, 5, 5)); + frame.add(asientosPanel, BorderLayout.CENTER); + + cargarPeliculas(); + cargarSalas(); + + salasComboBox.addActionListener(e -> cargarAsientos()); + + frame.setVisible(true); + } + + private void cargarPeliculas() { + try (Connection connection = DatabaseConnection.getConnection()) { + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT nombre FROM peliculas"); + while (resultSet.next()) { + peliculasComboBox.addItem(resultSet.getString("nombre")); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void cargarSalas() { + try (Connection connection = DatabaseConnection.getConnection()) { + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT nombre FROM salas"); + while (resultSet.next()) { + salasComboBox.addItem(resultSet.getString("nombre")); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void cargarAsientos() { + asientosPanel.removeAll(); + asientosBotones = new HashMap<>(); + + String salaSeleccionada = (String) salasComboBox.getSelectedItem(); + if (salaSeleccionada != null) { + try (Connection connection = DatabaseConnection.getConnection()) { + int idSala = obtenerIdSala(connection, salaSeleccionada); + String query = "SELECT id_asiento, numero_asiento, estado FROM asientos WHERE id_sala = ?"; + try (PreparedStatement statement = connection.prepareStatement(query)) { + statement.setInt(1, idSala); + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + int idAsiento = resultSet.getInt("id_asiento"); + int numeroAsiento = resultSet.getInt("numero_asiento"); + boolean disponible = "disponible".equals(resultSet.getString("estado")); + + JButton asientoButton = new JButton(String.valueOf(numeroAsiento)); + asientoButton.setEnabled(disponible); + asientoButton.addActionListener(e -> comprarBoleto(idAsiento, numeroAsiento)); + + asientosBotones.put(idAsiento, asientoButton); + asientosPanel.add(asientoButton); + } + } + } + } catch (SQLException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(frame, "Error al cargar los asientos: " + e.getMessage()); + } + } + asientosPanel.revalidate(); + asientosPanel.repaint(); + } + + private void comprarBoleto(int idAsiento, int numeroAsiento) { + String peliculaSeleccionada = (String) peliculasComboBox.getSelectedItem(); + String salaSeleccionada = (String) salasComboBox.getSelectedItem(); + + if (peliculaSeleccionada != null && salaSeleccionada != null) { + try (Connection connection = DatabaseConnection.getConnection()) { + int idPelicula = obtenerIdPelicula(connection, peliculaSeleccionada); + + String query = "INSERT INTO ventas_boletos (id_asiento, id_pelicula) VALUES (?, ?)"; + try (PreparedStatement statement = connection.prepareStatement(query)) { + statement.setInt(1, idAsiento); + statement.setInt(2, idPelicula); + statement.executeUpdate(); + } + + String updateQuery = "UPDATE asientos SET estado = 'vendido' WHERE id_asiento = ?"; + try (PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) { + updateStatement.setInt(1, idAsiento); + updateStatement.executeUpdate(); + } + + JOptionPane.showMessageDialog(frame, "Boleto comprado para " + peliculaSeleccionada + + " en " + salaSeleccionada + " (Asiento: " + numeroAsiento + ")"); + + JButton asientoButton = asientosBotones.get(idAsiento); + if (asientoButton != null) { + asientoButton.setEnabled(false); + } + } catch (SQLException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(frame, "Error al comprar el boleto: " + e.getMessage()); + } + } else { + JOptionPane.showMessageDialog(frame, "Por favor, selecciona una película y sala."); + } + } + + private int obtenerIdPelicula(Connection connection, String nombrePelicula) throws SQLException { + String query = "SELECT id_pelicula FROM peliculas WHERE nombre = ?"; + try (PreparedStatement statement = connection.prepareStatement(query)) { + statement.setString(1, nombrePelicula); + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getInt("id_pelicula"); + } else { + throw new SQLException("No se encontró la película: " + nombrePelicula); + } + } + } + } + + private int obtenerIdSala(Connection connection, String nombreSala) throws SQLException { + String query = "SELECT id_sala FROM salas WHERE nombre = ?"; + try (PreparedStatement statement = connection.prepareStatement(query)) { + statement.setString(1, nombreSala); + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getInt("id_sala"); + } else { + throw new SQLException("No se encontró la sala: " + nombreSala); + } + } + } + } +} diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/DatabaseConnection.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/DatabaseConnection.java new file mode 100644 index 0000000..d7dbf69 --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/DatabaseConnection.java @@ -0,0 +1,15 @@ +package com.mycompany.ticketcine; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DatabaseConnection { + private static final String URL = "jdbc:mysql://localhost:3306/cine"; + private static final String USER = "root"; + private static final String PASSWORD = "qwerty"; + + public static Connection getConnection() throws SQLException { + return DriverManager.getConnection(URL, USER, PASSWORD); + } +} diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/MainApp.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/MainApp.java new file mode 100644 index 0000000..08d9d20 --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/MainApp.java @@ -0,0 +1,12 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.ticketcine; + +public class MainApp { + public static void main(String[] args) { + // Inicializa la aplicación de compra de boletos + new CompraBoletosApp(); + } +} diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Pelicula.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Pelicula.java new file mode 100644 index 0000000..e445b09 --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Pelicula.java @@ -0,0 +1,49 @@ +package com.mycompany.ticketcine; + +public class Pelicula { + private int id; + private String nombre; + private int duracion; + private String genero; + + // Constructor + public Pelicula(int id, String nombre, int duracion, String genero) { + this.id = id; + this.nombre = nombre; + this.duracion = duracion; + this.genero = genero; + } + + // Getters y Setters + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public int getDuracion() { + return duracion; + } + + public void setDuracion(int duracion) { + this.duracion = duracion; + } + + public String getGenero() { + return genero; + } + + public void setGenero(String genero) { + this.genero = genero; + } +} diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Sala.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Sala.java new file mode 100644 index 0000000..6a50738 --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/Sala.java @@ -0,0 +1,43 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.ticketcine; + +public class Sala { + private int id; + private String nombre; + private int capacidad; + + // Constructor + public Sala(int id, String nombre, int capacidad) { + this.id = id; + this.nombre = nombre; + this.capacidad = capacidad; + } + + // Getters y Setters + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public int getCapacidad() { + return capacidad; + } + + public void setCapacidad(int capacidad) { + this.capacidad = capacidad; + } +} diff --git a/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/VentaBoleto.java b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/VentaBoleto.java new file mode 100644 index 0000000..1922484 --- /dev/null +++ b/NetBeansProjects/ticketCine/src/main/java/com/mycompany/ticketcine/VentaBoleto.java @@ -0,0 +1,53 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.mycompany.ticketcine; + +public class VentaBoleto { + private int id; + private int idAsiento; + private int idPelicula; + private String fechaVenta; + + // Constructor + public VentaBoleto(int id, int idAsiento, int idPelicula, String fechaVenta) { + this.id = id; + this.idAsiento = idAsiento; + this.idPelicula = idPelicula; + this.fechaVenta = fechaVenta; + } + + // Getters y Setters + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getIdAsiento() { + return idAsiento; + } + + public void setIdAsiento(int idAsiento) { + this.idAsiento = idAsiento; + } + + public int getIdPelicula() { + return idPelicula; + } + + public void setIdPelicula(int idPelicula) { + this.idPelicula = idPelicula; + } + + public String getFechaVenta() { + return fechaVenta; + } + + public void setFechaVenta(String fechaVenta) { + this.fechaVenta = fechaVenta; + } +}