0% found this document useful (0 votes)
6 views3 pages

Transport Service

The TransportService class implements the IService interface for managing transport entities in a database. It provides methods to create, update, delete, and read all transport records using SQL queries and PreparedStatements. The class also handles the conversion of transport types and date formats while interacting with the database.

Uploaded by

nadia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Transport Service

The TransportService class implements the IService interface for managing transport entities in a database. It provides methods to create, update, delete, and read all transport records using SQL queries and PreparedStatements. The class also handles the conversion of transport types and date formats while interacting with the database.

Uploaded by

nadia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package services;

import entities.Transport;
import entities.TransportType;
import utils.MyConnection;

import java.sql.*;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TransportService implements IService<Transport> {

private Connection cnx;

@Override
public void create(Transport transport) throws SQLException {
String query = "INSERT INTO transport (type, companyName,
departureLocation, arrivalLocation, departureTime, arrivalTime, price, isElectric,
availability) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

// PreparedStatement to insert the transport


PreparedStatement ps = cnx.prepareStatement(query);
ps.setString(1, transport.getType().name()); // Using enum's name() to get
the string representation of the type
ps.setString(2, transport.getCompanyName());
ps.setString(3, transport.getDepartureLocation());
ps.setString(4, transport.getArrivalLocation());
ps.setString(5, transport.getDepartureTime()); // Convert LocalDateTime to
Timestamp
ps.setString(6, transport.getArrivalTime()); // Convert LocalDateTime to
Timestamp
ps.setDouble(7, transport.getPrice());
ps.setBoolean(8, transport.isElectric());
ps.setBoolean(9, transport.getAvailability());

// Execute update
ps.executeUpdate();
System.out.println("Transport created");
}

@Override
public void update(Transport transport) throws SQLException {
String query = "UPDATE transport SET type = ?, companyName = ?,
departureLocation = ?, arrivalLocation = ?, departureTime = ?, arrivalTime = ?,
price = ?, isElectric = ?, availability = ? WHERE IdTransport = ?";

PreparedStatement ps = cnx.prepareStatement(query);

// Setting the updated values in the prepared statement

ps.setString(1, transport.getType().name()); // Enum to String


ps.setString(2, transport.getCompanyName());
ps.setString(3, transport.getDepartureLocation());
ps.setString(4, transport.getArrivalLocation());
ps.setString(5, transport.getDepartureTime());
ps.setString(6, transport.getArrivalTime());
ps.setDouble(7, transport.getPrice());
ps.setBoolean(8, transport.isElectric());
ps.setBoolean(9, transport.getAvailability());
ps.setInt(10, transport.getIdTransport());

// Execute the update transport


ps.executeUpdate();
System.out.println("Transport updated");
}

@Override
public void delete(Transport transport) throws SQLException {
String deleteReservationsQuery = "DELETE FROM reservation WHERE idTransport
= ?";
PreparedStatement ps1 = cnx.prepareStatement(deleteReservationsQuery);
ps1.setInt(1, transport.getIdTransport());
ps1.executeUpdate();

String query = "DELETE FROM transport WHERE idTransport = ?";

// PreparedStatement to execute the deletion query


PreparedStatement ps = cnx.prepareStatement(query);

// Setting the transport's ID for deletion


ps.setInt(1, transport.getIdTransport());

// Execute the delete operation


ps.executeUpdate();
System.out.println("Transport deleted");
}

@Override
public List<Transport> readAll() throws SQLException {
List<Transport> transports = new ArrayList<>();
String query = "SELECT * FROM transport";
Statement st = cnx.createStatement();
ResultSet rs = st.executeQuery(query);

while (rs.next()) {
// Retrieve data for each transport
String typeStr = rs.getString("type");
String companyName = rs.getString("companyName");
String departureLocation = rs.getString("departureLocation");
String arrivalLocation = rs.getString("arrivalLocation");
String departureTime = rs.getString("departureTime");
String arrivalTime = rs.getString("arrivalTime");
double price = rs.getDouble("price");
boolean isElectric = rs.getBoolean("isElectric");
boolean availability = rs.getBoolean("availability");

// Get the Date field from the ResultSet


String dateString = rs.getString("Date"); // Assuming "Date" is the
name of the date column
Date transportDate = null;

if (dateString != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
transportDate = sdf.parse(dateString); // Convert the string
to Date
} catch (Exception e) {
e.printStackTrace(); // Handle the exception in case of an
invalid date format
}
}

// Convert typeStr to the corresponding enum value


TransportType type = TransportType.valueOf(typeStr);

// Create a new Transport object


Transport transport = new Transport(
type,
companyName,
departureLocation,
arrivalLocation,
transportDate, // Pass the parsed Date object here
departureTime,
arrivalTime,
price,
isElectric,
availability
);

// Add the transport to the list


transports.add(transport);
}

return transports;
}

public TransportService() throws SQLException {


cnx= MyConnection.getInstance().getConnection();
}

You might also like