package uebung08.doc;

public class Paragraph implements TextComponent {

    private String text;

    public Paragraph(String text) {
        this.text = text;
    }


    public int countWords() {
        int wordcount = 0;
        boolean isInWord = false;


        for (int i = 0; i < text.length(); i++) {
            if(Character.isAlphabetic(text.charAt(i))){
                if (!isInWord) {
                    wordcount++;
                    isInWord = true;
                }
            } else {
                isInWord = false;
            }
        }

        return wordcount;
    }

    public String getText(){
        return text;
    }

    @Override
    public <T> T accept(Visitor<T> visitor) { return visitor.visit(this); }
}
