mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(examen 9-5-25): terminado
This commit is contained in:
@@ -186,6 +186,92 @@ public class ControladorConcierto {
|
|||||||
}
|
}
|
||||||
return lista;
|
return lista;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static Object[][] getTotalesFestivalesPorCiudad() {
|
||||||
|
List<Object[]> list = new ArrayList<Object[]>();
|
||||||
|
try {
|
||||||
|
Connection conn = ConnectionManager.getConnection();
|
||||||
|
Statement s = conn.createStatement();
|
||||||
|
ResultSet rs = s.executeQuery("select sala.ciudad as ciudad, "
|
||||||
|
+ "count(concierto.id) as festivales "
|
||||||
|
+ "from concierto inner join sala "
|
||||||
|
+ "on (concierto.sala_id = sala.id) "
|
||||||
|
+ "where concierto.es_festival = 1 "
|
||||||
|
+ "group by sala.ciudad;");
|
||||||
|
while (rs.next()) {
|
||||||
|
Object fila[] = new Object[2];
|
||||||
|
fila[0] = rs.getString("ciudad");
|
||||||
|
fila[1] = rs.getString("festivales");
|
||||||
|
list.add(fila);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convierto la lista en una matriz
|
||||||
|
Object m[][] = new Object[list.size()][2];
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
m[i][0] = list.get(i)[0];
|
||||||
|
m[i][1] = list.get(i)[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static float getPrecioMedioConciertos() {
|
||||||
|
try {
|
||||||
|
Connection conn = ConnectionManager.getConnection();
|
||||||
|
Statement s = conn.createStatement();
|
||||||
|
ResultSet rs =
|
||||||
|
s.executeQuery("select avg(precio) from concierto");
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getFloat(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getGrupoConMasConciertos() {
|
||||||
|
try {
|
||||||
|
Connection conn = ConnectionManager.getConnection();
|
||||||
|
Statement s = conn.createStatement();
|
||||||
|
ResultSet rs =
|
||||||
|
s.executeQuery("select count(concierto.id) as total, "
|
||||||
|
+ "grupo.nombre "
|
||||||
|
+ "from concierto inner join grupo "
|
||||||
|
+ "on concierto.grupo_id = grupo.id "
|
||||||
|
+ "group by grupo.nombre "
|
||||||
|
+ "order by total desc limit 1");
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getString(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return "Error al obtener el grup con más conciertos";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -198,3 +284,7 @@ public class ControladorConcierto {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package tutorialJava.examenes.examen20250509.vista;
|
||||||
|
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.awt.Font;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JTable;
|
||||||
|
|
||||||
|
import tutorialJava.examenes.examen20250509.controlador.ControladorConcierto;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
|
public class PanelEstadisticas extends JPanel {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private JTable jTableDatos;
|
||||||
|
private JLabel lblPrecioMedio;
|
||||||
|
JLabel lblGrupoConMasConciertos;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the panel.
|
||||||
|
*/
|
||||||
|
public PanelEstadisticas() {
|
||||||
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||||
|
gridBagLayout.columnWidths = new int[]{0, 0, 0};
|
||||||
|
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
|
||||||
|
gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
|
||||||
|
gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
|
||||||
|
setLayout(gridBagLayout);
|
||||||
|
|
||||||
|
JLabel lblNewLabel = new JLabel("Estadísticas");
|
||||||
|
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
|
||||||
|
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel.gridwidth = 2;
|
||||||
|
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblNewLabel.gridx = 0;
|
||||||
|
gbc_lblNewLabel.gridy = 0;
|
||||||
|
add(lblNewLabel, gbc_lblNewLabel);
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane();
|
||||||
|
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
|
||||||
|
gbc_scrollPane.gridwidth = 2;
|
||||||
|
gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_scrollPane.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_scrollPane.gridx = 0;
|
||||||
|
gbc_scrollPane.gridy = 1;
|
||||||
|
add(scrollPane, gbc_scrollPane);
|
||||||
|
|
||||||
|
jTableDatos = new JTable(getMatrizDeDatos(), getTitulosMatriz());
|
||||||
|
scrollPane.setViewportView(jTableDatos);
|
||||||
|
|
||||||
|
JButton btnNewButton_1 = new JButton("Calcular precio medio");
|
||||||
|
btnNewButton_1.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
lblPrecioMedio.setText("" +
|
||||||
|
ControladorConcierto.getPrecioMedioConciertos());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
|
||||||
|
gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_btnNewButton_1.gridx = 0;
|
||||||
|
gbc_btnNewButton_1.gridy = 2;
|
||||||
|
add(btnNewButton_1, gbc_btnNewButton_1);
|
||||||
|
|
||||||
|
lblPrecioMedio = new JLabel("New label");
|
||||||
|
GridBagConstraints gbc_lblPrecioMedio = new GridBagConstraints();
|
||||||
|
gbc_lblPrecioMedio.insets = new Insets(0, 0, 5, 0);
|
||||||
|
gbc_lblPrecioMedio.gridx = 1;
|
||||||
|
gbc_lblPrecioMedio.gridy = 2;
|
||||||
|
add(lblPrecioMedio, gbc_lblPrecioMedio);
|
||||||
|
|
||||||
|
JButton btnNewButton = new JButton("Grupo con más conciertos");
|
||||||
|
btnNewButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
lblGrupoConMasConciertos.setText(
|
||||||
|
ControladorConcierto.getGrupoConMasConciertos());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
|
||||||
|
gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_btnNewButton.gridx = 0;
|
||||||
|
gbc_btnNewButton.gridy = 3;
|
||||||
|
add(btnNewButton, gbc_btnNewButton);
|
||||||
|
|
||||||
|
lblGrupoConMasConciertos = new JLabel("New label");
|
||||||
|
GridBagConstraints gbc_lblGrupoConMasConciertos = new GridBagConstraints();
|
||||||
|
gbc_lblGrupoConMasConciertos.gridx = 1;
|
||||||
|
gbc_lblGrupoConMasConciertos.gridy = 3;
|
||||||
|
add(lblGrupoConMasConciertos, gbc_lblGrupoConMasConciertos);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private Object[][] getMatrizDeDatos() {
|
||||||
|
// String matriz[][] = new String[2][2];
|
||||||
|
// matriz[0][0] = "Rafa";
|
||||||
|
// matriz[0][1] = "Ismael";
|
||||||
|
// matriz[1][0] = "Rubén";
|
||||||
|
// matriz[1][1] = "Mª Jesús";
|
||||||
|
return ControladorConcierto.getTotalesFestivalesPorCiudad();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String[] getTitulosMatriz() {
|
||||||
|
return new String[] {"Ciudad", "Número de festivales"};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ public class PanelFiltradoConciertos extends JPanel {
|
|||||||
JComboBox<Sala> jcbSalas;
|
JComboBox<Sala> jcbSalas;
|
||||||
DefaultListModel<String> dlmConciertos =
|
DefaultListModel<String> dlmConciertos =
|
||||||
new DefaultListModel<String>();
|
new DefaultListModel<String>();
|
||||||
|
JList jlistConciertos;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the panel.
|
* Create the panel.
|
||||||
@@ -106,10 +107,15 @@ public class PanelFiltradoConciertos extends JPanel {
|
|||||||
gbc_scrollPane.gridy = 3;
|
gbc_scrollPane.gridy = 3;
|
||||||
add(scrollPane, gbc_scrollPane);
|
add(scrollPane, gbc_scrollPane);
|
||||||
|
|
||||||
JList jlistConciertos = new JList(dlmConciertos);
|
jlistConciertos = new JList(dlmConciertos);
|
||||||
scrollPane.setViewportView(jlistConciertos);
|
scrollPane.setViewportView(jlistConciertos);
|
||||||
|
|
||||||
JButton btnEliminar = new JButton("Eliminar");
|
JButton btnEliminar = new JButton("Eliminar");
|
||||||
|
btnEliminar.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
eliminarSeleccionados();
|
||||||
|
}
|
||||||
|
});
|
||||||
GridBagConstraints gbc_btnEliminar = new GridBagConstraints();
|
GridBagConstraints gbc_btnEliminar = new GridBagConstraints();
|
||||||
gbc_btnEliminar.anchor = GridBagConstraints.SOUTH;
|
gbc_btnEliminar.anchor = GridBagConstraints.SOUTH;
|
||||||
gbc_btnEliminar.insets = new Insets(0, 0, 5, 0);
|
gbc_btnEliminar.insets = new Insets(0, 0, 5, 0);
|
||||||
@@ -166,6 +172,17 @@ public class PanelFiltradoConciertos extends JPanel {
|
|||||||
dlmConciertos.removeAllElements();
|
dlmConciertos.removeAllElements();
|
||||||
dlmConciertos.addAll(conciertosFiltrados);
|
dlmConciertos.addAll(conciertosFiltrados);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void eliminarSeleccionados() {
|
||||||
|
int selectedIndices[] = this.jlistConciertos.getSelectedIndices();
|
||||||
|
for (int i = selectedIndices.length - 1; i >= 0; i--) {
|
||||||
|
System.out.println("Selected index: " + selectedIndices[i]);
|
||||||
|
dlmConciertos.removeElementAt(selectedIndices[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ public class VentanaPrincipal extends JFrame {
|
|||||||
PanelFiltradoConciertos panel2 = new PanelFiltradoConciertos();
|
PanelFiltradoConciertos panel2 = new PanelFiltradoConciertos();
|
||||||
tabbedPane.addTab("Filtrado de conciertos", null, panel2, null);
|
tabbedPane.addTab("Filtrado de conciertos", null, panel2, null);
|
||||||
|
|
||||||
JPanel panel3 = new JPanel();
|
PanelEstadisticas panel3 = new PanelEstadisticas();
|
||||||
tabbedPane.addTab("New tab", null, panel3, null);
|
tabbedPane.addTab("Estadísticas", null, panel3, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user