CurrencyToEng.java 7.21 KB
Newer Older
TongZuu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
/*
 * Created on 22 .. 2547
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
/**
 * @author KOM
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.csc.library.utilities;
import java.io.IOException;
import java.util.StringTokenizer;
public class CurrencyToEng {
	String curStr;
	int t1=0;
	// {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	private String[] NumRead = { "","One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
	private String[] teens = { "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
	private String[] tens = { "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
	private String[] SectionPos = { "", "Thousand", "Million", "Billion" };
	private String[] currency = { "Baht", "Satang" };
	private String afterDot;
	private String [] beforeDot,CurrencyStr ;
	private int prevPos;

	public CurrencyToEng() {
		super();
	}
	public CurrencyToEng(int amount) {
		curStr = (new Integer(amount)).toString();
		process();
	}
	public CurrencyToEng(String amount) {
		curStr = (new Integer(amount)).toString();
		process();
	}
	public CurrencyToEng(double amount) {
		curStr = (new Double(amount)).toString();
		process();
	}
	private void process() {
		String tempFullNumber="";
		if (curStr.indexOf(".") != -1) {
			if ((curStr.length() - curStr.indexOf(".")) < 3)
				curStr += "0";
			StringTokenizer token = new StringTokenizer(curStr, ".");
			tempFullNumber = token.nextToken();
			this.afterDot = token.nextToken();
		} else {
			tempFullNumber = curStr;
		}
		int arrSize = (tempFullNumber.length() % 3 == 0) ? 0 : 1;
		
		//System.out.println("tempFullnumber = "+tempFullNumber);
		
		this.beforeDot = new String[(tempFullNumber.length() / 3) + arrSize];
		
		this.CurrencyStr = new String[(tempFullNumber.length() / 3) + arrSize + 1];
		
		//System.out.println(tempFullNumber.length()+arrSize);
		
		int start = 0;
		int end = tempFullNumber.length() % 3;
		if (start == end)
			end = 3;
		String tmp;
		for (int i = 1; i <= beforeDot.length; i++) {
			beforeDot[i - 1] = tempFullNumber.substring(start, end);
			if (end == tempFullNumber.length()) {
				break;
			} else {
				start = end;
				end += 3;
			}
		}
		Convert();
	}
	
	private void Convert() {
		String test;
		boolean first = true;
		int counter = 0, SectionCounter = beforeDot.length;
		//System.out.println("SectionCounter = "+SectionCounter);
		for (int i = 1; i <= beforeDot.length; i++) {
			String sectionStr = "";
			String tempStr = beforeDot[i - 1];
			int tempposition = tempStr.length();
			
			//System.out.println(tempStr);
			
			for (int a = 1; a <= tempStr.length(); a++) {	
				String curStr = "" + tempStr.charAt(a-1);
				int tempCur = (new Integer(curStr)).intValue();
				--tempposition;
				sectionStr += position(tempposition, tempCur);
			}
			
			//System.out.println(findsection(tempStr,beforeDot,i));
			test=findsection(tempStr,beforeDot,i);
			if (test.equalsIgnoreCase("")){
				sectionStr += findsection(tempStr,beforeDot,i);
			}else sectionStr += findsection(tempStr,beforeDot,i)+checkand(i,beforeDot);
			//System.out.println(sectionStr);
			
			this.CurrencyStr[i - 1] = sectionStr;
		}
		if (afterDot != null) {
			String afterDotStr = "";
			int tempafterposition = afterDot.length();
			for (int j = 1; j <= afterDot.length(); j++) {
				String curStr = "" + afterDot.charAt(j - 1); 
				int tempCur = (new Integer(curStr)).intValue();
				--tempafterposition;
				afterDotStr += position(tempafterposition, tempCur);
			}
			this.CurrencyStr[this.CurrencyStr.length - 1] = afterDotStr;
		}
	}
	
	//=====process section===============
	private String findsection(String tempStr,String[] beforeDot,int i){
		int sum = 0;
		String result="";
		if (!(tempStr.equalsIgnoreCase("000")|| tempStr.equalsIgnoreCase("00"))){
			if (!(tempStr.equalsIgnoreCase("0"))){
				for(int x=i+1;i<beforeDot.length;i++)  ++sum;
				if (sum!=0) result +=  " "+SectionPos[sum]+" ";
			}
		}
	   return  result;
	}
	
	//===================================
	private String checkand(int i,String[] beforeDot){
		String temp;
		for (int x = i ;x < beforeDot.length;x++){
			temp=beforeDot[x];
			if (!temp.equalsIgnoreCase("000")) return " And ";
		}
		return "";
	}
	private String position(int nPosition, int nCurrency ) {
		String rtnStr = "";
		switch (nPosition) {
			/******************
			 *      999
			 *      ^
			 ******************/
			
			case 0 : //unit position
				if (t1==1){
						if (nCurrency==0){
							rtnStr = tens[nCurrency];
						}
						else rtnStr = teens[nCurrency-1];
						
						t1=0;
						
					}
					else{
						rtnStr = NumRead[nCurrency];
				}
				
				this.prevPos = nCurrency;
				break;
				/******************
				 *      999
				 *       ^
				 ******************/
			case 1 : //tens position
				if (nCurrency != 0) {
					if (nCurrency == 1){
						t1 = 1;
					}
					else rtnStr = tens[nCurrency-1];
				}
				this.prevPos = nCurrency;
				break;
			
				/******************
				 *      999
				 *        ^
				 ******************/
			case 2 : //Hundred
				rtnStr = NumRead[nCurrency];
				if (nCurrency!=0) rtnStr += " Hundred ";
				this.prevPos = nCurrency;
				break;
			default : //others position
				if (nCurrency == 0) {
					//10, 20, 30, .... , 80, 90
					if (this.prevPos != 0)
						rtnStr = tens[prevPos - 1];
				} else {
					if (this.prevPos == 0) {
						//01, 02, 03, ... , 08, 09
						rtnStr = NumRead[nCurrency];
					} else {
						rtnStr = tens[prevPos - 1] + NumRead[nCurrency];
					}
				}
		}
		return rtnStr;
	}
	public String getString() {
		int sCounter = this.CurrencyStr.length - 1;
		String out = new String();
		for (int i = 1; i <= this.CurrencyStr.length; i++) {
			if (this.CurrencyStr[i-1]==null) {
				this.CurrencyStr[i-1]="";
			}
			out += this.CurrencyStr[i - 1];
			if (i == this.CurrencyStr.length) {
				//*********************
				if (afterDot != null){
					if((this.afterDot!=null)&&(!this.afterDot.equals(""))&&(Integer.parseInt(this.afterDot)>0))
					out += " " + this.currency[1] + " ";//write Satang
				}
				//*********************
				} else if (i == this.CurrencyStr.length - 1) {
							out += " " + this.currency[0] + " ";//write Bath
						}	
		}
		return out;
	}
	protected String getValue(String value){ 
		this.curStr = value;
		this.process();
		return this.getString();
	}
	public String getString(int amount) {
		return ((amount==0)?"Zero Bath":this.getValue(String.valueOf(amount)));
	}
	public String getString(double amount) {
		return ((amount==0)?"Zero Bath":this.getValue(String.valueOf(amount)));
	}
	public String getString(String amount) {
		return this.getValue(String.valueOf(amount));
	}
	public static void main(String[] args) throws IOException {
		CurrencyToEng cur = new CurrencyToEng();
		double d = 0;
		int i=0;
		System.out.println(cur.getString("11127.50"));     
		System.out.println(cur.getString("27.50"));      
	}
}