lesson.java

来自「一个JAVA程序员的游戏」· Java 代码 · 共 200 行

JAVA
200
字号
/*
 * Lesson.java
 *
 * Created on 28. Januar 2007, 21:37
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.lesson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Lesson mantains a list of sections, which contain the entries that should be learned.
 * Note that the sections and the bot class referenced are part of the persisted
 * state of the lesson (e.g. in the XML based lsn format), while other settings 
 * (active sections, max number of entries) are not persisted here, but in the 
 * user preferences.
 * @author Pirx
 */
public class Lesson implements Iterable<Section> {
    
    private List<Section> sections = new ArrayList<Section>();

    private String name;
    private String description;
    private String botClass;
    private int maxEntries = Integer.MAX_VALUE;
    
    /**
     * Creates a new instance of Lesson
     * @param name The name of the Lesson
     * @param description A description of the lesson.
     */
    public Lesson(String name, String description) {
        this.setName(name);
        this.setDescription(description);
    }
    
    /**
     * Creates a new instance of Lesson
     */
    public Lesson() {
    }
    
    /**
     * Getter for the lesson name
     * @return The name of the lesson
     */
    public String getName() {
        return name;
    }
    
    /**
     * Getter for the lesson description
     * @return The description of the lesson
     */
    public String getDescription() {
        return description;
    }
    
    /**
     * Appends a new section to this lesson
     * @param section the section to be added
     */
    public void addSection(Section section) {
        sections.add(section);
    }
    
    /**
     * Implements the Iterable<Section> interface
     * @return The Iterator over all sections
     */
    public Iterator<Section> iterator() {
        return sections.iterator();
    }
    
    /**
     * Number of Sections
     * @return The number of sections in this lesson
     */
    public int size() {
        return sections.size();
    }
    
    /**
     * Getter for a section
     * @param index The index of the section
     * @return The section at the specified index
     */
    public Section getSection(int index) {
        return sections.get(index);
    }

    /**
     * Returns the entries of this lesson according to the current settings.
     * Only Entries from active sections are included. The list of entries
     * is shuffled and contains not more that getMaxEntries() entries.
     * @return A shuffled list of entries from the active sections, not more than getMaxEntries()
     */
    public List<Entry> getEntries() {
        //Level calls entries.remove(0), so LinkedList is better
        List<Entry> entries = new LinkedList<Entry>(); 
        for (Section section : sections) {
            if (section.isIncluded()) {
                entries.addAll(section.getEntries());
            }
        }
        Collections.shuffle(entries);
        return (entries.size() > maxEntries) 
                ? entries.subList(0,maxEntries) 
                : entries;
    }
    
    /**
     * The list of entries from all active sections, not shuffled.
     * @return The list of all entries from the active sections
     */
    public List<Entry> getAllSelectedEntries() {
        List<Entry> entries = new ArrayList<Entry>();
        for (Section section : sections) {
            if (section.isIncluded()) {
                entries.addAll(section.getEntries());
            }
        }
        return entries;
    }

    /**
     * The list of all entries, not shuffled, from active and inactive sections.
     * @return The list of all entries
     */
    public List<Entry> getAllEntries() {
        List<Entry> entries = new ArrayList<Entry>();
        for (Section section : sections) {
            entries.addAll(section.getEntries());
        }
        return entries;
    }

    /**
     * Getter for the qualified class name of the bot class that should be used with this lesson.
     * Currently the only available bot classes are kanjitori.graphics.bot.KanjiBot and
     * kanjitori.graphics.bot.FlagBot
     * @return The qualified name of the bot class
     */
    public String getBotClass() {
        return botClass;
    }

    /**
     * Setter for the qualified class name of the bot class that should be used with this lesson.
     * Currently the only available bot classes are kanjitori.graphics.bot.KanjiBot and
     * kanjitori.graphics.bot.FlagBot
     * @param botClass The qualified name of the bot class
     */
    public void setBotClass(String botClass) {
        this.botClass = botClass;
    }

    /**
     * Setter for the lesson name
     * @param name The name of the lesson
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Setter for the lesson description
     * @param description The description of this lesson
     */
    public void setDescription(String description) {
        this.description = description;
    }
    
    /**
     * Setter for the maxEntries property.
     * @param maxEntries The maximum number of entries
     * @see #getEntries
     */
    public void setMaxEntries(int maxEntries) {
        this.maxEntries = maxEntries;
    }
    
    /**
     * Getter for the maxEntries properties
     * @return The maximum number of entries
     * @see #getEntries
     */
    public int getMaxEntries() {
        return maxEntries;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?