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!
src
folder and a bin
folder. src
folder, create a MyFridge
folder. This way, we now have a MyFridge
package. MyFridge
package, create a Fridge.hx
file with the following code inside it:package MyFridge; class Fridge { public static var meals = new List<Meals>(); }
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; } }
Meal
and its instances will have a name. 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"); } }
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.
neko.Lib.println
function. It takes a String
as a parameter and prints it on a new line. 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. neko.io.File.stdin().readLine()
. This is a method that returns a String. Handling input and output will be discussed in more depth later.3.128.173.53