avances semana 1

This commit is contained in:
RictalLime 2025-02-27 11:18:31 -06:00
parent 727d7ad123
commit 737a71134d
9 changed files with 286 additions and 0 deletions

6
AdminTicket/MVC/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"c:\\Users\\Tron7\\Documents\\DesarrolloSoftware\\VentaBoletos\\AdminTicket\\MVC\\modelo\\mysql-connector-j-8.3.0.jar"
]
}

13
AdminTicket/MVC/Main.java Normal file
View File

@ -0,0 +1,13 @@
import modelo.Database;
import vista.VentaBoletosView;
import controlador.VentaBoletosController;
public class Main {
public static void main(String[] args) {
Database database = new Database();
VentaBoletosView vista = new VentaBoletosView();
VentaBoletosController controlador = new VentaBoletosController(vista, database);
vista.setVisible(true);
}
}

View File

@ -0,0 +1,107 @@
package controlador;
import modelo.*;
import vista.VentaBoletosView;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class VentaBoletosController {
private VentaBoletosView vista;
private Database database;
private List<JButton> botonesAsientos;
public VentaBoletosController(VentaBoletosView vista, Database database) {
this.vista = vista;
this.database = database;
this.botonesAsientos = new ArrayList<>();
cargarAsientos();
agregarListeners();
}
private void cargarAsientos() {
try {
Connection conexion = database.getConexion();
Statement stmt = conexion.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM asientos");
while (rs.next()) {
int numeroAsiento = rs.getInt("numero_asiento");
boolean disponible = rs.getBoolean("disponible");
JButton btnAsiento = new JButton(String.valueOf(numeroAsiento));
btnAsiento.setEnabled(disponible);
btnAsiento.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
venderAsiento(numeroAsiento);
}
});
botonesAsientos.add(btnAsiento);
vista.agregarBotonAsiento(btnAsiento);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void venderAsiento(int numeroAsiento) {
try {
Connection conexion = database.getConexion();
PreparedStatement pstmt = conexion.prepareStatement("UPDATE asientos SET disponible = FALSE WHERE numero_asiento = ?");
pstmt.setInt(1, numeroAsiento);
pstmt.executeUpdate();
pstmt = conexion.prepareStatement("INSERT INTO ventas (fecha_venta, numero_asiento, precio) VALUES (NOW(), ?, ?)");
pstmt.setInt(1, numeroAsiento);
pstmt.setDouble(2, 100.00); // Precio fijo por simplicidad
pstmt.executeUpdate();
vista.mostrarMensaje("Boleto vendido: Asiento " + numeroAsiento);
actualizarBotones();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void actualizarBotones() {
for (JButton btn : botonesAsientos) {
btn.setEnabled(false);
}
vista.actualizarBotones();
}
private void generarReporte() {
try {
Connection conexion = database.getConexion();
Statement stmt = conexion.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM ventas");
StringBuilder reporte = new StringBuilder("Reporte de Ventas:\n");
while (rs.next()) {
reporte.append("Fecha: ").append(rs.getTimestamp("fecha_venta")).append(", ");
reporte.append("Asiento: ").append(rs.getInt("numero_asiento")).append(", ");
reporte.append("Precio: $").append(rs.getDouble("precio")).append("\n");
}
vista.mostrarMensaje(reporte.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
private void agregarListeners() {
vista.getBtnGenerarReporte().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generarReporte();
}
});
}
}

View File

@ -0,0 +1,25 @@
package modelo;
public class Asiento {
private int id;
private int numeroAsiento;
private boolean disponible;
public Asiento(int id, int numeroAsiento, boolean disponible) {
this.id = id;
this.numeroAsiento = numeroAsiento;
this.disponible = disponible;
}
public int getNumeroAsiento() {
return numeroAsiento;
}
public boolean isDisponible() {
return disponible;
}
public void setDisponible(boolean disponible) {
this.disponible = disponible;
}
}

View File

@ -0,0 +1,35 @@
package modelo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
private Connection conexion;
public Database() {
conectar();
}
private void conectar() {
try {
conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/venta_boletos", "root", "4560");
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConexion() {
return conexion;
}
public void cerrarConexion() {
try {
if (conexion != null) {
conexion.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,29 @@
package modelo;
import java.sql.Timestamp;
public class Venta {
private int id;
private Timestamp fechaVenta;
private int numeroAsiento;
private double precio;
public Venta(int id, Timestamp fechaVenta, int numeroAsiento, double precio) {
this.id = id;
this.fechaVenta = fechaVenta;
this.numeroAsiento = numeroAsiento;
this.precio = precio;
}
public Timestamp getFechaVenta() {
return fechaVenta;
}
public int getNumeroAsiento() {
return numeroAsiento;
}
public double getPrecio() {
return precio;
}
}

Binary file not shown.

View File

@ -0,0 +1,43 @@
package vista;
import java.awt.*;
import javax.swing.*;
public class VentaBoletosView extends JFrame {
private JPanel panelAsientos;
private JButton btnGenerarReporte;
public VentaBoletosView() {
super("Venta de Boletos");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
panelAsientos = new JPanel(new GridLayout(10, 10));
btnGenerarReporte = new JButton("Generar Reporte de Ventas");
add(panelAsientos, BorderLayout.CENTER);
add(btnGenerarReporte, BorderLayout.SOUTH);
}
public JPanel getPanelAsientos() {
return panelAsientos;
}
public JButton getBtnGenerarReporte() {
return btnGenerarReporte;
}
public void agregarBotonAsiento(JButton boton) {
panelAsientos.add(boton);
}
public void mostrarMensaje(String mensaje) {
JOptionPane.showMessageDialog(this, mensaje);
}
public void actualizarBotones() {
panelAsientos.revalidate();
panelAsientos.repaint();
}
}

28
AdminTicket/bd.sql Normal file
View File

@ -0,0 +1,28 @@
CREATE DATABASE venta_boletos;
USE venta_boletos;
CREATE TABLE asientos (
id INT AUTO_INCREMENT PRIMARY KEY,
numero_asiento INT NOT NULL,
disponible BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE TABLE ventas (
id INT AUTO_INCREMENT PRIMARY KEY,
fecha_venta DATETIME NOT NULL,
numero_asiento INT NOT NULL,
precio DECIMAL(10, 2) NOT NULL
);
INSERT INTO asientos (numero_asiento, disponible) VALUES
(101, true),
(102, false),
(103, true),
(104, true),
(105, false),
(106, true),
(107, true),
(108, false),
(109, true),
(110, true);