Code

// i havent finished this yet

Refrigerator mainFridge;
Refrigerator mini;

void setup(){
 size(500,300);
mainFridge = new Refrigerator();
mini = null;
}

void draw(){
 background(200); 
mainFridge.draw(80,80,180,250);
if(mini !=null)
mini.draw(300,200,200,180);

}



void keyPressed(){
  switch(key){
  case 'o': 
    mainFridge.openDoor();
    break;
  case 'c':
    mainFridge.closeDoor();
    break;
  case '-':
    mainFridge.removeItem();
    break;
  case '+':
    mainFridge.addItem();
    break;
case 'f':
mini = mainFridge.makeSubcollection;
break;
case 'x':
int indexofItem = mainFridge.getIndexofItem();
if(indexofItem != -1){
 mainFridge.removethisItem(indexofItem); 
}
  break;
  case 'v':
  //i used code from the example here
  float tallness = 10 + random(35);
  color chroma = color(255,0,0);
  int type = (int)random(0,3);
  FoodItem F = newFoodItem(tallness,chroma,type);
  mainFridge.insertandsortItem(F);
  break;
  }
}

class Refrigerator{
  int nFoodItems;
  FoodItem foodItems[];
  boolean bDoorOpen;
  
  Refridgerator(){
   bDoorOpen = false;
  nFoodItems = 0;

foodItems = new FoodItem[10] ;
  }
  
  void draw(int x, int y, int w, int h){
   if (bDoorOpen == false){
    stroke(0);
    fill(255,255,240);
    rect(x,y,w,h);
    fill(255);
    rect(x+5,y+100, 5,60);
   } else{
    fill(200,200,200);
   rect(x,y,w,h);
  fill(255);
 rect(x+5, y+5, w-5,h-5);
for(int i=0; i<nFoodItems; i++){
 float foodx = x +25 +(i%5)*20;
 float foody = y +h - 10 - (50*(i/5));
 foodItems[i].render(foodx,foody);
} 
   }
  }
  
  boolean isDoorOpen(){
   return bDoorOpen; 
  }
  
  int getNFoodItems(){
   return nFoodItems; 
  }
  
  void openDoor(){
   bDoorOpen = true; 
  }
  
  void closeDoor(){
   bDoorOpen = false; 
  }
  
  Refrigerator makeSubcollection(){
  Refrigerator anotherFridge = new Refrigerator();
  outputFridge.openDoor();
  for(int i = 0; i<nFoodItems; i++){
    
   anotherFridge.addItem(foodItems[i]); 
  }
 
  
  return anotherFridge;
}
} 
  
  class FoodItem {

  float posx, posy;
  float tallness;
  color chroma;
  int   type;

  //-----------------------------------
  // Constructor for a FoodItem. 
  // All necessary variables are passed in.
  FoodItem (float tallness, color chroma, int type){
    this.tallness = tallness;
    this.chroma   = chroma;
    this.type     = type;
  }

  //-----------------------------------
  // How a FoodItem should draw itself.
  // Note that a position is passed in.
  void draw (float x, float y){
    posx = x;
    posy = y;

    smooth();
    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;
    }
    noSmooth();
  }

  //-----------------------------------
  // ACCESSOR METHODS.

  int getType(){
    return type;
  }

  float getTallness(){
    return tallness; 
  }

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

0905 - A Subcollection: Your Fridge makes a sub-Fridge

Statement:Hey, this is your last regular homework for the course. The most important aspect of this assignment is that it will form crucial rehearsal for the final exam.

Extend the functionality of your Refrigerator class with a "factory" method, which is able to produce a subcollection of the items in your fridge.

For a reasonable metaphor: Suppose you are going on a picnic which some friends who are strict circuphages. (That means they only eat circles.) Ask your fridge object to make a small sub-fridge (like a cooler) which only contains the circular FoodItems from your fridge.

Hints: you will need to make a factory method within the Refrigerator class that returns a Refrigerator (of course), and which takes as an argument some kind of "condition" parameter. That condition could be the "food type" (triangle, circle, square), or (alternatively) it could be the food size ("tallness"), etcetera. Your applet will also need to assign the result of this factory function to a second Refigerator object, and display it next to the main fridge.

For more information on how to do this, see the blog entry about factory methods" and the blog entry about sub-collections. All of the answers to doing this lie therein.

hide statement