First NIT release and new clean mercurial repository
[nit.git] / tests / example_beer.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # 99 bottles of beer
18 # Just for fun
19
20 class Bottle
21 meth sing is abstract
22 private
23 meth sing_start
24 do
25 printn(self, " of beer on the wall.\n")
26 printn(self, " of beer.\n")
27 printn("Take it down, pass it around.\n")
28 end
29 end
30
31 class OneBottle
32 special Bottle
33 redef meth sing
34 do
35 sing_start
36 printn("No more bottles of appear on the wall!\n\n")
37 end
38
39 redef meth to_s: String
40 do
41 return "One bottle"
42 end
43
44 init do end
45 end
46
47 class TwoBottles
48 special Bottle
49 redef meth sing
50 do
51 sing_start
52 printn("One bottle appears on the wall\n\n")
53 end
54
55 redef meth to_s: String
56 do
57 return "2 bottles"
58 end
59
60 init do end
61 end
62
63 class Bottles
64 special Bottle
65
66 redef meth sing
67 do
68 sing_start
69 printn(_quantity - 1," bottles appear on the wall!\n\n")
70 end
71
72 redef meth to_s: String
73 do
74 return _quantity.to_s + " bottles"
75 end
76
77 init(i: Int)
78 do
79 _quantity = i
80 end
81
82 attr _quantity: Int
83 end
84
85 var i = 99
86 while i > 2 do
87 (new Bottles(i)).sing
88 i = i - 1
89 end
90 (new TwoBottles).sing
91 (new OneBottle).sing
92