com.nkapps.billing.services.NumberServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.nkapps.billing.services.NumberServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nkapps.billing.services;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import org.springframework.stereotype.Service;

/**
 *
 * @author nuraddin
 */
@Service("numberService")
public class NumberServiceImpl implements NumberService {
    private final String[] hundredNamesRU = { "", " ?", " ?", " ?",
            " ?", " ??", " ??", " ??", " ??",
            " ??" };

    private final String[] tensNamesRU = { "", " ??", " ", " p",
            " ?p", " ???", " ???", " ???",
            " ???", " ??" };
    private final String[] numNamesRU = { "", " ", " ", " p", " p", " ?",
            " ?", " ?", " ?", " ?", " ??",
            " ", " ", " ",
            " ", " ?", " ?",
            " ?", " ?", " ?" };
    private final String[] tensNames = { "", " ", " ", " ", " ", " ?",
            " ", " ", " ??", " ?" };

    private final String[] numNames = { "", " ", " ", " ", " ", " ", " ",
            " ", " ?", " ", " ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ?",
            "  " };

    private String convertLessThanOneThousand(int number) {
        String soFar;

        if (number % 100 < 20) {
            soFar = numNamesRU[number % 100];
            number /= 100;
        } else {
            soFar = numNamesRU[number % 10];
            number /= 10;

            soFar = tensNamesRU[number % 10] + soFar;
            number /= 10;
        }
        if (number == 0)
            return soFar;
        return hundredNamesRU[number] + soFar;
    }

    @Override
    public String convertToText(Double number) {
        if (number == 0) {
            return "00";
        }

        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        String snumber = df.format(number.longValue());

        int billions = Integer.parseInt(snumber.substring(0, 3));
        int millions = Integer.parseInt(snumber.substring(3, 6));
        int hundredThousands = Integer.parseInt(snumber.substring(6, 9));
        int thousands = Integer.parseInt(snumber.substring(9, 12));

        String tradBillions;
        switch (billions) {
        case 0:
            tradBillions = "";
            break;
        default:
            tradBillions = convertLessThanOneThousand(billions) + "  ";
        }
        String result = tradBillions;

        String tradMillions;
        switch (millions) {
        case 0:
            tradMillions = "";
            break;
        default:
            tradMillions = convertLessThanOneThousand(millions) + "  ";
        }
        result = result + tradMillions;

        String tradHundredThousands;
        switch (hundredThousands) {
        case 0:
            tradHundredThousands = "";
            break;
        case 1:
            tradHundredThousands = "?? ";
            break;
        default:
            tradHundredThousands = convertLessThanOneThousand(hundredThousands) + " ??";
        }
        result = result + tradHundredThousands;

        String tradThousand;
        tradThousand = convertLessThanOneThousand(thousands);
        result = result + tradThousand;
        Double tiyin = number - number.intValue();
        // remove extra spaces!
        return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ") + " ? "
                + (tiyin.equals(0) ? "00" : Double.valueOf(tiyin * 100).intValue()) + " ";
    }

}