Java tutorial
package org.ESLM.Model; /* Copyright (C) 2015 Rmi Even, Eric Perlinski <remi.even@telecomnancy.net> <eric.perlinski@telecomnancy.net> This file is part of ESLM. ESLM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ESLM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.Iterator; import java.util.Observable; import org.json.JSONObject; public class ProtoLesson extends Observable implements Iterable<Instruction>, JSONSerializable { private String title; private ArrayList<Instruction> exercises; public ProtoLesson(String title) { this.title = title; this.exercises = new ArrayList<Instruction>(); } public int getNumberOfExercises() { return exercises.size(); } public void addExercise(Instruction exercise) { exercises.add(exercise); this.setChanged(); this.notifyObservers(); } public void addExercise(int exerciseNumber, Instruction exercise) { exercises.add(exerciseNumber - 1, exercise); this.setChanged(); this.notifyObservers(); } public void removeExercise(int exerciseNumber) { exercises.remove(exerciseNumber - 1); this.setChanged(); this.notifyObservers(); } public Instruction getExercise(int exerciseNumber) { return exercises.get(exerciseNumber - 1); } @Override public JSONObject toJSON() { JSONObject JSONProtoLesson = new JSONObject(); JSONProtoLesson.put("title", this.title); for (Instruction instruction : this.exercises) { JSONProtoLesson.append("exercises", instruction.toJSON()); } return JSONProtoLesson; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; this.setChanged(); this.notifyObservers(); } @Override public Iterator<Instruction> iterator() { return exercises.iterator(); } }