Skip to content
Merged
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
Binary file added .DS_Store
Binary file not shown.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ Finally, you will be ready to import the code into an IDE of your choice and run

### Testing

The app has unit tests and integration tests written. More of these need to be added and in some places that can be seen mentioend as `TODO` comments. The existing tests need to be triggered from maven-surefire plugin while we try to generate the final executable jar file.

To run the tests from maven, go to the folder that contains the pom.xml file and execute the below command.

`mvn test`
35 changes: 31 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<version>1.4.200</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.1.4</version>
</dependency>
</dependencies>

<build>
Expand All @@ -104,7 +109,7 @@
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>executable-jar-with-dependencies</descriptorRef>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
Expand Down Expand Up @@ -163,6 +168,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.1.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
</plugins>
</build>

Expand All @@ -181,10 +196,22 @@
</reportSets>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.1.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.21.0</version>
</plugin>

</plugins>
</reporting>

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/parkit/parkingsystem/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
/**
* Start App.
*/
public class App {
public final class App {
private App() { }
/**
* @see Logger
*/
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/parkit/parkingsystem/config/DataBaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@
* Configuration for DataBase using Singleton pattern.
*/
public enum DataBaseManager {
/**
* Instance for Singleton pattern.
*/
INSTANCE;

/**
* @see Logger
*/
private final Logger LOGGER = LogManager.getLogger(DataBaseManager.class);
private static final Logger LOGGER =
LogManager.getLogger(DataBaseManager.class);

/**
* @see DataSource
*/
private DataSource dataSource;

DataBaseManager() {
Properties properties = new Properties();
FileInputStream fis;
try {
URL propertiesUrl = DataBaseManager.class.getClassLoader().getResource("db.properties");
fis = new FileInputStream(new File(propertiesUrl.getFile()));
URL propertiesUrl = DataBaseManager.class.getClassLoader().
getResource("db.properties");
File file;
assert propertiesUrl != null;
file = new File(propertiesUrl.getFile());
try (FileInputStream fis = new FileInputStream(file)) {
properties.load(fis);
dataSource = DataSourceFactory.get(
properties.getProperty("jdbc.url"),
properties.getProperty("jdbc.user"),
properties.getProperty("jdbc.password")
);
} catch (IOException e) {
LOGGER.error("Error while getting db properties", e);
LogManager.getLogger(DataBaseManager.class)
.error("Error while getting db properties", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

import javax.sql.DataSource;

public class DataSourceFactory {
public final class DataSourceFactory {
private DataSourceFactory() { }

public static DataSource get(final String url, final String user, final String password) {
/**
* Get DataSource information to allow connect.
* @param url of properties file
* @param user user name for DB
* @param password Password associate for User of DB
* @return DataSource used
*/
public static DataSource get(
final String url, final String user, final String password) {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setURL(url);
dataSource.setUser(user);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Package info for Config package.
*/
package com.parkit.parkingsystem.config;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Manage DB constants.
*/
public class DBConstants {
public final class DBConstants {
private DBConstants() { }

/**
* Next parking spot.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/parkit/parkingsystem/constants/Fare.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Fare for type of vehicle.
*/
public class Fare {
public final class Fare {
private Fare() { }

/**
* Bike rate per hour.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ public enum ParkingType {
/**
* Bike.
*/
BIKE,
/**
* For testing only. voir si vraiment bonne méthode //TODO.
*/
TEST
BIKE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* All constants used in the program are noticed here to be re-used if needed.
*/
package com.parkit.parkingsystem.constants;
22 changes: 16 additions & 6 deletions src/main/java/com/parkit/parkingsystem/dao/ParkingSpotDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ public class ParkingSpotDAO {
/**
* @see Logger
*/
private static final Logger LOGGER = LogManager.getLogger(ParkingSpotDAO.class);
private static final Logger LOGGER =
LogManager.getLogger(ParkingSpotDAO.class);
/**
* Constant one.
*/
private static final byte ONE = 1;
/**
* Constant two.
*/
private static final byte TWO = 2;

/**
* Get the next available spot to park.
Expand All @@ -32,10 +41,10 @@ public int getNextAvailableSlot(final ParkingType parkingType) {
try (Connection con = DataBaseManager.INSTANCE.getConnection();
PreparedStatement ps =
con.prepareStatement(DBConstants.GET_NEXT_PARKING_SPOT)) {
ps.setString(1, parkingType.toString());
ps.setString(ONE, parkingType.toString());
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
result = rs.getInt(1);
result = rs.getInt(ONE);
}
}
} catch (SQLException ex) {
Expand All @@ -51,10 +60,11 @@ public int getNextAvailableSlot(final ParkingType parkingType) {
*/
public boolean updateParking(final ParkingSpot parkingSpot) {
try (Connection con = DataBaseManager.INSTANCE.getConnection();
PreparedStatement ps = con.prepareStatement(DBConstants.UPDATE_PARKING_SPOT)) {
PreparedStatement ps = con.prepareStatement(
DBConstants.UPDATE_PARKING_SPOT)) {

ps.setBoolean(1, parkingSpot.isAvailable());
ps.setInt(2, parkingSpot.getId());
ps.setBoolean(ONE, parkingSpot.isAvailable());
ps.setInt(TWO, parkingSpot.getId());
int updateRowCount = ps.executeUpdate();
return (updateRowCount == 1);
} catch (SQLException ex) {
Expand Down
79 changes: 57 additions & 22 deletions src/main/java/com/parkit/parkingsystem/dao/TicketDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;

/**
* Actions for tickets.
Expand All @@ -20,20 +25,46 @@ public class TicketDAO {
*/
private static final Logger LOGGER = LogManager.getLogger(TicketDAO.class);

/**
* Constant one.
*/
private static final byte ONE = 1;
/**
* Constant two.
*/
private static final byte TWO = 2;
/**
* Constant three.
*/
private static final byte THREE = 3;
/**
* Constant four.
*/
private static final byte FOUR = 4;
/**
* Constant five.
*/
private static final byte FIVE = 5;
/**
* Constant six.
*/
private static final byte SIX = 6;

/**
* Save ticket.
* @param ticket user ticket
* @return false
*/
public boolean saveTicket(final Ticket ticket) {
try (Connection con = DataBaseManager.INSTANCE.getConnection();
PreparedStatement ps = con.prepareStatement(DBConstants.SAVE_TICKET)) {
ps.setInt(1, ticket.getParkingSpot().getId());
ps.setString(2, ticket.getVehicleRegNumber());
ps.setNull(3, Types.DOUBLE);
ps.setTimestamp(4,
PreparedStatement ps = con.prepareStatement(
DBConstants.SAVE_TICKET)) {
ps.setInt(ONE, ticket.getParkingSpot().getId());
ps.setString(TWO, ticket.getVehicleRegNumber());
ps.setNull(THREE, Types.DOUBLE);
ps.setTimestamp(FOUR,
Timestamp.valueOf(ticket.getInTime()));
ps.setNull(5, Types.TIMESTAMP);
ps.setNull(FIVE, Types.TIMESTAMP);
int updateRowCount = ps.executeUpdate();
return (updateRowCount == 1);
} catch (SQLException ex) {
Expand All @@ -50,20 +81,22 @@ public boolean saveTicket(final Ticket ticket) {
public Ticket getTicket(final String vehicleRegNumber) {
Ticket ticket = null;
try (Connection con = DataBaseManager.INSTANCE.getConnection();
PreparedStatement ps = con.prepareStatement(DBConstants.GET_TICKET)) {
PreparedStatement ps =
con.prepareStatement(DBConstants.GET_TICKET)) {
ps.setString(1, vehicleRegNumber);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
ticket = new Ticket();
ParkingSpot parkingSpot = new ParkingSpot(rs.getInt(1),
ParkingType.valueOf(rs.getString(6)), false);
ParkingSpot parkingSpot = new ParkingSpot(rs.getInt(ONE),
ParkingType.valueOf(rs.getString(SIX)), false);
ticket.setParkingSpot(parkingSpot);
ticket.setId(rs.getInt(2));
ticket.setId(rs.getInt(TWO));
ticket.setVehicleRegNumber(vehicleRegNumber);
ticket.setPrice(rs.getDouble(3));
ticket.setInTime(rs.getTimestamp(4).toLocalDateTime());
Timestamp timestamp = rs.getTimestamp(5);
ticket.setOutTime(timestamp != null ? timestamp.toLocalDateTime() : null);
ticket.setPrice(rs.getDouble(THREE));
ticket.setInTime(rs.getTimestamp(FOUR).toLocalDateTime());
Timestamp timestamp = rs.getTimestamp(FIVE);
ticket.setOutTime(timestamp != null
? timestamp.toLocalDateTime() : null);
}
}
} catch (Exception ex) {
Expand All @@ -78,11 +111,12 @@ public Ticket getTicket(final String vehicleRegNumber) {
* @return true if one ticket contains vehicle reg number
*/
public boolean checkTicketByVehicleRegNumber(
final String vehicleRegNumber) throws SQLException {
final String vehicleRegNumber) {
boolean isRecurrent = false;
try (Connection con = DataBaseManager.INSTANCE.getConnection();
PreparedStatement ps = con.prepareStatement(DBConstants.FIND_TICKET_BY_VEHICLE_REG_NUMBER)) {
ps.setString(1, vehicleRegNumber);
PreparedStatement ps = con.prepareStatement(
DBConstants.FIND_TICKET_BY_VEHICLE_REG_NUMBER)) {
ps.setString(ONE, vehicleRegNumber);
try (ResultSet rs = ps.executeQuery()) {
isRecurrent = rs.next();
}
Expand All @@ -99,10 +133,11 @@ public boolean checkTicketByVehicleRegNumber(
*/
public boolean updateTicket(final Ticket ticket) {
try (Connection con = DataBaseManager.INSTANCE.getConnection();
PreparedStatement ps = con.prepareStatement(DBConstants.UPDATE_TICKET)) {
ps.setDouble(1, ticket.getPrice());
ps.setTimestamp(2, Timestamp.valueOf(ticket.getOutTime()));
ps.setInt(3, ticket.getId());
PreparedStatement ps = con.prepareStatement(
DBConstants.UPDATE_TICKET)) {
ps.setDouble(ONE, ticket.getPrice());
ps.setTimestamp(TWO, Timestamp.valueOf(ticket.getOutTime()));
ps.setInt(THREE, ticket.getId());
int updateRowCount = ps.executeUpdate();
return (updateRowCount == 1);
} catch (SQLException ex) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/parkit/parkingsystem/dao/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* DAO manage the relation between model and database.
*/
package com.parkit.parkingsystem.dao;
Loading