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

Project 04 final #52

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<version>0.8.7</version>
<type>maven-plugin</type>
</dependency>
<dependency>
Expand Down Expand Up @@ -81,7 +81,7 @@
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>executable-jar-with-dependencies</descriptorRef>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
Expand Down Expand Up @@ -124,7 +124,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<version>0.8.7</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public Connection getConnection() throws ClassNotFoundException, SQLException {
logger.info("Create DB connection");
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/prod","root","rootroot");
"jdbc:mysql://localhost:3306/prod?useUnicode=true"
+ "&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false"
+ "&serverTimezone=Europe/Paris","root","rootroot");
}

public void closeConnection(Connection con){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public class DBConstants {

public static final String SAVE_TICKET = "insert into ticket(PARKING_NUMBER, VEHICLE_REG_NUMBER, PRICE, IN_TIME, OUT_TIME) values(?,?,?,?,?)";
public static final String UPDATE_TICKET = "update ticket set PRICE=?, OUT_TIME=? where ID=?";
public static final String GET_TICKET = "select t.PARKING_NUMBER, t.ID, t.PRICE, t.IN_TIME, t.OUT_TIME, p.TYPE from ticket t,parking p where p.parking_number = t.parking_number and t.VEHICLE_REG_NUMBER=? order by t.IN_TIME limit 1";
public static final String GET_TICKET = "select t.PARKING_NUMBER, t.ID, t.PRICE, t.IN_TIME, t.OUT_TIME, p.TYPE from ticket t,parking p where p.parking_number = t.parking_number and t.VEHICLE_REG_NUMBER=? and t.OUT_TIME IS NULL order by t.IN_TIME limit 1";
}
17 changes: 17 additions & 0 deletions src/main/java/com/parkit/parkingsystem/dao/TicketDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public boolean saveTicket(Ticket ticket){
ps.setDouble(3, ticket.getPrice());
ps.setTimestamp(4, new Timestamp(ticket.getInTime().getTime()));
ps.setTimestamp(5, (ticket.getOutTime() == null)?null: (new Timestamp(ticket.getOutTime().getTime())) );

setIfReccurentUser(ticket);
return ps.execute();
}catch (Exception ex){
logger.error("Error fetching next available slot",ex);
Expand Down Expand Up @@ -58,6 +60,7 @@ public Ticket getTicket(String vehicleRegNumber) {
ticket.setPrice(rs.getDouble(3));
ticket.setInTime(rs.getTimestamp(4));
ticket.setOutTime(rs.getTimestamp(5));
setIfReccurentUser(ticket);
}
dataBaseConfig.closeResultSet(rs);
dataBaseConfig.closePreparedStatement(ps);
Expand Down Expand Up @@ -86,4 +89,18 @@ public boolean updateTicket(Ticket ticket) {
}
return false;
}

private void setIfReccurentUser(Ticket ticket) {
Connection con = null;
try {
con = dataBaseConfig.getConnection();
PreparedStatement ps = con.prepareStatement("Select * from ticket where VEHICLE_REG_NUMBER=\""+ticket.getVehicleRegNumber()+"\" and OUT_TIME IS NOT NULL");
ps.execute();
ticket.setRecuringMember(ps.getResultSet().next());
}catch (Exception ex){
logger.error("Error looking for reccurences",ex);
}finally {
dataBaseConfig.closeConnection(con);
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/parkit/parkingsystem/model/Ticket.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Ticket {
private int id;
private ParkingSpot parkingSpot;
private String vehicleRegNumber;
private boolean recuringMember;
private double price;
private Date inTime;
private Date outTime;
Expand Down Expand Up @@ -58,4 +59,12 @@ public Date getOutTime() {
public void setOutTime(Date outTime) {
this.outTime = outTime;
}

public void setRecuringMember(boolean recuringMember) {
this.recuringMember = recuringMember;
}

public boolean isRecuringMember() {
return this.recuringMember;
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,80 @@
package com.parkit.parkingsystem.service;

import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.parkit.parkingsystem.constants.Fare;
import com.parkit.parkingsystem.model.Ticket;

public class FareCalculatorService {

private static final Logger logger = LogManager.getLogger("FareCalculator");

public void calculateFare(Ticket ticket){
if( (ticket.getOutTime() == null) || (ticket.getOutTime().before(ticket.getInTime())) ){
if( (ticket.getOutTime() == null)){
logger.info("getInTime: " + ticket.getInTime());
throw new IllegalArgumentException("Out time provided is incorrect:"+ticket.getOutTime().toString());
}

double inHour = dateToHour(ticket.getInTime());
double outHour = dateToHour(ticket.getOutTime());

if( inHour > outHour ){
logger.info("getInTime: " + ticket.getInTime());
throw new IllegalArgumentException("Out time provided is incorrect:"+ticket.getOutTime().toString());
}

double duration = outHour - inHour;

logger.info("inHour : " + inHour + " | outHour : " + outHour + " | duration : " + duration);

int inHour = ticket.getInTime().getHours();
int outHour = ticket.getOutTime().getHours();

//TODO: Some tests are failing here. Need to check if this logic is correct
int duration = outHour - inHour;

switch (ticket.getParkingSpot().getParkingType()){
case CAR: {
ticket.setPrice(duration * Fare.CAR_RATE_PER_HOUR);
break;
}
case BIKE: {
ticket.setPrice(duration * Fare.BIKE_RATE_PER_HOUR);
break;
}
default: throw new IllegalArgumentException("Unkown Parking Type");
double price = 0;
if(duration > 0.5) {
price = duration;
switch (ticket.getParkingSpot().getParkingType()){
case CAR: {
price *= Fare.CAR_RATE_PER_HOUR;
break;
}
case BIKE: {
price *= Fare.BIKE_RATE_PER_HOUR;
break;
}
default: throw new IllegalArgumentException("Unkown Parking Type");
}
if(ticket.isRecuringMember())price *= 0.95;
}
ticket.setPrice(price);
}

/**@author Mo-BiuS
* @param d Date
* @return double representing the number of hour elapsed from the 1st January 2000.
*/
protected double dateToHour(Date d) {


double year = d.getYear()-101;
if(year % 4 == 0) year *= 8784;
else year *= 8760;

double month = 0;
for(int m = 1; m < d.getMonth(); m++) {
if(m == 2) {
if(d.getYear() % 4 == 0) month += 696;
else month += 672;
}
else if(m % 2 == 0) month += 720;
else month += 744;
}

double day = (d.getDate()-1)*24;

double hour = d.getHours();

double minute = ((double)d.getMinutes())/60;

return year + month + day + hour + minute;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void processIncomingVehicle() {
ticket.setOutTime(null);
ticketDAO.saveTicket(ticket);
System.out.println("Generated Ticket and saved in DB");
if(ticket.isRecuringMember()) System.out.println("Welcome back! As a recurring user of our parking lot, you'll benefit from a 5% discount");
System.out.println("Please park your vehicle in spot number:"+parkingSpot.getId());
System.out.println("Recorded in-time for vehicle number:"+vehicleRegNumber+" is:"+inTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,46 @@ public void calculateFareCarWithMoreThanADayParkingTime(){
assertEquals( (24 * Fare.CAR_RATE_PER_HOUR) , ticket.getPrice());
}

@Test
public void calculateFareBikeWithLessThanHalfAnHourParkingTime(){
Date inTime = new Date();
inTime.setTime( System.currentTimeMillis() - ( 20 * 60 * 1000) );//20 minutes parking time should give free parking fare
Date outTime = new Date();
ParkingSpot parkingSpot = new ParkingSpot(1, ParkingType.BIKE,false);

ticket.setInTime(inTime);
ticket.setOutTime(outTime);
ticket.setParkingSpot(parkingSpot);
fareCalculatorService.calculateFare(ticket);
assertEquals( 0 , ticket.getPrice() );
}

@Test
public void calculateFareCarWithLessThanHalfAnOurParkingTime(){
Date inTime = new Date();
inTime.setTime( System.currentTimeMillis() - ( 20 * 60 * 1000) );//20 minutes parking time should give free parking fare
Date outTime = new Date();
ParkingSpot parkingSpot = new ParkingSpot(1, ParkingType.CAR,false);

ticket.setInTime(inTime);
ticket.setOutTime(outTime);
ticket.setParkingSpot(parkingSpot);
fareCalculatorService.calculateFare(ticket);
assertEquals( 0 , ticket.getPrice());
}

@Test
public void calculateFareCarRecuringMember(){
Date inTime = new Date();
inTime.setTime( System.currentTimeMillis() - ( 60 * 60 * 1000) );//20 minutes parking time should give free parking fare
Date outTime = new Date();
ParkingSpot parkingSpot = new ParkingSpot(1, ParkingType.CAR,false);

ticket.setInTime(inTime);
ticket.setOutTime(outTime);
ticket.setParkingSpot(parkingSpot);
ticket.setRecuringMember(true);
fareCalculatorService.calculateFare(ticket);
assertEquals( 0.95 * Fare.CAR_RATE_PER_HOUR , ticket.getPrice());
}
}
Loading