mezontleTeam/SeleccionarAsientoV.java

272 lines
8.9 KiB
Java
Raw Permalink Normal View History

2024-03-11 01:43:27 +00:00
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
2024-03-20 00:57:51 +00:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
2024-03-11 01:43:27 +00:00
public class SeleccionarAsientoV extends JPanel {
2024-03-11 01:43:27 +00:00
JPanel panel;
2024-03-11 01:43:27 +00:00
private JPanel pSuperior = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JButton bRegresar = new JButton("Regresar.");
2024-03-11 02:31:19 +00:00
private int noAsientosSeleccionados = 0;
2024-03-20 02:04:31 +00:00
DefaultComboBoxModel salas = new DefaultComboBoxModel();
JComboBox cbSalas = new JComboBox(salas);
2024-03-11 01:43:27 +00:00
private JLabel lSeleccionar = new JLabel("Asientos seleccionados: " + noAsientosSeleccionados);
2024-03-20 00:57:51 +00:00
private JLabel lFuncion = new JLabel("Función: XXX");
2024-03-11 01:43:27 +00:00
private JPanel pInferior = new JPanel(new FlowLayout(FlowLayout.CENTER));
private int total = 0;
private JLabel lTotal = new JLabel("Total: " + total + "$");
private JButton bSeleccionar = new JButton("Seleccionar");
private JPanel pSala = new JPanel(new GridBagLayout());
2024-03-11 02:31:19 +00:00
private JToggleButton [][]bAsientos;
private Asiento [][] asientos;
List<Asiento> asientosSeleccionados = new ArrayList<Asiento>();
2024-03-11 01:43:27 +00:00
2024-03-20 00:57:51 +00:00
public SeleccionarAsientoV() throws HeadlessException {
this.setLayout(new BorderLayout());
2024-03-20 05:12:29 +00:00
//llenarAsientos();
obtenerAsientos(1);
2024-03-20 00:57:51 +00:00
agregarComponentes();
crearCeldas(7,10);
}
2024-03-11 01:43:27 +00:00
public void agregarComponentes(){
pSuperior.add(this.bRegresar);
pSuperior.add(lSeleccionar);
2024-03-20 02:04:31 +00:00
pSuperior.add(this.cbSalas);
this.llenarSalas();
2024-03-20 05:12:29 +00:00
cbSalas.addItemListener(new GestiónBotónSeleccionarSala(this));
2024-03-11 01:43:27 +00:00
pInferior.add(this.lTotal);
pInferior.add(this.bSeleccionar);
this.add(pSuperior,BorderLayout.NORTH);
this.add(pInferior,BorderLayout.SOUTH);
}
public void crearCeldas(int parX, int parY){
GridBagConstraints con = new GridBagConstraints();
2024-03-11 02:31:19 +00:00
bAsientos = new JToggleButton[parX][parY];
2024-03-11 01:43:27 +00:00
String zona = "";
JToggleButton tfAux;
for(int i = 0; i < parX; i++){
for(int j = 0; j < parY; j ++){
tfAux = new JToggleButton("");
if( ( (j >= 2) && (j <= 7) ) && ( (i >= 2) && (i <= 6) ) ){
tfAux.setBackground(Color.PINK);
zona = "A";
2024-03-11 02:31:19 +00:00
tfAux.setText("200$");
2024-03-11 01:43:27 +00:00
}
if( ((i == 1) && (j > 0) && (j < 9)) || ((j == 1) && (i > 0)) || ((j == 8) && (i > 0)) ){
tfAux.setBackground(Color.ORANGE);
zona = "B";
2024-03-11 02:31:19 +00:00
tfAux.setText("130$");
2024-03-11 01:43:27 +00:00
}
if( (i == 0) || (j == 0) || (j == 9)){
tfAux.setBackground(Color.YELLOW);
zona = "C";
2024-03-11 02:31:19 +00:00
tfAux.setText("65$");
2024-03-11 01:43:27 +00:00
}
2024-03-11 03:12:56 +00:00
if(asientos[i][j].isDisponibilidad()){
tfAux.setBackground(Color.DARK_GRAY);
tfAux.setText("ND");
}
2024-03-11 01:43:27 +00:00
tfAux.setSize(100,100);
//tfAux.addKeyListener(new GestiónTecladoEscribirCaracter(this,i,j));
tfAux.addActionListener(new GestiónBotónSeleccionarAsiento(new Asiento(i,j,zona,asientos[i][j].isDisponibilidad()),this));
2024-03-11 01:43:27 +00:00
con.gridx = j;
con.gridy = i;
con.gridheight = 1;
con.gridwidth = 1;
2024-03-11 02:31:19 +00:00
System.out.println(i+" "+j+ " "+bAsientos.length);
bAsientos[i][j] = tfAux;
pSala.add(bAsientos[i][j],con);
2024-03-11 01:43:27 +00:00
}
}
pSala.setBounds(0,0,700,700);
this.add(pSala,BorderLayout.CENTER);
}
2024-03-20 05:12:29 +00:00
public void actualizarCeldas(){
String zona;
for(int i = 0; i < 7; i++){
for(int j = 0; j < 10; j ++){
if( ( (j >= 2) && (j <= 7) ) && ( (i >= 2) && (i <= 6) ) ){
bAsientos[i][j].setBackground(Color.PINK);
zona = "A";
bAsientos[i][j].setText("200$");
}
if( ((i == 1) && (j > 0) && (j < 9)) || ((j == 1) && (i > 0)) || ((j == 8) && (i > 0)) ){
bAsientos[i][j].setBackground(Color.ORANGE);
zona = "B";
bAsientos[i][j].setText("130$");
}
if( (i == 0) || (j == 0) || (j == 9)){
bAsientos[i][j].setBackground(Color.YELLOW);
zona = "C";
bAsientos[i][j].setText("65$");
}
if(asientos[i][j].isDisponibilidad()){
bAsientos[i][j].setBackground(Color.DARK_GRAY);
bAsientos[i][j].setText("ND");
}
}
}
}
2024-03-20 00:57:51 +00:00
public void llenarAsientos(){
asientos = new Asiento[7][10];
String zona = "";
for(int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if( ( (j >= 2) && (j <= 7) ) && ( (i >= 2) && (i <= 6) ) ){
zona = "A";
}
if( ((i == 1) && (j > 0) && (j < 9)) || ((j == 1) && (i > 0)) || ((j == 8) && (i > 0)) ){
zona = "B";
}
if( (i == 0) || (j == 0) || (j == 9)){
zona = "C";
}
asientos[i][j]=new Asiento(i,j,zona,false);
}
}
}
2024-03-11 01:43:27 +00:00
public void seleccionarAsientos(Asiento asiento){
int precio = 0;
int x = asiento.getX(), y = asiento.getY();
String zona = asiento.getZona();
switch (zona){
case "A":
precio = 200;
break;
case "B":
precio = 130;
break;
case "C":
precio = 65;
break;
}
2024-03-11 03:12:56 +00:00
if(!bAsientos[x][y].getText().equals("ND")) {
if (bAsientos[x][y].isSelected()) {
noAsientosSeleccionados++;
total += precio;
asientosSeleccionados.add(asiento);
2024-03-11 03:12:56 +00:00
} else {
noAsientosSeleccionados--;
total -= precio;
for(int a = 0; a < asientosSeleccionados.size() ;a++){
if(asientosSeleccionados.get(a).equals(asiento)){
asientosSeleccionados.remove(a);
}
}
2024-03-11 03:12:56 +00:00
}
2024-03-11 01:43:27 +00:00
}else{
2024-03-11 03:12:56 +00:00
bAsientos[x][y].setSelected(false);
2024-03-11 01:43:27 +00:00
}
System.out.print("[");
for(int a = 0; a < asientosSeleccionados.size() ;a++){
System.out.print(asientosSeleccionados.get(a).getX() + " " + asientosSeleccionados.get(a).getY() + ", " );
}
System.out.println("]");
2024-03-11 01:43:27 +00:00
lSeleccionar.setText("Asientos seleccionados: " + noAsientosSeleccionados);
lTotal.setText("Total: " + total + "$");
}
2024-03-20 00:57:51 +00:00
2024-03-20 05:12:29 +00:00
public void obtenerAsientos(int salaSeleccionada){
Connection conexion = null;
2024-03-20 00:57:51 +00:00
ResultSet rs;
Statement st;
2024-03-20 05:12:29 +00:00
String consulta = "SELECT disponibilidad, ubicacion, idAsiento FROM asientos WHERE fksala = "+salaSeleccionada;
asientos = new Asiento[7][10];
String zona = "";
try {
conexion = ConexionBD.getConnection();
st = conexion.createStatement();
rs = st.executeQuery(consulta);
while(rs.next()){
String isDisponible = String.valueOf(rs.getShort("disponibilidad"));
boolean disponibilidad = false;
if(isDisponible.equals("0")){
disponibilidad = true;
}
int i = Character.getNumericValue(rs.getString("ubicacion").charAt(0));
int j = Character.getNumericValue(rs.getString("ubicacion").charAt(2));
int id = rs.getInt("idAsiento");
if( ( (j >= 2) && (j <= 7) ) && ( (i >= 2) && (i <= 6) ) ){
zona = "A";
}
if( ((i == 1) && (j > 0) && (j < 9)) || ((j == 1) && (i > 0)) || ((j == 8) && (i > 0)) ){
zona = "B";
}
if( (i == 0) || (j == 0) || (j == 9)){
zona = "C";
}
Asiento aux = new Asiento(id,i,j,zona,disponibilidad);
asientos[i][j] = aux;
}
conexion.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
2024-03-20 00:57:51 +00:00
}
2024-03-20 05:12:29 +00:00
public void llenarSalas() {
Connection conexion = null;
ResultSet rs;
Statement st;
String consulta = "SELECT idSala FROM sala";
try {
conexion = ConexionBD.getConnection();
st = conexion.createStatement();
rs = st.executeQuery(consulta);
while(rs.next()){
salas.addElement(rs.getInt("idSala"));
}
conexion.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
2024-03-11 01:43:27 +00:00
}