fork(2) download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class Icecream {
  5. private String name;
  6. private boolean hasChocolate;
  7. private double fatPercentage;
  8.  
  9. public Icecream(String name, boolean hasChocolate, double fatPercentage) {
  10. this.name = name;
  11. this.hasChocolate = hasChocolate;
  12. this.fatPercentage = fatPercentage;
  13. }
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18.  
  19. public boolean hasChocolate() {
  20. return hasChocolate;
  21. }
  22.  
  23. public double getFatPercentage() {
  24. return fatPercentage;
  25. }
  26.  
  27. public static void main(String[] args) {
  28. List<Icecream> icecreams = new ArrayList<>();
  29. icecreams.add(new Icecream("Vanilla", false, 10.0));
  30. icecreams.add(new Icecream("Chocolate", true, 15.0));
  31. icecreams.add(new Icecream("Strawberry", false, 8.0));
  32. icecreams.add(new Icecream("Mint", false, 12.0));
  33. icecreams.add(new Icecream("Chocolate Chip", true, 18.0));
  34.  
  35. double totalFatPercentage = 0;
  36. int chocolateCount = 0;
  37.  
  38. System.out.println("Информация о мороженом:");
  39. for (Icecream icecream : icecreams) {
  40. totalFatPercentage += icecream.getFatPercentage();
  41. if (icecream.hasChocolate()) {
  42. chocolateCount++;
  43. }
  44. System.out.println("Название: " + icecream.getName());
  45. System.out.println("Наличие шоколада: " + (icecream.hasChocolate() ? "Да" : "Нет"));
  46. System.out.println("Процент жирности: " + icecream.getFatPercentage());
  47. System.out.println("------------------------");
  48. }
  49. double averageFatPercentage = totalFatPercentage / icecreams.size();
  50.  
  51. System.out.println("Средний процент жирности: " + averageFatPercentage);
  52. System.out.println("Количество мороженого с шоколадом: " + chocolateCount);
  53. }
  54. }
  55.  
Success #stdin #stdout 0.16s 57820KB
stdin
Standard input is empty
stdout
Информация о мороженом:
Название: Vanilla
Наличие шоколада: Нет
Процент жирности: 10.0
------------------------
Название: Chocolate
Наличие шоколада: Да
Процент жирности: 15.0
------------------------
Название: Strawberry
Наличие шоколада: Нет
Процент жирности: 8.0
------------------------
Название: Mint
Наличие шоколада: Нет
Процент жирности: 12.0
------------------------
Название: Chocolate Chip
Наличие шоколада: Да
Процент жирности: 18.0
------------------------
Средний процент жирности: 12.6
Количество мороженого с шоколадом: 2