RGAlex

Salad.java

Jun 22nd, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Salad {
  4.  
  5.     private  String name;
  6.     private  List<Vegetable> products;
  7.  
  8.     public Salad(String name) {
  9.         this.name = name;
  10.         this.products = new ArrayList<>();
  11.     }
  12.  
  13.     public String getName() {
  14.         return name;
  15.     }
  16.  
  17.     public int getTotalCalories() {
  18.         return this.products.stream().mapToInt(Vegetable::getCalories).sum();
  19.     }
  20.  
  21.     public int getProductCount() {
  22.         return this.products.size();
  23.     }
  24.  
  25.     public void add(Vegetable product) {
  26.         this.products.add(product);
  27.     }
  28.  
  29.     @Override
  30.     public String toString() {
  31.         StringBuilder output = new StringBuilder(String.format("* Salad %s is %d calories and have %d products:",
  32.                 this.getName(), this.getTotalCalories(), this.getProductCount()));
  33.         output.append(System.lineSeparator());
  34.         this.products.forEach(e -> output.append(e).append(System.lineSeparator()));
  35.         return output.toString().trim();
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment