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

Toll-Fee-Calculator| SpringBoot #104

Open
wants to merge 23 commits into
base: master
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
96 changes: 96 additions & 0 deletions toll_fee_calc_app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.evolve-technology.161602</groupId>
<artifactId>toll_free_calc_app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring-version>5.3.19</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

</dependencies>
<build>
<plugins>

<!-- Need at least 2.22.0 to support JUnit 5 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolve_technology.calculator;

public class TollFeeCalculatorApplication {

public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.evolve_technology.calculator.exception;

import lombok.Data;

@Data
public class CustomErrorException extends RuntimeException {
private Object data = null;

public CustomErrorException() {
super();
}

public CustomErrorException(String message) {
super(message);
}

public CustomErrorException(String message, Object data) {
this(message);
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.evolve_technology.calculator.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

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

import lombok.Data;

@Data
public class TollConfiguration {
private static final Logger logger = LogManager
.getLogger(TollConfiguration.class);
private String year;
private List<String> dates;
private List<String> vehicles;
private List<String> months;

public TollConfiguration() {
try (InputStream input = TollConfiguration.class.getClassLoader()
.getResourceAsStream("application.properties")) {

Properties prop = new Properties();

if (input == null) {
logger.info("Sorry, unable to find application.properties");
return;
}

// load a properties file from class path, inside static method
prop.load(input);

// get the property value and print it out
if (prop.getProperty("dates") != null) {
setDates(Arrays.asList(prop.getProperty("dates").split(",")));
}

if (prop.getProperty("months") != null) {
setMonths(Arrays.asList(prop.getProperty("months").split(",")));
}

if (prop.getProperty("vehicles") != null) {
setVehicles(
Arrays.asList(prop.getProperty("vehicles").split(",")));
}

setYear(prop.getProperty("year"));

logger.info("dates :: {} " + getDates());
logger.info("months :: {} " + getMonths());
logger.info("vehicles :: {} " + getVehicles());
logger.info("year :: {} " + getYear());

} catch (IOException ex) {
ex.printStackTrace();
logger.error(ex.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.evolve_technology.calculator.service;

import java.time.LocalDateTime;
import java.util.List;

public interface TollFeeService {

public Integer getTollFee(final List<LocalDateTime> list, String vehicle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.evolve_technology.calculator.service;

import java.util.List;

public interface TollFreeDatesService {
public Boolean isTollFreeDate(String date);

public List<String> getTollFreeDates();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.evolve_technology.calculator.service;

import java.util.List;

public interface TollFreeVehicleService {
public List<String> getTollFreeVehicles();

public boolean isTollFreeVehicle(String vehicle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.evolve_technology.calculator.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

import com.evolve_technology.calculator.service.TollFeeService;
import com.evolve_technology.calculator.util.TollUtil;
import com.evolve_technology.calculator.validation.TollValidation;

public class TollFeeServiceImpl implements TollFeeService {

private static final Logger logger = LogManager
.getLogger(TollFeeServiceImpl.class);

TollUtil tollUtil;

private Map<String, Integer> tollMap = new HashMap<>();

public TollFeeServiceImpl(TollUtil tollUtil) {
this.tollUtil = tollUtil;
}

public Integer getTollFee(List<LocalDateTime> inputDates, String vehicle) {
TollValidation.validate(inputDates, vehicle);
logger.info(
"Inside getTollFee method :: inputDates = {} and vehicle = {}",
inputDates, vehicle);
for (LocalDateTime localDateTime : inputDates) {
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
LocalDate localDate = localDateTime.toLocalDate();
Integer newFee = tollUtil.tollCompute(vehicle, localDate, hour,
minute);
String key = localDate.toString() + ":" + hour;
if (!tollMap.containsKey(key)) {
tollMap.put(key, newFee);
} else {
if (newFee > tollMap.get(key))
tollMap.put(key, newFee);
}
}
return process();
}

public int process() {
logger.info("tollMap {} :: " + tollMap);
Map<LocalDate, Integer> map = new HashMap<>();
for (String key : tollMap.keySet()) {
LocalDate localDate = LocalDate.parse(key.split(":")[0]);
if (!map.containsKey(localDate)) {
map.putIfAbsent(localDate, tollMap.get(key));
} else {
int sum = tollMap.get(key) + map.get(localDate);
map.put(localDate, sum > 60 ? 60 : sum);
}
}
return map.values().stream()
.collect(Collectors.summingInt(Integer::intValue));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.evolve_technology.calculator.service.impl;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;

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

import com.evolve_technology.calculator.properties.TollConfiguration;
import com.evolve_technology.calculator.service.TollFreeDatesService;

public class TollFreeDatesServiceImpl implements TollFreeDatesService {
private static final Logger logger = LogManager
.getLogger(TollFreeDatesServiceImpl.class);

TollConfiguration tollConfiguration;

public TollFreeDatesServiceImpl(TollConfiguration tollConfiguration) {
this.tollConfiguration = tollConfiguration;
}

@Override
public Boolean isTollFreeDate(String date) {
logger.info("Inside isTollFreeDate method :: date = {} ", date);

LocalDate input = LocalDate.parse(date);
if (input.getDayOfWeek() == DayOfWeek.SATURDAY
|| input.getDayOfWeek() == DayOfWeek.SUNDAY) {
logger.info("date {} is actually a weekend so no toll Enjoy.",
date);
return true;
}

if (tollConfiguration.getMonths().stream()
.filter(month -> month.trim()
.equalsIgnoreCase(input.getMonth().toString()))
.findAny().isPresent()) {
logger.info("date {} lies in JULY month so no toll Enjoy.", date);
return true;
}

return getTollFreeDates().stream()
.filter(x -> x.trim().equalsIgnoreCase(input.toString()))
.findAny().isPresent();
}

@Override
public List<String> getTollFreeDates() {
logger.info("Inside getTollFreeDates method ");
return tollConfiguration.getDates();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.evolve_technology.calculator.service.impl;

import java.util.List;

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

import com.evolve_technology.calculator.properties.TollConfiguration;
import com.evolve_technology.calculator.service.TollFreeVehicleService;

public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService {

TollConfiguration tollConfiguration;

private static final Logger logger = LogManager
.getLogger(TollFreeVehiclesServiceImpl.class);

public TollFreeVehiclesServiceImpl(TollConfiguration tollConfiguration) {
this.tollConfiguration = tollConfiguration;
}

public List<String> getTollFreeVehicles() {
logger.info("Inside getTollFreeVehicles method ");
return tollConfiguration.getVehicles();
}

public boolean isTollFreeVehicle(String vehicle) {
logger.info("Inside isTollFreeVehicle method :: vehicle = {} ",
vehicle);
List<String> vehicles = getTollFreeVehicles();
return vehicles.stream().filter(v -> v.trim().equalsIgnoreCase(vehicle))
.findAny().isPresent();
}

}
Loading