Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Writing our Fridge class


Now, let's start writing our Fridge class.

Let's start of easily by just implementing it so that it has a list of storables in which to store our items:

package fridgeManager;

class Fridge
{
   public var storedItems : List<Storable>;
   
   public function new()
   {
      storedItems = new List<Storable>();
   }
   
   public function addItem(item : Storable)
   {
      storedItems.push(item);
      item.storedOn = Date.now();
   }
   
   public function removeItem(item : Storable)
   {
      storedItems.remove(item);
      item.storedOn = null;
   }
   
}

What just happened?

to manage items in the fridge: addItem and removeItem.

The addItem function will add the item to the list and update its storedOn property. The removeItem function will set storedOn to null as the object isn't stored anymore.