Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/save booking details #11

Open
wants to merge 18 commits into
base: repositorio-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .classpath

This file was deleted.

6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin/*
.idea
*.iml
.metadata/*
.classpath
testbin/*
Binary file added JARS/byte-buddy-1.14.5.jar
Binary file not shown.
Binary file added JARS/byte-buddy-agent-1.14.5.jar
Binary file not shown.
Binary file added JARS/hamcrest-2.2.jar
Binary file not shown.
Binary file added JARS/jgoodies-common-1.2.0.jar
Binary file not shown.
Binary file added JARS/jgoodies-looks-2.4.1.jar
Binary file not shown.
Binary file added JARS/junit-4.13.2.jar
Binary file not shown.
Binary file added JARS/mockito-core-5.4.0.jar
Binary file not shown.
1 change: 0 additions & 1 deletion bin/.gitignore

This file was deleted.

Binary file removed bin/imagenes/Ha-100px.png
Binary file not shown.
Binary file removed bin/imagenes/aH-150px.png
Binary file not shown.
Binary file removed bin/imagenes/aH-40px.png
Binary file not shown.
Binary file removed bin/imagenes/busqueda.png
Binary file not shown.
Binary file removed bin/imagenes/calendario.png
Binary file not shown.
Binary file removed bin/imagenes/cancelar.png
Binary file not shown.
Binary file removed bin/imagenes/cerrar-24px.png
Binary file not shown.
Binary file removed bin/imagenes/cerrar-sesion 32-px.png
Binary file not shown.
Binary file removed bin/imagenes/deletar.png
Binary file not shown.
Binary file removed bin/imagenes/disquete.png
Binary file not shown.
Binary file removed bin/imagenes/editar-texto.png
Binary file not shown.
Binary file removed bin/imagenes/hotel.png
Binary file not shown.
Binary file removed bin/imagenes/icon-buscar.png
Binary file not shown.
Binary file removed bin/imagenes/icon-reservas.png
Binary file not shown.
Binary file removed bin/imagenes/img-hotel-login-.png
Binary file not shown.
Binary file removed bin/imagenes/lOGO-50PX.png
Binary file not shown.
Binary file removed bin/imagenes/login.png
Binary file not shown.
Binary file removed bin/imagenes/login2.png
Binary file not shown.
Binary file removed bin/imagenes/lupa-1.png
Binary file not shown.
Binary file removed bin/imagenes/lupa2.png
Binary file not shown.
Binary file removed bin/imagenes/menu-img.png
Binary file not shown.
Binary file removed bin/imagenes/papelera-de-reciclaje.png
Binary file not shown.
Binary file removed bin/imagenes/perfil-del-usuario.png
Binary file not shown.
Binary file removed bin/imagenes/persona.png
Binary file not shown.
Binary file removed bin/imagenes/pessoas.png
Binary file not shown.
Binary file removed bin/imagenes/registro.png
Diff not rendered.
Binary file removed bin/imagenes/reservado.png
Diff not rendered.
Binary file removed bin/imagenes/reservas-img-3.png
Diff not rendered.
Binary file removed bin/imagenes/reservas.png
Diff not rendered.
Binary file removed bin/views/Busqueda$1.class
Binary file not shown.
Binary file removed bin/views/Busqueda$2.class
Binary file not shown.
Binary file removed bin/views/Busqueda$3.class
Binary file not shown.
Binary file removed bin/views/Busqueda.class
Binary file not shown.
Binary file removed bin/views/Exito$1.class
Binary file not shown.
Binary file removed bin/views/Exito.class
Binary file not shown.
Binary file removed bin/views/Login$1.class
Binary file not shown.
Binary file removed bin/views/Login$2.class
Binary file not shown.
Binary file removed bin/views/Login.class
Binary file not shown.
Binary file removed bin/views/MenuPrincipal$1.class
Binary file not shown.
Binary file removed bin/views/MenuPrincipal$2.class
Binary file not shown.
Binary file removed bin/views/MenuPrincipal$3.class
Binary file not shown.
Binary file removed bin/views/MenuPrincipal.class
Binary file not shown.
Binary file removed bin/views/MenuUsuario$1.class
Binary file not shown.
Binary file removed bin/views/MenuUsuario$2.class
Binary file not shown.
Binary file removed bin/views/MenuUsuario$3.class
Binary file not shown.
Binary file removed bin/views/MenuUsuario$4.class
Binary file not shown.
Binary file removed bin/views/MenuUsuario.class
Binary file not shown.
Binary file removed bin/views/RegistroHuesped$1.class
Binary file not shown.
Binary file removed bin/views/RegistroHuesped$2.class
Binary file not shown.
Binary file removed bin/views/RegistroHuesped$3.class
Binary file not shown.
Binary file removed bin/views/RegistroHuesped.class
Binary file not shown.
2 changes: 2 additions & 0 deletions config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db_url=jdbc:mysql://localhost/hotel?useTimeZone=true&serverTimeZone=UTC
price_per_night=5000
52 changes: 52 additions & 0 deletions database/1-create-database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

use hotel;

DROP TABLE guest;
DROP TABLE booking;
DROP TABLE user_data;



CREATE TABLE booking (

id INT AUTO_INCREMENT,
entry_date TIMESTAMP,
departure_date TIMESTAMP,
price DOUBLE,
method_payment VARCHAR(20),
PRIMARY KEY(id)
)Engine=InnoDB;

CREATE TABLE guest (

id INT AUTO_INCREMENT,
name VARCHAR(50)NOT NULL,
lastname VARCHAR(100)NOT NULL,
birthdate DATE,
nationality VARCHAR(50),
telephone VARCHAR(15), CONSTRAINT chk_telephone CHECK (telephone REGEXP '^[0-9]{7,10}$'),
id_booking INT,
FOREIGN KEY(id_booking)REFERENCES booking(id),
PRIMARY KEY(id)
)Engine=InnoDB;

CREATE TABLE user_data (

id INT AUTO_INCREMENT,
login VARCHAR(50),
password VARCHAR(50),
PRIMARY KEY(id)
)Engine=InnoDB;

INSERT INTO user_data(login,password)
VALUES('abeltran','1234'),
('bbeltran','5678'),
('cbeltran','9876'),
('dbeltran','5432'),
('ebeltran','1234'),
('fbeltran','5678'),
('gbeltran','9876'),
('hbeltran','5432'),
('ibeltran','1234'),
('jbeltran','5678');

31 changes: 31 additions & 0 deletions src/config/Setting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config;

import java.io.FileInputStream;
import java.util.Properties;

public class Setting {

private static final String CONFIG_PROPERTIES = "config.properties";
private final Properties properties;

public Setting() {
properties=new Properties();
loadProperties(CONFIG_PROPERTIES);
}

private void loadProperties(String filePath) {
FileInputStream fileInputStream;

try {
fileInputStream=new FileInputStream(filePath);
properties.load(fileInputStream);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public String getProperty(String name) {
return properties.getProperty(name);
}
}

33 changes: 33 additions & 0 deletions src/database/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package database;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import config.Setting;

public class ConnectionManager {

private final DataSource datasource;
private final Setting setting;
private static final int MAX_POOL_SIZE=10;

public ConnectionManager() {
ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
setting=new Setting();
pooledDataSource.setJdbcUrl(setting.getProperty("db_url"));
pooledDataSource.setUser(System.getenv("USERNAME"));
pooledDataSource.setPassword(System.getenv("PASSWORD"));
pooledDataSource.setMaxPoolSize(MAX_POOL_SIZE);
this.datasource=pooledDataSource;
}

public Connection getConnection() {
try {
return this.datasource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

}
32 changes: 32 additions & 0 deletions src/database/DbLogin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package database;

import java.util.HashMap;
import java.util.Map;

public class DbLogin {

private final Map <String, String>loginPassword=new HashMap<>();

public DbLogin(){
loginPassword.put("asalinas","1234");
loginPassword.put("bsalinas","5678");
loginPassword.put("csalinas","9876");
loginPassword.put("dsalinas","5432");
loginPassword.put("esalinas","1234");
loginPassword.put("fsalinas","5678");
loginPassword.put("gsalinas","9876");
loginPassword.put("hsalinas","5432");
loginPassword.put("isalinas","1234");
loginPassword.put("jsalinas","5678");

}

public boolean loginUser(String login, String constrasena) {
if(loginPassword.containsKey(login)){
if(loginPassword.get(login).equals(constrasena)){
return true;
}
}
return false;
}
}
58 changes: 58 additions & 0 deletions src/database/dao/BookingDataDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package database.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import database.dto.BookingDataDTO;

public class BookingDataDAO extends MainDAO {

private static final String SAVE_IN_BOOKING="INSERT INTO booking(entry_date,departure_date,method_payment,price)"+
"VALUES(?,?,?,?)";

public BookingDataDTO save(BookingDataDTO bookingData) {
Connection con= super.getConnection();

try {
final PreparedStatement statement = con.prepareStatement(SAVE_IN_BOOKING, Statement.RETURN_GENERATED_KEYS);
try (statement){
Integer id = saveRecord(bookingData, statement);
bookingData.setId(id);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return bookingData;
}

Integer saveRecord(BookingDataDTO bookingData, PreparedStatement statement) throws SQLException {
LocalDateTime entryDatebyUser=bookingData.getEntryDate();
Timestamp entryTimestampUser=Timestamp.valueOf(entryDatebyUser);

LocalDateTime departureDatebyUser=bookingData.getDepartureDate();
Timestamp departureTimestampUser=Timestamp.valueOf(departureDatebyUser);

String methodPaymentUser = bookingData.getMethodPayment().getName();
BigDecimal priceByBookingService=bookingData.getPrice();

statement.setTimestamp(1,entryTimestampUser);
statement.setTimestamp(2, departureTimestampUser);
statement.setString(3, methodPaymentUser);
statement.setBigDecimal(4, priceByBookingService);

statement.execute();

final ResultSet resultSet=statement.getGeneratedKeys();
try(resultSet){
if(resultSet.next()) {
return resultSet.getInt(1);
}
}
return null;
}
}
33 changes: 33 additions & 0 deletions src/database/dao/MainDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package database.dao;

import java.sql.Connection;
import java.sql.SQLException;
import database.ConnectionManager;

public class MainDAO implements AutoCloseable{

private Connection con;

protected synchronized Connection getConnection() {
if (con == null) {
con = new ConnectionManager().getConnection();
} else {
try{
if (con.isClosed()) {
con = new ConnectionManager().getConnection();
}
} catch (SQLException e) {
e.printStackTrace();
con = new ConnectionManager().getConnection();
}
}
return con;
}

@Override
public void close() throws Exception {
con.close();

}

}
45 changes: 45 additions & 0 deletions src/database/dao/UserDataDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package database.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import database.dto.UserDataDTO;

public class UserDataDAO extends MainDAO{

private Connection con;
private static final String SELECT_USER_BY_LOGIN="SELECT id, login, password FROM user_data WHERE login=?";


public UserDataDTO getUserByLogin(String login){

UserDataDTO userDataDTO = null;
con= super.getConnection();

try {
final PreparedStatement statement=con.prepareStatement(SELECT_USER_BY_LOGIN);

try(statement){
statement.setString(1,login);
statement.execute();
final ResultSet resultSet=statement.getResultSet();

try(resultSet){
if(resultSet.next()) {
userDataDTO=new UserDataDTO(
resultSet.getInt("id"),
resultSet.getString("login"),
resultSet.getString("password"));
}
}
}
return userDataDTO;
}catch(SQLException e) {
throw new RuntimeException(e);
}
}
}



69 changes: 69 additions & 0 deletions src/database/dto/BookingDataDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package database.dto;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public class BookingDataDTO {

private int id;
private LocalDateTime entryDate;
private LocalDateTime departureDate;
private BigDecimal price;
private PaymentMethodDTO methodPayment;

public BookingDataDTO(LocalDateTime entryDate, LocalDateTime departureDate, PaymentMethodDTO methodPayment) {
this.entryDate=entryDate;
this.departureDate=departureDate;
this.methodPayment=methodPayment;
}

public BookingDataDTO(LocalDateTime entryDate, LocalDateTime departureDate, PaymentMethodDTO methodPayment,
BigDecimal bookingPrice) {
this.entryDate=entryDate;
this.departureDate=departureDate;
this.methodPayment=methodPayment;
this.price=bookingPrice;
}

public LocalDateTime getEntryDate() {
return entryDate;
}

public void setEntryDate(LocalDateTime entryDate) {
this.entryDate = entryDate;
}

public LocalDateTime getDepartureDate() {
return departureDate;
}

public void setDepartureDate(LocalDateTime departureDate) {
this.departureDate = departureDate;
}

public PaymentMethodDTO getMethodPayment() {
return methodPayment;
}

public void setMethodPayment(PaymentMethodDTO methodPayment) {
this.methodPayment = methodPayment;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public BigDecimal getPrice() {
return price;
}

@Override
public String toString() {
String formattedDateTime=entryDate.toString();
return String.format("id: %s and date: %s", this.id+formattedDateTime);
}
}
Loading