Skip to content

Instantly share code, notes, and snippets.

View GayanM's full-sized avatar

Gayan GayanM

View GitHub Profile
@GayanM
GayanM / ProductResource.java
Created September 21, 2022 07:36
Filter products by category and price range
@RequestMapping("")
public List<Product> filterProducts(@Param("category") final Optional<String> category,
@Param("lower-price-limit") final Optional<Double> lowerLimitPrice,
@RequestParam("upper-price-limit") final Optional<Double> upperLimitPrice ) {
return productRepos.findAll().stream().
filter(p -> {
if (category.isEmpty()) {
return true;
@GayanM
GayanM / OrderResource.java
Created September 25, 2022 04:09
Get orders contains given product category
@RequestMapping("")
public List<Order> getOrdersByProductCategory (@RequestParam("category") final Optional<String> category) {
return orderRepo.findAll().stream().
filter(o -> o.getProducts().stream().anyMatch(p -> {
if (category.isEmpty()) {
return true;
}
return category.get().equalsIgnoreCase(p.getCategory());
})).
map(o -> new Order(o.getId(), o.getOrderDate(), o.getDeliveryDate(), o.getStatus())).
@GayanM
GayanM / ProductResourceDiscount.java
Created September 25, 2022 04:16
Ex-3:Obtain a list of product with given category and then apply 10% discount
@RequestMapping("/discount")
public List<Product> applyDiscountOnCategory(@RequestParam("category") final Optional<String> category,
@RequestParam("discount") final Optional<Double> discount) {
return productRepos.findAll().stream().
filter(p -> {
if (category.isEmpty()) {
return true;
}
return category.get().equalsIgnoreCase(p.getCategory());
}).map(p -> {
@GayanM
GayanM / ProductsByCustomerTier.java
Created September 25, 2022 04:22
Ex-4: Obtain a list of products based on customer tier and date range
@RequestMapping("/customer-tier")
public List<Product> getProductsByCustomerTier (@RequestParam("tier") final Optional<Integer> tier,
@RequestParam("start-date") final Optional<String> startDate,
@RequestParam("end-date") final Optional<String> endDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return orderRepo.findAll().stream().
filter(o -> {
if (tier.isEmpty()) {
return true;
}
@GayanM
GayanM / CheapestProduct.java
Created September 25, 2022 04:28
Ex-5: Get the cheapest products by category
@RequestMapping("/cheapest")
public Optional<Product> getCheapestProductByCategory(@RequestParam("category") final Optional<String> category) {
return productRepos.findAll().stream().filter(p -> {
if (category.isEmpty()) {
return true;
}
return category.get().equalsIgnoreCase(p.getCategory());
}).min(Comparator.comparing(com.mycompany.functional.programming.exercises.models.Product::getPrice)).map(p ->
new Product(p.getId(), p.getName(), p.getPrice(), p.getCategory()));
}
@GayanM
GayanM / RecentOrders.java
Created September 25, 2022 10:10
Get the 3 most recent placed order
@RequestMapping("/recent")
public List<Order> getRecentOrders (@RequestParam("limit") final Optional<Long> limit) {
return orderRepo.findAll().stream().
sorted(Comparator.
comparing(com.mycompany.functional.programming.exercises.models.Order::getOrderDate).
reversed()).limit(limit.get()).map(o -> new Order(o.getId(), o.getOrderDate(),
o.getDeliveryDate(), o.getStatus())).collect(Collectors.toList());
}
@GayanM
GayanM / LogOrders.java
Created September 25, 2022 10:16
Get a list of orders which were ordered on 15-Mar-2021, log the order records to the console and then return its product list
@RequestMapping("/log-orders")
public List<Product> logOrdersOnGivenDateAndReturnProducts(@RequestParam("date") final Optional<String> date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return orderRepo.findAll().stream()
.filter(o -> o.getOrderDate().isEqual(LocalDate.parse(date.get(), formatter)))
.peek(o -> System.out.println(o.toString()))
.flatMap(o -> o.getProducts().stream())
.distinct().map(p -> new Product(p.getId(), p.getName(), p.getPrice(), p.getCategory()))
.collect(Collectors.toList());
@GayanM
GayanM / LumpSumOfOrders.java
Created September 25, 2022 10:21
Calculate total lump sum of all orders placed in Feb 2021:Answ=11995.36
@RequestMapping("/lump-sum")
public Double lumpSumOfOrders (@RequestParam("month") final int month) {
return orderRepo.findAll()
.stream()
.filter(o -> o.getOrderDate().compareTo(LocalDate.of(2021, month, 1)) >= 0)
.filter(o -> o.getOrderDate().compareTo(LocalDate.of(2021, month + 1, 1)) < 0)
.flatMap(o -> o.getProducts().stream())
.mapToDouble(p -> p.getPrice())
.sum();
@GayanM
GayanM / AvgPayment.java
Created September 25, 2022 10:25
Calculate order average payment placed on 14-Mar-2021:Answ=287.604
@RequestMapping("/avg-payment")
public Double getAvgPayment(@RequestParam("date") final Optional<String> date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return orderRepo.findAll().stream().filter(o -> o.getOrderDate().isEqual(LocalDate.
parse(date.get(), formatter))).flatMap(o -> o.getProducts().stream()).mapToDouble(p ->
p.getPrice()).average().getAsDouble();
}
@GayanM
GayanM / Statistics.java
Created September 25, 2022 10:28
Obtain a collection of statistic figures (i.e. sum, average, max, min, count) for all products of given category
@RequestMapping("/statistics")
public DoubleSummaryStatistics getStatistics(@RequestParam("category") final Optional<String> category) {
return productRepos.findAll().stream().filter(p -> category.get().equalsIgnoreCase(p.getCategory())).
mapToDouble(p -> p.getPrice()).summaryStatistics();
}