Time for action—Managing a fridge

We are going to create software to manage a fridge. We want to be able to add meals inside it, and to list what is in it. Ok, let's start!

  1. Create a folder that is going to hold your project's files.
  2. Create two folders inside it: a src folder and a bin folder.
  3. In your src folder, create a MyFridge folder. This way, we now have a MyFridge package.
  4. In the MyFridge package, create a Fridge.hx file with the following code inside it:
    package MyFridge;
    class Fridge
    {
    public static var meals = new List<Meals>();
    }
    
  5. This way, our fridge will hold a list of meals inside it. We can make this variable static because we will only have one fridge.
  6. Now, in the MyFridge package, create a file named Meal.hx and write the following code in it:
    package MyFridge;
    class Meal
    {
    public var name : String;
    public function new(f_name : String)
    {
    this.name = f_name;
    }
    }
    
  7. We now have a class Meal and its instances will have a name.
  8. We will now create a menu. In your src folder create a file named MyFridge.hx and write the following code in it:
    import neko.Lib;
    class MyFridge
    {
    public static function main() : Void
    {
    var choice;
    do
    {
    Lib.println("Main Menu");
    Lib.println("1) Add new meal");
    Lib.println("2) List meals in fridge");
    Lib.println("9) Exit");
    var choice = neko.io.File.stdin().readLine();
    switch(choice)
    {
    case "1":
    var nMeal = new Meal(neko.io.File.stdin().readLine()); //Create the meal and ask user its name
    MyFridge.Fridge.meals.add(nMeal);//Add the meal to list
    case "2":
    Lib.println("Meals in fridge:")
    for(meal in MyFridge.Fridge.meals)
    {
    Lib.println(meal.name);
    }
    }
    } while(choice != "9");
    }
    }
    
  9. Now, we have a fully functional menu and software that does what we want.

What just happened?

If you've read this chapter, you should be able to understand what you have been doing on your own. I'll just give you some hints on some lines.

  • Writing to the console: Here, we are writing to the console by using the neko.Lib.println function. It takes a String as a parameter and prints it on a new line.
  • Storing a list: In the Fridge class, we have a List of Meal. We haven't discussed the List class yet and you may be wondering what those < and > characters are. Don't worry, we will explain that later. Basically, it's just to tell the compiler that we will be storing objects of type Meal in the list.
  • Reading from the command line: Reading from the command line is done using neko.io.File.stdin().readLine(). This is a method that returns a String. Handling input and output will be discussed in more depth later.

Have a go hero—Throw Exceptions to prevent crashes

Now, create a class with a function taking two integers as parameters. This function will divide the first integer by the second one. As dividing by 0 is not possible, you will test if the second integer is equal to 0. If it is, throw an exception.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.173.53