Skip to content

Commit

Permalink
Merge pull request #67 from CodeSystem2022/David
Browse files Browse the repository at this point in the history
Completamos la clase de Java Avanzado.
  • Loading branch information
David-Esteche authored Oct 23, 2023
2 parents dbe4675 + 2b6405c commit d4b6246
Show file tree
Hide file tree
Showing 2 changed files with 302 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="utn.tienda_libros.vista.LibroFrom">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="677" height="324"/>
<xy x="20" y="20" width="773" height="324"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="59ae0" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="28" style="1"/>
<text value="Tienda de Libros"/>
</properties>
</component>
<vspacer id="d6073">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<scrollpane id="ae6fc">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="2" row-span="1" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<properties>
<background color="-15781868"/>
</properties>
<border type="none"/>
<children>
<component id="d61be" class="javax.swing.JTable" binding="tablaLibros" custom-create="true">
Expand All @@ -35,6 +32,175 @@
</component>
</children>
</scrollpane>
<vspacer id="d6073">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="3ebce" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b5e0c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="cf687" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Libro"/>
</properties>
</component>
<component id="9e0a3" class="javax.swing.JTextField" binding="libroTexto">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="adf31" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="af0ff" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Autor"/>
</properties>
</component>
<component id="f4e64" class="javax.swing.JTextField" binding="autorTexto">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="dc8cf" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5a7b3" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Precio"/>
</properties>
</component>
<component id="ef1d3" class="javax.swing.JTextField" binding="precioTexto">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="12346" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7863e" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Existencias"/>
</properties>
</component>
<component id="6a81c" class="javax.swing.JTextField" binding="existenciasTexto">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<hspacer id="e1ecb">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="20ed7" class="javax.swing.JButton" binding="modificarButton" default-binding="true">
<constraints>
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Modificar"/>
</properties>
</component>
<vspacer id="1c1f3">
<constraints>
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<hspacer id="2a01d">
<constraints>
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="ab745" class="javax.swing.JButton" binding="eliminarButton" default-binding="true">
<constraints>
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Eliminar"/>
</properties>
</component>
<component id="20c2b" class="javax.swing.JButton" binding="agregarButton" default-binding="true">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Agregar"/>
</properties>
</component>
<vspacer id="cb785">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,48 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import utn.tienda_libros.modelo.Libro;
import utn.tienda_libros.servicio.LibroServicio;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Component
public class LibroFrom extends JFrame {
LibroServicio libroServicio;
private JPanel panel;
private JTable tablaLibros;
private JTextField idTexto;
private JTextField libroTexto;
private JTextField autorTexto;
private JTextField precioTexto;
private JTextField existenciasTexto;
private JButton agregarButton;
private JButton modificarButton;
private JButton eliminarButton;
private DefaultTableModel tablaModeloLibros;


@Autowired
public LibroFrom(LibroServicio libroServicio){
this.libroServicio = libroServicio;
iniciarForma();
agregarButton.addActionListener(e -> agregarLibro());
tablaLibros.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
cargarLibroSeleccionado();
}

});
modificarButton.addActionListener(e -> modificarLibro());
eliminarButton.addActionListener(e -> eliminarLibro());
}

private void iniciarForma(){
Expand All @@ -35,12 +59,113 @@ private void iniciarForma(){
setLocation(x, y);
}

private void agregarLibro(){
//Leer los valores del formulario
if(libroTexto.getText().equals("")){
mostrarMensaje("Ingresa el nombre del libro");
libroTexto.requestFocusInWindow();
return;
}
var nombreLibro = libroTexto.getText();
var autor = autorTexto.getText();
var precio = Double.parseDouble(precioTexto.getText());
var existencias = Integer.parseInt(existenciasTexto.getText());
//Creamos el objeto
var libro = new Libro(null, nombreLibro, autor, precio, existencias);
//libro.setNombreLibro(nombreLibro);
//libro.setAutor(autor);
//libro.setPrecio(precio);
//libro.setExistencias(existencias);
this.libroServicio.guardarLibro(libro);
mostrarMensaje("Se agrego el libro...");
limparFormulario();
listarLibros();
}

private void cargarLibroSeleccionado(){
//los indices de la columna inician en cero.
var renglon = tablaLibros.getSelectedRow();
if(renglon != -1){
String idLibro = tablaLibros.getModel().getValueAt(renglon, 0).toString();
idTexto.setText(idLibro);
String nombreLibro = tablaLibros.getModel().getValueAt(renglon, 1).toString();
libroTexto.setText(nombreLibro);
String autor = tablaLibros.getModel().getValueAt(renglon, 2).toString();
autorTexto.setText(autor);
String precio = tablaLibros.getModel().getValueAt(renglon, 3).toString();
precioTexto.setText(precio);
String existencias = tablaLibros.getModel().getValueAt(renglon, 4).toString();
existenciasTexto.setText(existencias);
}
}

private void modificarLibro(){
if(this.idTexto.equals("")){
mostrarMensaje("Debes selecionar un registro en la tabla");
}
else {
//verificamos que nombre del libro no sea nulo
if (libroTexto.getText().equals("")){
mostrarMensaje("Digite el nombre del libro..");
libroTexto.requestFocusInWindow();
return;
}
//Llenamos el objeto libro a actualizar.
int idLibro = Integer.parseInt(idTexto.getText());
var nombreLibro = libroTexto.getText();
var autor = autorTexto.getText();
var precio = Double.parseDouble(precioTexto.getText());
var existencias = Integer.parseInt(existenciasTexto.getText());
var libro = new Libro(idLibro, nombreLibro, autor, precio, existencias);
libroServicio.guardarLibro(libro);
mostrarMensaje("Se modifico el libro...");
limparFormulario();
listarLibros();
}
}

private void eliminarLibro(){
var renglon = tablaLibros.getSelectedRow();
if(renglon != -1){
String idLibro = tablaLibros.getModel().getValueAt(renglon, 0).toString();
var libro = new Libro();
libro.setIdLibro(Integer.parseInt(idLibro));
libroServicio.eliminarLibro(libro);
mostrarMensaje("Libro "+idLibro+" ELIMINADO");
limparFormulario();
listarLibros();
}
else {
mostrarMensaje("No se ha seleccionado ningun libro a eliminar");
}
}

private void limparFormulario(){
libroTexto.setText("");
autorTexto.setText("");
precioTexto.setText("");
existenciasTexto.setText("");
}

private void mostrarMensaje(String mensaje){
JOptionPane.showMessageDialog(this, mensaje);
}

private void createUIComponents() {
this.tablaModeloLibros = new DefaultTableModel(0, 5);
idTexto = new JTextField("");
idTexto.setVisible(false);
this.tablaModeloLibros = new DefaultTableModel(0, 5){
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
String[] cabezera = {"Id", "Libro", "Autor", "Precio", "Existencias"};
this.tablaModeloLibros.setColumnIdentifiers(cabezera);
//Instancias el objeto table
this.tablaLibros = new JTable(tablaModeloLibros);
//Evitamos que se seleccione los registros.
tablaLibros.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listarLibros();
}

Expand Down

0 comments on commit d4b6246

Please sign in to comment.