Code

Refridgerator Fridgerfrator;
FoodItem groceries [];
int maxNGroceries;
int nGroceries;
float tallness;
float maxTallness = 30;

void setup(){
  smooth();
  noStroke();
  size(300,300);
  Fridgerfrator = new Refridgerator ();
}

void draw(){
  background(247,240,152);
  Fridgerfrator.render();
}

void keyPressed(){
  switch(key){
  case 'o':
    Fridgerfrator.openDoor();
    break;
  case 'c':
    Fridgerfrator.closeDoor();
    break;
  case '-':
    Fridgerfrator.removeItem();
    break;
  case '+':
    Fridgerfrator.addItem();
    break;
  }
}

class Refridgerator {
  color fridge = color(158,198,198);
  color fridgeLine = color(218,245,244);
  color fridgeInside = color (132,162,162);
  color ufo = color(196,247,152);
  int fridgeH = 200;
  int fridgeW = 100;
  int fridgeX = 50;
  int fridgeY = height-fridgeH;
  int shelfHeight=0;
  int shelfRestart = 0;
  float foodW = 15;
  float foodH = 15;
  int  nItems;
  boolean bDoorOpen = false;

  Refridgerator(){
    nItems = 6;
    bDoorOpen = false;

    maxNGroceries = 9;
    nGroceries= 5;

    groceries = new FoodItem[maxNGroceries];
    for (int i=0; i<maxNGroceries; i++){
      tallness = 10+ i*5;
      if (tallness > maxTallness){
        tallness = maxTallness;
      }
      groceries[i]=new FoodItem(tallness);
    }
  }

  void render(){
    fill (fridge);
    stroke (fridgeLine);
    strokeWeight (1);
    rect (fridgeX,fridgeY,fridgeW,fridgeH);
    if (bDoorOpen == false){
      fill (fridgeLine);
      rect (fridgeX+10,fridgeY+10, fridgeW-20,fridgeH-20);
      fill(fridge);
      rect (fridgeX+15, fridgeY+(fridgeH)-125, 5,100);
      rect (fridgeX+15, fridgeY+(fridgeH)-175, 5,50);
      rect (fridgeX+15, fridgeY+(fridgeH)-125, 70,2);
    }
    if(bDoorOpen == true){
      fill(fridgeInside);
      rect (fridgeX+10,fridgeY+10, fridgeW-20,fridgeH-20);
      fill(fridge);
      rect (fridgeX+fridgeW-10,fridgeY+10, fridgeW-20,fridgeH-20);
      fill (fridgeLine);
      for (int i=0; i<nItems; i++){
        if (nItems >=maxNGroceries){
          nItems = maxNGroceries;
        }
        float x = 70+i*25;
        float y = 150;
        groceries[i].draw(x,y);
      }
    }
  }

  boolean isDoorOpen(){
    return bDoorOpen;
  }

  void openDoor(){
    bDoorOpen = true;
  }

  void closeDoor(){
    bDoorOpen = false;
  }

  int getNItems(){
    return nItems;
  }

  void addItem(){
    if (bDoorOpen == true ){
      nItems ++;
    } 
    else {
      println ("Door must be open to add an item");
    }
  }

  void removeItem(){
    if (bDoorOpen == true && nItems >= 0){
      nItems --;
    } 
    else {
      println ("Door must be open to remove an item");
    }
  }

}

class FoodItem {
  float posx, posy;
  float tallness;
  color chroma;
  int type;

  FoodItem (float t){
    tallness = t;
    chroma = color(random(100,255), random(128,255), random(0,100));
    type = (int)random(0,3);
  }

  void draw (float x, float y){ 
    posx = x;
    posy = y;

    smooth();
    if (isMouseHovering()){
      strokeWeight(4);
    } 
    else {
      strokeWeight(1);
    }

    fill(chroma);
    switch(type){
    case 0:
      rect (posx-6, posy-tallness, 12,tallness);
      break;
    case 1:
      ellipse (posx, posy-tallness/2, 12,tallness);
      break;
    case 2:
      triangle (posx-6, posy, posx+6, posy, posx, posy-tallness);
      break;
    }
  }

