Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
2
Indexable
Never
package com.bocodes.integer.division;

import java.util.List;

public class LongMethodFormat implements Formatter {
    @Override
    public String format(Result result) {
        List<Long> quotients = result.getQuotients();
        List<Long> remainders = result.getRemainders();

        StringBuilder sb = new StringBuilder();

        // Initial dividend and first quotient
        long dividend = result.getDividend();
        long quotient = quotients.get(0);
        sb.append(dividend).append(" | ").append(quotient).append("\n");

        // Iterate over the remaining quotients
        int size = quotients.size();
        for (int i = 1; i < size; i++) {
            // Calculate the new dividend
            long remainder = remainders.get(i - 1);
            dividend = remainder * 10 + quotients.get(i);

            // Append the dividend, quotient, and new line
            sb.append(createWhitespace(i))
                    .append(dividend)
                    .append(" | ").append(quotients.get(i))
                    .append("\n");
            sb.append(createWhitespace(i + 1))
                    .append("-")
                    .append(createLine(quotients.get(i)))
                    .append("\n");
        }

        return sb.toString();
    }

    private String createWhitespace(int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(" ");
        }
        return sb.toString();
    }

    private String createLine(long quotient) {
        StringBuilder sb = new StringBuilder();
        int digitCount = String.valueOf(quotient).length();
        for (int i = 0; i < digitCount; i++) {
            sb.append("-");
        }
        return sb.toString();
    }

    @Override
    public Formatter get(String formatType) {
        if (formatType.equalsIgnoreCase("longMethod")) {
            return new LongMethodFormat();
        }
        // Add more format types here if needed
        return null;
    }
}