Code

Refridgerator Fridgerfrator;
FoodItem groceries [];
FoodItem table = new FoodItem(0);
int maxNGroceries;
int nGroceries;
float tallness;
float maxTallness = 70;
boolean isClicked = false;

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

  maxNGroceries = 9;
  nGroceries = Fridgerfrator.getNItems();

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

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;
  }

  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 drawGrocery=0; drawGrocery<nItems; drawGrocery++){

        if (nItems >=maxNGroceries){
          nItems = maxNGroceries;
        }
        float x = 70+25*(drawGrocery%3);
        float y = 150+ drawGrocery/3*70;
        groceries[drawGrocery].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 <= maxNGroceries - 1) {
      int newTallness = (int)random(0,50);
      groceries[nItems] = new FoodItem(newTallness);
      for (int sortX=1; sortX<nItems; sortX++){
        for (int sortY=1; sortY<nItems+1; sortY++){
          if (groceries[sortY].tallness < groceries[sortY-1].tallness){
            table = groceries[sortY-1];
            groceries[sortY-1] = groceries[sortY];
            groceries[sortY] = table;
          }
        }
      }

      nItems ++;

    } 
    else {
      println ("Door must be open to add an item");
    }
  }

  void removeItem(){
    if (bDoorOpen == true && nItems >= 0){
      nItems --;
      for (int moveGroceries = 4; moveGroceries <nItems+1; moveGroceries ++){

        groceries [moveGroceries-1] = groceries [moveGroceries];
      }
    } 
    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));
  }

}

0903 - Arrays of Objects: Keeping items sorted: Insertion

Statement:In this exercise, modify your previous example so that:

  • When the user presses the key to add an item to the fridge, a new FoodItem is created with a random height;
  • This new (randomly-sized) FoodItem is inserted into the array in the right place, so as to maintain the sorted order;
  • Pre-existing FoodItems which happen to be larger than the newly inserted FoodItem must be shifted down one place in the array (to the right);
  • If the array is full and a new item is inserted, decide whether to discard one of the items, or disallow the insertion...


hide statement