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
|
class RadioStation { constructor(frequency) { this.frequency = frequency; }
getFrequency() { return this.frequency; } }
class StationList { constructor(){ this.index = -1; this.stations = []; }
get(i){ return this.stations[this.index]; }
hasNext(){ let index = this.index + 1; return this.stations[index] !== void 0; }
next(){ return this.stations[++this.index]; }
addStation(station) { this.stations.push(station); }
removeStation(toRemove) { const toRemoveFrequency = toRemove.getFrequency(); this.stations = this.stations.filter(station => station.getFrequency() !== toRemoveFrequency); } }
(function(){ const stationList = new StationList(); stationList.addStation(new RadioStation(89)); stationList.addStation(new RadioStation(101)); stationList.addStation(new RadioStation(102)); stationList.addStation(new RadioStation(103.2)); stationList.stations.forEach(station => console.log(station.getFrequency())); stationList.removeStation(new RadioStation(89)); while(stationList.hasNext()) console.log(stationList.next().getFrequency()); })();
|