-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-3-iterator.vala
70 lines (51 loc) · 1.72 KB
/
3-3-iterator.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Gee;
class RadioStation {
protected float frequency;
public RadioStation (float frequency) {
this.frequency = frequency;
}
public float get_frequency () {
return frequency;
}
}
class StationList : Object, Traversable<RadioStation>, Iterable<RadioStation> {
protected ArrayList<RadioStation> stations = new ArrayList<RadioStation> ();
public void add_station (RadioStation station) {
stations.add (station);
}
public bool remove_station (RadioStation to_remove) {
foreach (RadioStation station in stations) {
if (station.get_frequency () == to_remove.get_frequency ()) {
stations.remove (station);
return true;
}
}
return false;
}
public int count () {
return stations.size;
}
public Type element_type {
get { return typeof (RadioStation); }
}
public bool @foreach (Gee.ForallFunc<RadioStation> f) {
return iterator ().foreach (f);
}
public Iterator<RadioStation> iterator () {
return stations.iterator ();
}
}
public int main (string[] args) {
var station_list = new StationList ();
station_list.add_station (new RadioStation (89.0f));
station_list.add_station (new RadioStation (101.0f));
station_list.add_station (new RadioStation (102.0f));
station_list.add_station (new RadioStation (103.2f));
foreach (RadioStation r in station_list) {
print ("%f\n", r.get_frequency ());
}
print ("We have %d stations\n", station_list.count ());
station_list.remove_station (new RadioStation (89.0f));
print ("We have %d stations\n", station_list.count ());
return 0;
}