tw-oocamp-201512 / the-code-of-you

使用你最喜欢的语言写一段代码来介绍你自己,提交在Issues里
1 stars 1 forks source link

Jin Sun Profile #5

Open sunjintw opened 8 years ago

sunjintw commented 8 years ago
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JinSunProfile {
    public static void main(String[] args) {
        int lineSize = 100;
        List<DocField> content = new ArrayList<>();
        TitleField titleField = new TitleField();
        titleField.setValue("Jin's Profile");
        DocField paragraph1 = new ParagraphField();
        paragraph1.setValue("Hello, I'm Jin Sun, a developer from LiveText C1 team. " +
                "I joined Thoughtworks on 9th November. I like Java, JavaScript, Html and SQL, I worked as a Java" +
                " Engineer almost three years.");
        DocField paragraph2 = new ParagraphField();
        paragraph2.setValue("In my spare time, I like swimming, playing video game and watching movies.");
        DocField paragraph3 = new ParagraphField();
        paragraph3.setValue("I'm very happy to join this training section with you, " +
                "I believe we will have a good time.");
        content.add(titleField);
        content.add(paragraph1);
        content.add(paragraph2);
        content.add(paragraph3);
        Document briefIntroduce = new Document();
        briefIntroduce.setContent(content);
        briefIntroduce.display(lineSize);
    }
}

interface Displayable {
    void display(int lineSize);
}

abstract class DocField implements Displayable {
    protected String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public abstract void display(int lineSize);
}

class Document implements Displayable {

    List<DocField> content;

    public List<DocField> getContent() {
        return content;
    }

    public void setContent(List<DocField> content) {
        this.content = content;
    }

    @Override
    public void display(int lineSize) {
        if (null != this.content) {
            for (DocField paragraph : content) {
                paragraph.display(lineSize);
            }
        }
    }
}

class ParagraphField extends DocField {
    @Override
    public void display(int lineSize) {
        Pattern pattern = Pattern.compile("\\G\\s*(.{1,"+lineSize+"})(?=\\s|$)",Pattern.DOTALL);
        Matcher matcher = pattern.matcher(value);
        while (matcher.find()){
            System.out.println(matcher.group(1));
        }
        System.out.println();
    }
}

class TitleField extends DocField {
    @Override
    public void display(int lineSize) {
        String startIndex = new String(new char[(lineSize-value.length())/2]).replace('\0',' ');
        System.out.println(startIndex+value);
    }
}
sunjintw commented 8 years ago
                                       Jin's Profile

Hello, I'm Jin Sun, a developer from LiveText C1 team. I joined Thoughtworks on 9th November. I like Java, JavaScript, Html and SQL, I worked as a Java Engineer almost three years.

In my spare time, I like swimming, playing video game and watching movies.

I'm very happy to join this training section with you, I believe we will have a good time.