Skip to content

Commit 3f86515

Browse files
authored
Merge pull request #8 from uo287568/HU07-EntregaEnDestino
Hu07 entrega en destino
2 parents b8b3ac6 + a05aac2 commit 3f86515

21 files changed

+335
-110
lines changed

src/main/java/files/Amazon.csv

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Luis;[email protected];Calle Vel�zquez 43, 1C;Pedro;[email protected];Calle Florez Estrada 7, 4B;7.4;6.54;6.81;12.56;17.45;(Nada)
2+
Marina;[email protected];Calle La Mancha 9, 2C;Manuel;[email protected];Calle Bail�n 17, 3A;4.56;9.87;5.9;9.48;25.67;(Nada)
3+
Lucia;[email protected];Calle Valencia 12, 1B;Lara;[email protected];Calle Tineo 15, 2A;1.25;5.89;3.61;17.42;9.52;Mantener timbre 3 segundos, esta roto.
4+
Lucas;[email protected];Calle Roncal 15, 1A;Mar�a;[email protected];Calle San Ferm�n 3, 4C;18.65;2.49;7.84;13.59;32.65;(Nada)
5+
Emilio;[email protected];Calle Julio 8, 5A;Carmen;[email protected];Calle Gonzalo Suarez 9, 5B;3.56;7.13;6.42;14.8;19.06;Introducir paquete por la rejilla
6+
Mart�n;[email protected];Calle Marcelino 17, 7A;Gracia;[email protected]; Avenida La Plata 7, 6A;21.47;8.05;9.11;7.92;15.46;(Nada)

src/main/java/files/Ebay.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Juan;[email protected];Calle Ramiro I 43, 1C;Aitor;[email protected];Calle Sta Susana 7, 4B;7.4;6.54;6.81;12.56;17.45;(Nada)
2+
Fernando;[email protected];Calle Torrelavega 9, 2C;Hugo;[email protected];Calle Bernabeu 17, 3A;4.56;9.87;5.9;9.48;25.67;Dejar sobre el taburete de la puerta
3+
Celia;[email protected];Calle Manzanares 12, 1B;Gabriel;[email protected];Calle Terr�n de Vega 15, 2A;1.25;5.89;3.61;17.42;9.52;(Nada)

src/main/java/files/GrupoCovadonga.csv

-1
This file was deleted.

src/main/java/giis/demo/tkrun/CargaController.java

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ private boolean comprobarEnvio(int id, int nref) {
5353
JOptionPane.showMessageDialog(null, "La ubicación de la carga no coincide con la de la última descarga");
5454
return false;
5555
}
56+
} else if(!envio.getDireccionEmisor().equals(view.getTfUbicacion().getText())) {
57+
JOptionPane.showMessageDialog(null, "La ubicación de la primera carga no coincide con la de la recogida");
58+
return false;
5659
}
5760
return true;
5861
}

src/main/java/giis/demo/tkrun/CargaModel.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
import java.util.List;
44

55
import giis.demo.util.Database;
6-
/**
7-
* Acceso a los datos de carreras e inscripciones,
8-
* utilizado como modelo para el ejemplo de swing y para las pruebas unitarias y de interfaz de usuario.
9-
*
10-
* <br/>En los metodos de este ejemplo toda la logica de negocio se realiza mediante una unica query sql por lo que siempre
11-
* se utilizan los metodos de utilidad en la clase Database que usan apache commons-dbutils y controlan la conexion.
12-
* En caso de que en un mismo metodo se realicen diferentes queries se deberia controlar la conexion desde esta clase
13-
* (ver como ejemplo la implementacion en Database).
14-
*
15-
* <br/>Si utilizase algún otro framework para manejar la persistencia, la funcionalidad proporcionada por esta clase sería la asignada
16-
* a los Servicios, Repositorios y DAOs.
17-
*/
6+
187
public class CargaModel {
198

209
private Database db=new Database();

src/main/java/giis/demo/tkrun/CargaView.java

-6
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@ public class CargaView {
2828
private JTextField tfUbicacion;
2929
private JLabel lbFecha;
3030

31-
/**
32-
* Create the application.
33-
*/
3431
public CargaView() {
3532
initialize();
3633
}
3734

38-
/**
39-
* Initialize the contents of the frame.
40-
*/
4135
private void initialize() {
4236
frmCargaEnvios = new JFrame();
4337
frmCargaEnvios.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));

src/main/java/giis/demo/tkrun/DescargaController.java

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ private boolean comprobarEnvio(int id, int nref) {
5353
JOptionPane.showMessageDialog(null, "La ubicación de la descarga coincide con la de la última carga");
5454
return false;
5555
}
56+
} else {
57+
JOptionPane.showMessageDialog(null, "El envío aún no ha sido recogido");
58+
return false;
5659
}
5760
return true;
5861
}