  boolean isMouseHovering(){
    return (
    (mouseX > posx-6) &&
      (mouseX < posx+6) &&
      (mouseY < posy) &&
      (mouseY > posy-tallness));
  }
}

0901 - Arrays of Objects: The Fridge as Compound Object

Statement:To bolster your understanding of one last topic (manipulating arrays of Objects), we'll extend the complexity of the Refrigerator example from last week.

Previously, your Refrigerator just maintained a single piece of information -- one integer -- which indicated how many "items" were in the 'fridge. We're going to elaborate this information so that items are themselves Objects with various properties. When you're done with this exercise, your Refrigerator will now own an array of Items. To begin, consider the following (complete) program -- copy and paste this into Processing. More instructions follow after this code:

FoodItem groceries[]; // DECLARE THE ARRAY OF OBJECTS
int maxNGroceries;    // Will be used to keep the size of the array.
int nGroceries;       // Indicates how many are actually used/shown.

//=====================================================
void setup(){
  size(500,200);
  
  maxNGroceries = 10;
  nGroceries    = 5;
  
  // ALLOCATE MEMORY FOR THE ARRAY
  groceries = new FoodItem[maxNGroceries]; 
  for (int i=0; i < maxNGroceries; i++){
    // CREATE THE INDIVIDUAL ELEMENTS OF THE ARRAY
    float tallness = 10+i*5;
    groceries[i] = new FoodItem(tallness); 
  }
}

//=====================================================
void draw(){
  smooth();
  background(255);
  
  // DO SOMETHING WITH EACH ELEMENT OF THE ARRAY
  for (int i=0; i < nGroceries; i++){
    float x = 100+i*30;
    float y = 120;
    groceries[i].draw(x,y);
  }
}


//=====================================================
class FoodItem {
  
  float posx, posy;
  float tallness;
  color chroma;
  int   type;
  
  //-----------------------------------
  FoodItem (float t){
    tallness = t;
    chroma   = color(random(100,255), random(128,255), random(0,100));
    type     = (int)random(0,3);
  }
  
  //-----------------------------------
  void draw (float x, float y){
    posx = x;
    posy = y;
    
    stroke(0);
    if (isMouseHovering()){
      strokeWeight(4);
    } else {
      strokeWeight(1);
    }
    
    fill(chroma);
    switch(type){
      case 0: // RECT
        rect(posx-6,posy-tallness, 12,tallness); 
        break;
      case 1: // ELLIPSE
        ellipse(posx,posy-tallness/2, 12,tallness);
        break;
      case 2: // TRIANGLE
        triangle(posx-6,posy, posx+6,posy, posx,posy-tallness);
        break;
    }
  }
  
  //-----------------------------------
  boolean isMouseHovering(){
    return (
        (mouseX > posx-6) && 
        (mouseX < posx+6) &&
        (mouseY < posy) && 
        (mouseY > posy-tallness));
  }
}
As you can see, this draws a handful of FoodItems in a row. [For reasons that will become clear later, these happen to be sorted already from shortest to tallest. It didn't have to be that way (their heights could have been random), but in the next assignment that will become important.] For now, your task in Assignment 0901 is just to add an array of FoodItems to your Refrigerator. To be precise: change the way food is represented in your fridge, so that the single number you used previously is replaced (inside the definition of your Refrigerator class) with an array of FoodItems and their associated counters. You should be able to selectively copy and paste some of the code given to you above. When you're done, the keypresses should work as they did in your fridge from last week.

Stock your fridge with a half-dozen or so items when we first launch the program. Also, make sure you handle the situation in which the user asks to add one too many FoodItems....

One last thing. Even though the Refrigerator is a "compound object" (because it contains other Objects, namely FoodItems, as data fields), the definitions of the two different classes should be presented sequentially and not nested. Thus, your code will have the overall structure:
void setup(){
  //.....
}

void draw(){
  //.....
}

void keyPressed(){
  //.....
}

//-----------------------
class Refrigerator {
  // Definition of a Refrigerator...
  FoodItem groceries[]; 
  int nFoodItems;
}


class FoodItem {
  // Definition of a FoodItem...
}

hide statement