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