1 year ago

#363475

test-img

HariHaravelan

How can i print two objects side by side in Java

I'm modeling a solution just to display (in console) the FreeCell card game. I have two classes Home and FreeCells. Both have overridden toString, it is a common implementation abstracted out to a static method.

Class Home

 public class Home {
    private final int MAX_CELLS = 4;
    private Stack<Card>[] homeCells;

    public Home() {
        this.suitToPlacements = new HashMap<>();
        this.homeCells = new Stack[MAX_CELLS];
    }


    public boolean addACard(Card card) {
        //some code
    }

    @Override
    public String toString() {
        return Layout.draw(Arrays.stream(this.homeCells).filter(Objects::nonNull).map(Stack::peek).collect(Collectors.toList()));
    }
   }

Class FreeCells

    public class FreeCells {
    private final int MAX_CELLS = 4;
    private List<Card> cells;

    public FreeCells() {
        this.cells = new ArrayList<>();
    }

    public boolean addCard(Card card) {
        //some code
    }

    @Override
    public String toString() {
        return Layout.draw(this.cells);
    }
}

Class Layout

public class Layout {
    private final static int MAX_CELLS = 4;


    public static String draw(List<Card> cards) {
        StringBuilder box = new StringBuilder();
        box.append(buildHorizontalBorder());
        box.append("\n");
        box.append(buildContent(cards));
        box.append("\n");
        box.append(buildHorizontalBorder());
        return box.toString();
    }


    private static String buildContent(List<Card> cards) {
        StringBuilder contentBox = new StringBuilder();
        contentBox.append("|");
        for (int index = 0; index < MAX_CELLS; index++) {
            contentBox.append(" ").append(index < cards.size() ? cards.get(index).toString() : "E ").append(" ").append("|");
        }
        return contentBox.toString();
    }

    private static String buildHorizontalBorder() {
        StringBuilder border = new StringBuilder();
        border.append("+");
        for (int i = 0; i < MAX_CELLS; i++) {
            border.append("----").append("+");
        }
        return border.toString();
    }
}

Class Grid

public class Grid {
private List<Card>[] columns;

public Grid(List<Card> cards) {
    this.columns = new List[8];
    this.shuffle(cards);
}

private void shuffle(List<Card> cards) {
    while (cards.size() > 0) {
        for (int i = 0; cards.size() > 0 && i < columns.length; i++) {
            Card randomCard = cards.remove(new Random().nextInt(cards.size()));
            if (columns[i] == null) {
                columns[i] = new ArrayList<>();
            }
            columns[i].add(randomCard);
        }
    }
}

@Override
public String toString() {
    int cardCount = 0;
    StringBuilder layOut = new StringBuilder();
    for (int i = 0; i < columns[0].size(); i++) {
        for (int j = 0; j < columns.length && cardCount < 52; j++) {
            layOut.append(columns[j].get(i).toString() + "\t");
            cardCount++;
        }
        layOut.append("\n");
    }
    return layOut.toString();
}
}

Class Board

public class Board {
    private FreeCells freeCells;
    private Home home;
    private Grid grid;

    public Board(FreeCells freeCells, Home home, Grid grid) {
        this.freeCells = freeCells;
        this.home = home;
        this.grid = grid;
    }

    public boolean canAddToHome(int columnNumber, int cardCount) {
        return false;
    }

    @Override
    public String toString() {
        StringBuilder boardLayout = new StringBuilder();
        boardLayout.append("Free Cell:\n").append(freeCells.toString()).append("\n").append("Home:\n").append(home.toString());
        boardLayout.append("\n");
        boardLayout.append(grid.toString());
        return boardLayout.toString();
    }
}

When I am printing the board.toString(), it looks like below, I have used newLine between the freeCells and the home objects in the board's toString, which results in them being printed one under the other.

Is it possible to print the freeCeels and home side by side, so that e.g. the headings are in one line as shown below?

Free Cells      Home
+---------+    +-------+
| ...     |    | ...   |
+---------+    +-------|

java

tostring

0 Answers

Your Answer

Accepted video resources