fork download
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4.  
  5. class Coffee {
  6. private String name;
  7. private double pricePerKg;
  8. private double volumePerKg;
  9.  
  10. public Coffee(String name, double pricePerKg, double volumePerKg) {
  11. this.name = name;
  12. this.pricePerKg = pricePerKg;
  13. this.volumePerKg = volumePerKg;
  14. }
  15.  
  16. public String getName() {
  17. return name;
  18. }
  19.  
  20. public double getPricePerKg() {
  21. return pricePerKg;
  22. }
  23.  
  24. public double getVolumePerKg() {
  25. return volumePerKg;
  26. }
  27. }
  28.  
  29. class CoffeeVan {
  30. private double capacity;
  31. private List<Coffee> coffees;
  32.  
  33. public CoffeeVan(double capacity) {
  34. this.capacity = capacity;
  35. this.coffees = new ArrayList<>();
  36. }
  37.  
  38. public void addCoffee(Coffee coffee, double quantity) {
  39. coffees.add(coffee);
  40. }
  41.  
  42. public void sortCoffeesByValue() {
  43. coffees.sort(Comparator.comparingDouble((Coffee c) -> c.getPricePerKg() / c.getVolumePerKg()).reversed());
  44. }
  45.  
  46. public List<Coffee> findCoffeeByQuality(double minPricePerKg, double maxPricePerKg, double minVolumePerKg, double maxVolumePerKg) {
  47. List<Coffee> result = new ArrayList<>();
  48. for (Coffee coffee : coffees) {
  49. if (coffee.getPricePerKg() >= minPricePerKg && coffee.getPricePerKg() <= maxPricePerKg &&
  50. coffee.getVolumePerKg() >= minVolumePerKg && coffee.getVolumePerKg() <= maxVolumePerKg) {
  51. result.add(coffee);
  52. }
  53. }
  54. return result;
  55. }
  56. }
  57.  
  58. public class Main {
  59. public static void main(String[] args) {
  60. Coffee espressoBeans = new Coffee("Espresso Beans", 30, 0.5);
  61. Coffee arabicaGround = new Coffee("Arabica Ground", 20, 0.6);
  62. Coffee instantCoffee = new Coffee("Instant Coffee", 15, 0.7);
  63.  
  64. CoffeeVan coffeeVan = new CoffeeVan(100);
  65. coffeeVan.addCoffee(espressoBeans, 50);
  66. coffeeVan.addCoffee(arabicaGround, 30);
  67. coffeeVan.addCoffee(instantCoffee, 20);
  68.  
  69. coffeeVan.sortCoffeesByValue();
  70.  
  71. List<Coffee> resultCoffees = coffeeVan.findCoffeeByQuality(10, 25, 0.5, 0.7);
  72.  
  73. System.out.println("Найденный кофе:");
  74. for (Coffee coffee : resultCoffees) {
  75. System.out.println(coffee.getName() + " - Цена: " + coffee.getPricePerKg() + "$, Объем: " + coffee.getVolumePerKg() + " кг");
  76. }
  77. }
  78. }
Success #stdin #stdout 0.2s 58376KB
stdin
Standard input is empty
stdout
Найденный кофе:
Arabica Ground - Цена: 20.0$, Объем: 0.6 кг
Instant Coffee - Цена: 15.0$, Объем: 0.7 кг