diff --git a/src/cl/com/alura/hotel/factory/ConnectionFactory.java b/src/cl/com/alura/hotel/factory/ConnectionFactory.java new file mode 100644 index 0000000..250c6b9 --- /dev/null +++ b/src/cl/com/alura/hotel/factory/ConnectionFactory.java @@ -0,0 +1,37 @@ +package cl.com.alura.hotel.factory; + +import java.sql.Connection; +import javax.sql.DataSource; + +import com.mchange.v2.c3p0.ComboPooledDataSource; +import io.github.cdimascio.dotenv.Dotenv; + +public class ConnectionFactory { + + private static String dbaddr; + private static String dbname; + private final static String driver = "jdbc:mysql://"; + private final static String params = "?useTimeZone=true&serverTimeZone=UTC"; + private DataSource datasource; + + public ConnectionFactory() { + Dotenv dotenv = Dotenv.load(); + dbname = dotenv.get("DBNAME"); + dbaddr = dotenv.get("DBADDR"); + final String dburl = driver + dbaddr +"/"+ dbname + params; + var pooledDataSource = new ComboPooledDataSource(); + pooledDataSource.setJdbcUrl(dburl); + pooledDataSource.setUser(dotenv.get("DBUSER")); + pooledDataSource.setPassword(dotenv.get("DBPASS")); + pooledDataSource.setMaxPoolSize(500); + this.datasource = pooledDataSource; + } + + public Connection getConexion() { + try { + return this.datasource.getConnection(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/cl/com/alura/hotel/modelo/Huesped.java b/src/cl/com/alura/hotel/modelo/Huesped.java new file mode 100644 index 0000000..3861dd3 --- /dev/null +++ b/src/cl/com/alura/hotel/modelo/Huesped.java @@ -0,0 +1,87 @@ +package cl.com.alura.hotel.modelo; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class Huesped { + + private Integer id; + private Integer nombre; + private Integer apellido; + private Date fechaNacimiento; + private String nacionalidad; + private String telefono; + private List reservas; + + public Huesped(Integer id, Integer nombre, Integer apellido, Date fechaNacimiento, + String nacionalidad, String telefono) { + this.id = id; + this.nombre = nombre; + this.apellido = apellido; + this.fechaNacimiento = fechaNacimiento; + this.nacionalidad = nacionalidad; + this.telefono = telefono; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getNombre() { + return nombre; + } + + public void setNombre(Integer nombre) { + this.nombre = nombre; + } + + public Integer getApellido() { + return apellido; + } + + public void setApellido(Integer apellido) { + this.apellido = apellido; + } + + public Date getFechaNacimiento() { + return fechaNacimiento; + } + + public void setFechaNacimiento(Date fechaNacimiento) { + this.fechaNacimiento = fechaNacimiento; + } + + public String getNacionalidad() { + return nacionalidad; + } + + public void setNacionalidad(String nacionalidad) { + this.nacionalidad = nacionalidad; + } + + public String getTelefono() { + return telefono; + } + + public void setTelefono(String telefono) { + this.telefono = telefono; + } + + public List getReservas() { + return reservas; + } + + public void addReserva(Reserva reserva) { + if (this.reservas == null) { + this.reservas = new ArrayList<>(); + } + this.reservas.add(reserva); + } + + // TODO toString(); +} \ No newline at end of file diff --git a/src/cl/com/alura/hotel/modelo/Reserva.java b/src/cl/com/alura/hotel/modelo/Reserva.java new file mode 100644 index 0000000..d0fc4f9 --- /dev/null +++ b/src/cl/com/alura/hotel/modelo/Reserva.java @@ -0,0 +1,72 @@ +package cl.com.alura.hotel.modelo; + +import java.util.Date; + +public class Reserva { + + private Integer id; + private Date fechaEntrada; + private Date fechaSalida; + private Double valor; + private String formaPago; + private Integer idHuesped; + + public Reserva(Integer id, Date fechaEntrada, Date fechaSalida, Double valor, String formaPago, Integer idHuesped) { + this.id = id; + this.fechaEntrada = fechaEntrada; + this.fechaSalida = fechaSalida; + this.valor = valor; + this.formaPago = formaPago; + this.idHuesped = idHuesped; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Date getFechaEntrada() { + return fechaEntrada; + } + + public void setFechaEntrada(Date fechaEntrada) { + this.fechaEntrada = fechaEntrada; + } + + public Date getFechaSalida() { + return fechaSalida; + } + + public void setFechaSalida(Date fechaSalida) { + this.fechaSalida = fechaSalida; + } + + public Double getValor() { + return valor; + } + + public void setValor(Double valor) { + this.valor = valor; + } + + public String getFormaPago() { + return formaPago; + } + + public void setFormaPago(String formaPago) { + this.formaPago = formaPago; + } + + public Integer getIdHuesped() { + return idHuesped; + } + + public void setIdHuesped(Integer idHuesped) { + this.idHuesped = idHuesped; + } + + // TODO toString(); +} diff --git a/src/cl/com/alura/hotel/pruebas/PruebaConexion.java b/src/cl/com/alura/hotel/pruebas/PruebaConexion.java index 7a9c026..ee7370c 100644 --- a/src/cl/com/alura/hotel/pruebas/PruebaConexion.java +++ b/src/cl/com/alura/hotel/pruebas/PruebaConexion.java @@ -10,8 +10,6 @@ public class PruebaConexion { private static String dbaddr; private static String dbname; - private static String dbuser; - private static String dbpass; private final static String driver = "jdbc:mysql://"; private final static String params = "?useTimeZone=true&serverTimeZone=UTC"; @@ -19,11 +17,13 @@ public class PruebaConexion { Dotenv dotenv = Dotenv.load(); dbname = dotenv.get("DBNAME"); dbaddr = dotenv.get("DBADDR"); - dbuser = dotenv.get("DBUSER"); - dbpass = dotenv.get("DBPASS"); final String dburl = driver + dbaddr + "/" + dbname + params; System.out.println("Iniciando conexión con DB: " + dburl); - Connection con = DriverManager.getConnection(dburl, dbuser, dbpass); + Connection con = DriverManager.getConnection( + dburl, + dotenv.get("DBUSER"), + dotenv.get("DBPASS") + ); con.close(); System.out.println("Conexión con DB cerrada!"); } diff --git a/src/cl/com/alura/hotel/pruebas/PruebaConexionFactory.java b/src/cl/com/alura/hotel/pruebas/PruebaConexionFactory.java new file mode 100644 index 0000000..4cad9f0 --- /dev/null +++ b/src/cl/com/alura/hotel/pruebas/PruebaConexionFactory.java @@ -0,0 +1,17 @@ +package cl.com.alura.hotel.pruebas; + +import java.sql.Connection; +import java.sql.SQLException; + +import cl.com.alura.hotel.factory.ConnectionFactory; + +public class PruebaConexionFactory { + + public static void main(String[] args) throws SQLException { + System.out.println("Abriendo conexión"); + Connection con = new ConnectionFactory().getConexion(); + System.out.println("Cerrando conexión"); + con.close(); + } + +} diff --git a/src/cl/com/alura/hotel/views/Exito.java b/src/cl/com/alura/hotel/views/Exito.java index 1ad70ee..8d0a9b9 100644 --- a/src/cl/com/alura/hotel/views/Exito.java +++ b/src/cl/com/alura/hotel/views/Exito.java @@ -53,7 +53,7 @@ public class Exito extends JDialog { contentPanel.add(lblNewLabel); } { - JLabel lblNewLabel_1 = new JLabel("Datos guardados satisfactoriamente"); + JLabel lblNewLabel_1 = new JLabel("Datos guardados exitosamente"); lblNewLabel_1.setForeground(new Color(12, 138, 199)); lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 18)); lblNewLabel_1.setBounds(27, 122, 322, 21); @@ -67,7 +67,8 @@ public class Exito extends JDialog { JButton okButton = new JButton("OK"); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - dispose();// sirve para cerrar la ventana actual + // cerrar ventana actual + dispose(); MenuUsuario usuario = new MenuUsuario(); usuario.setVisible(true); } diff --git a/src/cl/com/alura/hotel/views/Login.java b/src/cl/com/alura/hotel/views/Login.java index c91c6c3..075347f 100644 --- a/src/cl/com/alura/hotel/views/Login.java +++ b/src/cl/com/alura/hotel/views/Login.java @@ -194,7 +194,7 @@ public class Login extends JFrame { @Override public void mouseClicked(MouseEvent e) { - Login(); + LoginAction(); } }); btnLogin.setBackground(SystemColor.textHighlight); @@ -236,7 +236,7 @@ public class Login extends JFrame { header.setLayout(null); } - private void Login() { + private void LoginAction() { String Usuario = "admin"; String Contraseña = "admin"; diff --git a/src/module-info.java b/src/module-info.java index 77978d0..6ab6307 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -11,4 +11,5 @@ module hotel_alura { requires java.sql; requires java.desktop; requires jcalendar; + requires c3p0; } \ No newline at end of file