src/main/java/giis/demo/tkrun/DescargaModel.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
import java.util.List;
44

55
import giis.demo.util.Database;
6-
/**
7-
* Acceso a los datos de carreras e inscripciones,
8-
* utilizado como modelo para el ejemplo de swing y para las pruebas unitarias y de interfaz de usuario.
9-
*
10-
* <br/>En los metodos de este ejemplo toda la logica de negocio se realiza mediante una unica query sql por lo que siempre
11-
* se utilizan los metodos de utilidad en la clase Database que usan apache commons-dbutils y controlan la conexion.
12-
* En caso de que en un mismo metodo se realicen diferentes queries se deberia controlar la conexion desde esta clase
13-
* (ver como ejemplo la implementacion en Database).
14-
*
15-
* <br/>Si utilizase algún otro framework para manejar la persistencia, la funcionalidad proporcionada por esta clase sería la asignada
16-
* a los Servicios, Repositorios y DAOs.
17-
*/
6+
187
public class DescargaModel {
198

209
private Database db=new Database();

src/main/java/giis/demo/tkrun/DescargaView.java

-6
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ public class DescargaView {
2525
private JTextField tfUbicacion;
2626
private JLabel lbFecha;
2727

28-
/**
29-
* Create the application.
30-
*/
3128
public DescargaView() {
3229
initialize();
3330
}
3431

35-
/**
36-
* Initialize the contents of the frame.
37-
*/
3832
private void initialize() {
3933
frmDescargaEnvios = new JFrame();
4034
frmDescargaEnvios.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));

src/main/java/giis/demo/tkrun/EmpresaModel.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ public void crearPedidos(String nEmpresa) {
1919
List<EnvioEntity> envios = cargarEnvios("src/main/java/files/" + nEmpresa + ".csv", nEmpresa);
2020
if (envios.size() > 0) {
2121
Random rd = new Random();
22+
List<Integer> nrefs = new ArrayList<>();
2223
for (EnvioEntity envio: envios) {
23-
envio.setNref(rd.nextInt(999999999));
24+
int rand = rd.nextInt(999999999);
25+
envio.setNref(rand);
26+
nrefs.add(rand);
2427
db.executeUpdate(sql,envio.getNref(),envio.getNombreEmisor(),envio.getCorreoEmisor(),envio.getDireccionEmisor(),
2528
envio.getNombreReceptor(),envio.getCorreoReceptor(),envio.getDireccionReceptor(),envio.getLargo(),envio.getAncho(),
2629
envio.getAlto(),envio.getPeso(),envio.getPrecio(),envio.getInstrExtra());
2730
}
28-
JOptionPane.showMessageDialog(null, "SE HAN REGISTRADO CORRECTAMENTE " + envios.size() + " ENVÍOS");
31+
JOptionPane.showMessageDialog(null, "SE HAN REGISTRADO CORRECTAMENTE " + envios.size() + " ENVÍOS\nNúmeros de referencia: " + nrefs.toString());
2932
}
3033
}
3134

src/main/java/giis/demo/tkrun/EmpresaView.java

-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@ public class EmpresaView {
2222
private JButton btnCancelar;
2323
private JButton btRegistro;
2424

25-
/**
26-
* Create the application.
27-
*/
2825
public EmpresaView() {
2926
initialize();
3027
}
3128

32-
/**
33-
* Initialize the contents of the frame.
34-
*/
3529
private void initialize() {
3630
frmRegistroEmpresa = new JFrame();
3731
frmRegistroEmpresa.setTitle("RegistroEmpresa");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package giis.demo.tkrun;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import javax.swing.JOptionPane;
9+
import javax.swing.table.TableModel;
10+
11+
import giis.demo.util.SwingUtil;
12+
13+
public class EntregaController {
14+
private EntregaModel model;
15+
private EntregaView view;
16+
17+
public EntregaController(EntregaModel entregaModel, EntregaView entregaView) {
18+
this.model = entregaModel;
19+
this.view = entregaView;
20+
//no hay inicializacion especifica del modelo, solo de la vista
21+
this.initView();
22+
}
23+
24+
public void initController() {
25+
view.getBtCancelar().addActionListener(e -> SwingUtil.exceptionWrapper(() -> salir()));
26+
view.getBtRegistrar().addActionListener(e -> realizarEntrega());
27+
view.getBtInfo().addActionListener(e -> verInfo());
28+
}
29+
30+
private void verInfo() {
31+
String nref = view.getTfNRef().getText();
32+
if(comprobarNRef(nref)) {
33+
int nrefnum = Integer.parseInt(nref);
34+
EnvioDisplayDTO envio = model.getEnvio(nrefnum);
35+
if(validarEnvio(envio,nrefnum)) {
36+
List<EnvioDisplayDTO> pedidos = new ArrayList<>();
37+
pedidos.add(envio);
38+
TableModel tmodel = SwingUtil.getTableModelFromPojos(pedidos, new String[] {"nombreReceptor", "correoReceptor","direccionReceptor"});
39+
view.getTbInfo().setModel(tmodel);
40+
SwingUtil.autoAdjustColumns(view.getTbInfo());
41+
}
42+
}
43+
}
44+
45+
private boolean validarEnvio(EnvioDisplayDTO envio, int nrefnum) {
46+
if (envio==null) {
47+
JOptionPane.showMessageDialog(null, "No existe un envio \ncon el NRef: " + nrefnum);
48+
return false;
49+
} else return true;
50+
}
51+
52+
private boolean comprobarNRef(String nref) {
53+
try {
54+
if(nref.isBlank() || Integer.parseInt(nref) <= 0) {
55+
JOptionPane.showMessageDialog(null, "Formato de NRef inválido");
56+
return false;
57+
} else return true;
58+
} catch (NumberFormatException e) {
59+
JOptionPane.showMessageDialog(null, "Formato de NRef inválido");
60+
return false;
61+
}
62+
}
63+
64+
private void realizarEntrega() {
65+
if (comprobarCampos()) {
66+
int id = Integer.parseInt(view.getTfID().getText());
67+
int nref = Integer.parseInt(view.getTfNRef().getText());
68+
if (comprobarEnvio(id, nref)) {
69+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
70+
LocalDateTime now = LocalDateTime.now();
71+
String fechaHoraActual = dtf.format(now);
72+
model.registrarEntrega(id, nref, "Entrega", view.getTfUbicacion().getText(), fechaHoraActual);
73+
JOptionPane.showMessageDialog(null, "REGISTRO DE ENTREGA REALIZADO CORRECTAMENTE");
74+
view.reset();
75+
}
76+
}
77+
}
78+
79+
private boolean comprobarEnvio(int id, int nref) {
80+
PedidosTransportistaDisplayDTO envio = model.getEnvio(id, nref);
81+
if (envio==null) {
82+
JOptionPane.showMessageDialog(null, "No existe un pedido con el número: " + nref + "\nasignado al repartidor con ID: " + id);
83+
return false;
84+
} else {
85+
MovimientosDisplayDTO movimiento = model.getLastMov(id, nref);
86+
if (movimiento != null) {
87+
if (movimiento.getMovimiento().equals("Descarga")) {
88+
JOptionPane.showMessageDialog(null, "No puede haber un movimiento de entrega seguido de uno de descarga. \nEl último movimiento debe ser de carga");
89+
return false;
90+
} else if (!envio.getDireccionReceptor().equals(view.getTfUbicacion().getText())) {
91+
JOptionPane.showMessageDialog(null, "La ubicación de la entrega no coincide con la de la entrega asignada");
92+
return false;
93+
}
94+
} else {
95+
JOptionPane.showMessageDialog(null, "El envío aún no ha sido recogido");
96+
return false;
97+
}
98+
return true;
99+
}
100+
}
101+
102+
private boolean comprobarCampos() {
103+
try {
104+
if(view.getTfID().getText().isBlank() || view.getTfNRef().getText().isBlank() || view.getTfUbicacion().getText().isBlank()) {
105+
JOptionPane.showMessageDialog(null, "Rellena todos los campos");
106+
return false;
107+
} else if(Integer.parseInt(view.getTfID().getText()) <= 0 || Integer.parseInt(view.getTfNRef().getText()) <= 0) {
108+
JOptionPane.showMessageDialog(null, "Formato de ID o Número de referencia inválido");
109+
return false;
110+
} else return true;
111+
} catch (NumberFormatException e) {
112+
JOptionPane.showMessageDialog(null, "Formato de ID o Número de referencia inválido");
113+
return false;
114+
}
115+
}
116+
117+
private void salir() {
118+
int eleccion=JOptionPane.showConfirmDialog(null, "¿Está segur@ de cancelar la entrega del envío?");
119+
if(eleccion==JOptionPane.YES_OPTION)
120+
view.reset();
121+
}
122+
123+
public void initView() {
124+
view.getFrame().setVisible(true);
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package giis.demo.tkrun;
2+
3+
import java.util.List;
4+
5+
import giis.demo.util.Database;
6+
7+
public class EntregaModel {
8+
9+
private Database db=new Database();
10+
11+
public PedidosTransportistaDisplayDTO getEnvio(int id, int nref) {
12+
String sql = "select * from pedidosTransportista where id=? and nref=?";
13+
List<PedidosTransportistaDisplayDTO> envios = db.executeQueryPojo(PedidosTransportistaDisplayDTO.class, sql, id, nref);
14+
if(envios.isEmpty()) {
15+
return null;
16+
}
17+
return envios.get(0);
18+
}
19+
20+
public MovimientosDisplayDTO getLastMov(int id, int nref) {
21+
String sql = "select * from movimientos where id=? and nref=? order by fechamov desc";
22+
List<MovimientosDisplayDTO> movimientos = db.executeQueryPojo(MovimientosDisplayDTO.class, sql, id, nref);
23+
if(movimientos.isEmpty()) {
24+
return null;
25+
}
26+
return movimientos.get(0);
27+
}
28+
29+
public void registrarEntrega(int id, int nref, String mov, String ubi, String fechaHoraActual) {
30+
String sql="insert into movimientos (id,nref,movimiento,ubicacion,fechaMov) values (?,?,?,?,?)";
31+
db.executeUpdate(sql,id,nref,mov,ubi,fechaHoraActual);
32+
}
33+
34+
public EnvioDisplayDTO getEnvio(int nrefnum) {
35+
String sql = "select * from envios where nref=?";
36+
List<EnvioDisplayDTO> envios = db.executeQueryPojo(EnvioDisplayDTO.class, sql, nrefnum);
37+
if(envios.isEmpty()) {
38+
return null;
39+
}
40+
return envios.get(0);
41+
}
42+
43+
44+
45+
}

0 commit comments

Comments
 (0)