Weihnachtsbaum

Aus Byte-Welt Wiki
Version vom 7. August 2013, 14:54 Uhr von Fastjack (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „Kategorie:Java Da viele immer mal wieder einen Weihnachtsbaum erstellen möchten, stelle ich mal meinen vor Wem es hilft, der kann sich gerne bedienen. Viel…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springenZur Suche springen


Da viele immer mal wieder einen Weihnachtsbaum erstellen möchten, stelle ich mal meinen vor Wem es hilft, der kann sich gerne bedienen.

Viel Spaß!

/*
 * ChristmasTreeTest.java
 *
 * 05.01.2012
 *
 * Copyright 2012 fastjack
 */
package blog.tree;
 
import org.junit.*;
 
import static org.junit.Assert.*;
 
public class ChristmasTreeTest {
    private static final String LINE_END = System.getProperty("line.separator");
 
    @Test(expected=IllegalArgumentException.class)
    public void failCreateWithNegativeHigh() {
        new ChristmasTree(-1);
    }
 
    @Test(expected=IllegalArgumentException.class)
    public void failCreateWithZeroHigh() {
        new ChristmasTree(0);
    }
 
    @Test
    public void shouldCreateWithHighOfOne() {
        StringBuilder expected = new StringBuilder();
        expected.append("*").append(LINE_END);
        expected.append("|").append(LINE_END);
        assertEquals("christmas tree was not correct!", expected.toString(), 
                new ChristmasTree(1).toString());
    }
 
    @Test
    public void shouldCreateWithHighOfTwo() {
        StringBuilder expected = new StringBuilder();
        expected.append(" * ").append(LINE_END);
        expected.append("***").append(LINE_END);
        expected.append(" | ").append(LINE_END);
        assertEquals("christmas tree was not correct!", expected.toString(), 
                new ChristmasTree(2).toString());
    }
 
    @Test
    public void shouldCreateWithHighOfThree() {
        StringBuilder expected = new StringBuilder();
        expected.append("  *  ").append(LINE_END);
        expected.append(" *** ").append(LINE_END);
        expected.append("*****").append(LINE_END);
        expected.append("  |  ").append(LINE_END);
        assertEquals("christmas tree was not correct!", expected.toString(), 
                new ChristmasTree(3).toString());
    }
 
    @Test
    public void shouldCreateWithHighOfFour() {
        StringBuilder expected = new StringBuilder();
        expected.append("   *   ").append(LINE_END);
        expected.append("  ***  ").append(LINE_END); 
        expected.append(" ***** ").append(LINE_END);
        expected.append("*******").append(LINE_END);
        expected.append("   |   ").append(LINE_END);
        assertEquals("christmas tree was not correct!", expected.toString(), 
                new ChristmasTree(4).toString());
    }
 
    @Test
    public void shouldCreateWithHighOfFive() {
        StringBuilder expected = new StringBuilder();
        expected.append("    *    ").append(LINE_END);
        expected.append("   ***   ").append(LINE_END); 
        expected.append("  *****  ").append(LINE_END);
        expected.append(" ******* ").append(LINE_END);
        expected.append("*********").append(LINE_END);
        expected.append("    |    ").append(LINE_END);
        assertEquals("christmas tree was not correct!", expected.toString(), 
                new ChristmasTree(5).toString());
    }
 
}

/*
 * ChristmasTree.java
 *
 * 05.01.2012
 *
 * Copyright 2012 fastjack
 */
package blog.tree;
 
import org.apache.commons.lang.*;
 
/**
 * This class defines a christmas tree. After construction the tree can represented as String 
 * via the toString() method.
 * 
 * <pre>
 * System.out.println(new ChristmasTree(3).toString());
 * </pre>
 * 
 * prints out a christmas tree with a height of three in form: 
 * 
 * <pre>
 *   *  
 *  *** 
 * *****
 *   |  
 * </pre>
 * 
 * the idea for defined height n is
 *  
 * <pre>
 *        // 2n-1 lines for body
 *   *    // for actual row (beginning at zero) r print out 2(r+1)-1 stars
 *  ***   // 
 *  |.|   // if actual column is in star printing area |.| including the | then print a star 
 *  |.|   // otherwise a space 
 * *****  //
 *   |    // and a separate line for the centered trunk
 * </pre>
 * 
 * @author fastjack
 */
public class ChristmasTree {
    private static final String TRUNK = "|";
    private static final String SPACE = " ";
    private static final String STAR = "*";
    private static final String LINE_END = System.getProperty("line.separator");
    private int height;
    private int maxTreeWidth;
    private int halfOfTreeWidth;
    
    /**
     * Creates a christmas tree for the defined height.
     * 
     * @param height the height of the tree must be greater than zero
     */
    public ChristmasTree(int height) {
        Validate.isTrue(height > 0, "height must be greater than zero!");
        this.height = height;
        maxTreeWidth = 2 * height - 1;
        halfOfTreeWidth = maxTreeWidth / 2;
    }
 
    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(makeBody());
        s.append(makeBottomLineWithTrunk());
        return s.toString();
    }
 
    private String makeBody() {
        StringBuilder lines = new StringBuilder();
        for(int row = 0; row < height; row ++) {
            int numberOfStarsInRow = getNumberOfStarsInRow(row);
            int startOfStarPrinting = getStartColumnToPrintStars(numberOfStarsInRow);
            int endOfStarPrinting = startOfStarPrinting + numberOfStarsInRow;
            lines.append(makeLine(startOfStarPrinting, endOfStarPrinting));
        }
        return lines.toString();
    }
 
    private int getNumberOfStarsInRow(int row) {
        return 2 * (row + 1) - 1;
    }
 
    private int getStartColumnToPrintStars(int numberOfStarsInRow) {
        return halfOfTreeWidth - (numberOfStarsInRow / 2);
    }
 
    private StringBuilder makeLine(int startOfStarPrinting, int endOfStarPrinting) {
        StringBuilder line = new StringBuilder();
        for(int column = 0; column < maxTreeWidth; column ++) {
            if (isStarToPrint(startOfStarPrinting, endOfStarPrinting, column)) {
                line.append(STAR);
            } else {
                line.append(SPACE);
            }
        }
        line.append(LINE_END);
        return line;
    }
 
    private boolean isStarToPrint(int startOfStarPrinting, int endOfStarPrinting, int column) {
        return column >= startOfStarPrinting && column < endOfStarPrinting;
    }
 
    private String makeBottomLineWithTrunk() {
        StringBuilder bottom = new StringBuilder();
        for(int column = 0; column < maxTreeWidth; column ++) {
            if (column == maxTreeWidth / 2) {
                bottom.append(TRUNK);
            } else {
                bottom.append(SPACE);
            }
        }
        bottom.append(LINE_END);
        return bottom.toString();
    }
    
}