Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.7 kB
2
Indexable
Never
package model;

import java.util.ArrayList;
import java.util.List;

// Represents an apple basket having many apples
public class AppleBasket {
  // Internal ID: QS1N7YFF (ignore this comment)

  private List<Apple> allApples;
  private List<Apple> oldApples;

  // EFFECTS: constructs an empty apple basket maintaining a list of all apples
  public AppleBasket() {
    this.allApples = new ArrayList<>();
    this.oldApples = new ArrayList<>();
  }

  // MODIFIES: this
  // EFFECTS: adds the given apple to this apple basket
  public void addApple(Apple apple) {
    this.allApples.add(apple);
  }

  // EFFECTS: returns a list of all apples in this apple basket that are
  //  considered "old", in the order in which they were added to the basket.
  public List<Apple> getOldApples() {
    // for (int i = 0 ; i < this.allApples.size(); i++) {

    //   Apple apple = this.allApples.get(i);

    //   if (isAppleOld(apple)) {
    //       this.oldApples.add(i, apple);
    //   }

    // }

    //exam implementation 

    List<Apple> oldApples1 = new ArrayList<>();

    for (Apple apple: allApples) {
      if (isAppleOld(apple)){
        oldApples1.add(apple);
      }
    }

    return oldApples1;


  }

  // EFFECTS: returns the total number of apples in this basket
  public int getTotalNumberOfApples() {
    return allApples.size() + oldApples.size();
  }

  // EFFECTS: determines whether the given apple is considered "old"
  // if age is 10 and threshold is same as age, is is alr old
  private boolean isAppleOld(Apple apple) {
    return apple.getAge() >= apple.getAgeThreshold();
  }
}
Leave a Comment