Решение Задачи на стримы от Java Gym Rat
This commit is contained in:
30
src/main/java/com/algos/gym_rat/Main.java
Normal file
30
src/main/java/com/algos/gym_rat/Main.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.algos.gym_rat;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Product product1 = new Product("Молоко", 10, 2.0);
|
||||||
|
Product product2 = new Product("Масло", 1, 10.0);
|
||||||
|
Product product3 = new Product("Сыр", 5, 2.0);
|
||||||
|
Product product4 = new Product("Хлеб", 3, 1.0);
|
||||||
|
Product product5 = new Product("Лаваш", 4, 5.0);
|
||||||
|
|
||||||
|
Order order1 = new Order("1", "Вася");
|
||||||
|
order1.setProducts(new ArrayList<>(List.of(product1, product2, product3, product4)));
|
||||||
|
|
||||||
|
System.out.println(OrderAnalysis.calculateOrderTotal(order1));
|
||||||
|
|
||||||
|
Order order2 = new Order("2", "Коля");
|
||||||
|
order2.setProducts(new ArrayList<>(List.of(product2, product4, product5)));
|
||||||
|
|
||||||
|
List<Order> orders = new ArrayList<>(List.of(order1, order2));
|
||||||
|
System.out.println(OrderAnalysis.getUniqueProducts(orders));
|
||||||
|
|
||||||
|
System.out.println(OrderAnalysis.findMostPopularProduct(orders, 3));
|
||||||
|
System.out.println(OrderAnalysis.findMostPopularProductNice(orders, 3));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,4 +6,17 @@ public class Order {
|
|||||||
String orderId;
|
String orderId;
|
||||||
String customerName;
|
String customerName;
|
||||||
List<Product> products;
|
List<Product> products;
|
||||||
|
|
||||||
|
public Order(String orderId, String customerName) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
this.customerName = customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Product> getProducts() {
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProducts(List<Product> products) {
|
||||||
|
this.products = products;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,63 @@
|
|||||||
package com.algos.gym_rat;
|
package com.algos.gym_rat;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Set;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class OrderAnalysis {
|
public class OrderAnalysis {
|
||||||
|
|
||||||
// Рассчитать полную стоимость заказа
|
// Рассчитать полную стоимость заказа
|
||||||
public static Double calculateOrderTotal(Order order) {
|
public static Double calculateOrderTotal(Order order) {
|
||||||
|
|
||||||
return 0.0;
|
return order
|
||||||
|
.getProducts()
|
||||||
|
.stream()
|
||||||
|
.mapToDouble(p -> p.getQuantity() * p.getPrice())
|
||||||
|
.sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Получить список наименований товаров из заказов
|
//Получить список наименований товаров из заказов
|
||||||
public static Set<String> getUniqueProducts(List<Order> orders) {
|
public static Set<String> getUniqueProducts(List<Order> orders) {
|
||||||
|
|
||||||
return null;
|
return orders
|
||||||
|
.stream()
|
||||||
|
.flatMap(o -> o.getProducts().stream())
|
||||||
|
.map(Product::getName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
//Вывести наименования N популярных товаров в порядке уменьшения популярности
|
//Вывести наименования N популярных товаров в порядке уменьшения популярности
|
||||||
//Популярный товар - товар, который больше остальных покупается
|
//Популярный товар - товар, который больше остальных покупается
|
||||||
public static List<String> findMostPopularProduct(List<Order> orders, int topN) {
|
public static List<String> findMostPopularProduct(List<Order> orders, int topN) {
|
||||||
|
|
||||||
return null;
|
Map<String, Integer> popularProducts = new HashMap<>();
|
||||||
|
for (Order order : orders) {
|
||||||
|
List<Product> products = order.getProducts();
|
||||||
|
for (Product product : products) {
|
||||||
|
popularProducts.merge(product.getName(), product.getQuantity(), Integer::sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return popularProducts
|
||||||
|
.entrySet()
|
||||||
|
.stream()
|
||||||
|
.sorted((e1, e2) -> {
|
||||||
|
return e2.getValue().compareTo(e1.getValue());
|
||||||
|
})
|
||||||
|
.limit(topN)
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
public static List<String> findMostPopularProductNice(List<Order> orders, int topN) {
|
||||||
|
|
||||||
|
return orders
|
||||||
|
.stream()
|
||||||
|
.flatMap(o -> o.getProducts().stream())
|
||||||
|
.collect(Collectors.groupingBy(Product::getName, Collectors.summingInt(Product::getQuantity)))
|
||||||
|
.entrySet()
|
||||||
|
.stream()
|
||||||
|
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
|
||||||
|
.limit(topN)
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,4 +5,22 @@ public class Product {
|
|||||||
String name;
|
String name;
|
||||||
Integer quantity;
|
Integer quantity;
|
||||||
Double price;
|
Double price;
|
||||||
|
|
||||||
|
public Product(String name, Integer quantity, Double price) {
|
||||||
|
this.name = name;
|
||||||
|
this.quantity = quantity;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getQuantity() {
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user