Taxi fare refactor (#2328)
This commit is contained in:
committed by
GitHub
parent
7f22e3dc35
commit
e8df7f8116
@@ -17,7 +17,7 @@ public class DataProducerController {
|
||||
return "Hello world";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/get-image")
|
||||
@GetMapping("/get-image")
|
||||
public @ResponseBody byte[] getImage() throws IOException {
|
||||
final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg");
|
||||
return IOUtils.toByteArray(in);
|
||||
|
||||
@@ -14,5 +14,4 @@ public class Application extends SpringBootServletInitializer {
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,11 +14,11 @@ import com.baeldung.web.log.util.RequestLoggingUtil;
|
||||
@Component
|
||||
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
|
||||
|
||||
Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String postData = null;
|
||||
String postData;
|
||||
HttpServletRequest requestCacheWrapperObject = null;
|
||||
try {
|
||||
// Uncomment to produce the stream closed issue
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
package com.baeldung.web.log.config;
|
||||
|
||||
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
|
||||
|
||||
@Configuration
|
||||
public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private TaxiFareRequestInterceptor taxiFareRequestInterceptor;
|
||||
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/");
|
||||
|
||||
@@ -14,8 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import com.baeldung.web.log.data.RateCard;
|
||||
import com.baeldung.web.log.data.TaxiRide;
|
||||
import com.baeldung.web.log.service.TaxiFareCalculatorService;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
@RestController
|
||||
public class TaxiFareController {
|
||||
|
||||
@Autowired
|
||||
@@ -23,15 +24,13 @@ public class TaxiFareController {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class);
|
||||
|
||||
@GetMapping(value = "/taxifare/get/")
|
||||
@ResponseBody
|
||||
@GetMapping("/taxifare/get/")
|
||||
public RateCard getTaxiFare() {
|
||||
LOGGER.debug("getTaxiFare() - START");
|
||||
return new RateCard();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/taxifare/calculate/")
|
||||
@ResponseBody
|
||||
@PostMapping("/taxifare/calculate/")
|
||||
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
|
||||
LOGGER.debug("calculateTaxiFare() - START");
|
||||
String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
|
||||
|
||||
@@ -4,25 +4,28 @@ public class RateCard {
|
||||
|
||||
private String nightSurcharge;
|
||||
private String ratePerMile;
|
||||
|
||||
public RateCard(){
|
||||
nightSurcharge="Extra $ 100";
|
||||
ratePerMile="$ 10 Per Mile";
|
||||
|
||||
public RateCard() {
|
||||
nightSurcharge = "Extra $ 100";
|
||||
ratePerMile = "$ 10 Per Mile";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getNightSurcharge() {
|
||||
return nightSurcharge;
|
||||
}
|
||||
|
||||
public void setNightSurcharge(String nightSurcharge) {
|
||||
this.nightSurcharge = nightSurcharge;
|
||||
}
|
||||
|
||||
public String getRatePerMile() {
|
||||
return ratePerMile;
|
||||
}
|
||||
|
||||
public void setRatePerMile(String ratePerMile) {
|
||||
this.ratePerMile = ratePerMile;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,14 +5,15 @@ public class TaxiRide {
|
||||
private Boolean isNightSurcharge;
|
||||
private Long distanceInMile;
|
||||
|
||||
public TaxiRide(){}
|
||||
|
||||
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){
|
||||
public TaxiRide() {
|
||||
}
|
||||
|
||||
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile) {
|
||||
this.isNightSurcharge = isNightSurcharge;
|
||||
this.distanceInMile = distanceInMile;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Boolean getIsNightSurcharge() {
|
||||
return isNightSurcharge;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
package com.baeldung.web.log.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.web.log.data.TaxiRide;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TaxiFareCalculatorService {
|
||||
|
||||
public String calculateFare(TaxiRide taxiRide) {
|
||||
Long fare = 0l;
|
||||
if (taxiRide.getIsNightSurcharge()) {
|
||||
fare = taxiRide.getDistanceInMile() * 10 + 100;
|
||||
} else {
|
||||
fare = taxiRide.getDistanceInMile() * 10;
|
||||
}
|
||||
return String.valueOf(fare);
|
||||
return String.valueOf((Long) (taxiRide.getIsNightSurcharge()
|
||||
? taxiRide.getDistanceInMile() * 10 + 100
|
||||
: taxiRide.getDistanceInMile() * 10));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,5 +12,4 @@ public class MainApplication extends WebMvcConfigurerAdapter {
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(MainApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.baeldung.repository;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.baeldung.web.dto.HeavyResource;
|
||||
import org.baeldung.web.dto.HeavyResourceAddressOnly;
|
||||
|
||||
public class HeavyResourceRepository {
|
||||
import java.util.Map;
|
||||
|
||||
public class HeavyResourceRepository {
|
||||
|
||||
public void save(HeavyResource heavyResource) {
|
||||
}
|
||||
@@ -21,6 +21,7 @@ public class HeavyResourceRepository {
|
||||
public void save(HeavyResource heavyResource, String id) {
|
||||
|
||||
}
|
||||
|
||||
public void save(HeavyResourceAddressOnly partialUpdate, String id) {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user