Home > Java > Fungsi Terbilang

Fungsi Terbilang

Berikut dibawah ini salah satu contoh program terbilang

 

package percobaan;

/**
*
* @author Yuniar Kurniawan
* @date 26 Desember 2010
*
*/

// Simpan file dengan Nama Terbilang.java

import java.io.IOException;

class NumericHelper
{
public static boolean cekNumber(String paramInput)
{
char [] karakter = paramInput.toCharArray();
for(int i=0;i<paramInput.length();i+=1)
{
if(!Character.isDigit(karakter[i]))
{
return false;
}
}

return true;
}
}

class BacaKeyboard
{
public static String bacaInputan()
{
int karakter;
String out = “”;
boolean selesai = false;

while(!selesai)
{
try
{
karakter = System.in.read();
if(karakter < 0 || (char)karakter == ‘\n’)
selesai = true;
else if((char)karakter != ‘\r’)
out = out + (char)karakter;
}
catch (IOException e)
{
System.err.println(“Ada kesalahan”);
selesai = true;
}
}

return out;
}
}

public class Terbilang
{
public static void main(String [] args)
{
System.out.print(“Input Bilangan¬† : “);
String nilaiInputan = BacaKeyboard.bacaInputan();

if(NumericHelper.cekNumber(nilaiInputan))
System.out.println(“Nilai Terbilang : ” + getProses(nilaiInputan).trim());
else
System.out.println(“Inputan Berupa String”);

}

private static String getProses(String paramInput)
{
StringBuilder out = new StringBuilder();
Integer panjang = paramInput.length();

switch (panjang)
{
case 1: out.append(getSatuan(paramInput));
break;
case 2: out.append(getPuluhan(paramInput));
break;
case 3: out.append(getRatusan(paramInput));
break;
case 4: out.append(getRibuan(paramInput));
break;
case 5: out.append(getPuluhanRibu(paramInput));
break;
case 6: out.append(getRatusanRibu(paramInput));
break;
case 7: out.append(getJuta(paramInput));
break;
default:out.append(getPuluhanJuta(paramInput));
break;
}

out.append(” Rupiah”);

return out.toString();
}

private static String getPuluhanJuta(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getPuluhan(paramInput.substring(0,2)));
out.append(” Juta”);
out.append(getRatusanRibu(paramInput.substring(2,8)));

return out.toString();
}

private static String getJuta(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getSatuan(paramInput.substring(0,1)));
out.append(” Juta”);
out.append(getRatusanRibu(paramInput.substring(1,7)));

return out.toString();
}

private static String getRatusanRibu(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getRatusan(paramInput.substring(0,3)));
out.append(” Ribu”);
out.append(getRatusan(paramInput.substring(3,6)));

return out.toString();
}

private static String getPuluhanRibu(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getPuluhan(paramInput.substring(0,2)));
out.append(” Ribu”);
out.append(getRatusan(paramInput.substring(2,5)));

return out.toString();
}

private static String getRibuan(String paramInput)
{
StringBuilder out = new StringBuilder();

if(paramInput.substring(0,1).equals(“1″))
{
out.append(” Seribu”);
out.append(getRatusan(paramInput.substring(1,4)));
}
else
{
out.append(paramInput.substring(0,1));
out.append(” Ribu”);
out.append(getRatusan(paramInput.substring(1,4)));
}

return out.toString();
}

private static String getRatusan(String paramInput)
{
StringBuilder out = new StringBuilder();

if(paramInput.substring(0,1).equals(“0″))
out.append(“”);
else if(paramInput.substring(0,1).equals(“1″))
out.append(” Seratus”);
else
{
out.append(getSatuan(paramInput.substring(0,1)));
out.append(” Ratus”);
}

if(paramInput.substring(1,2).equals(“0″))
out.append(getSatuan(paramInput.substring(2,3)));
else
out.append(getPuluhan(paramInput.substring(1,3)));

return out.toString();
}

private static String getPuluhan(String paramInput)
{
StringBuilder out = new StringBuilder();

if(paramInput.substring(0, 1).equals(“1″))
{
String tmp = paramInput.substring(1,2);
if(tmp.equals(“1″))
out.append(” Sebelas”);
else if(tmp.equals(“0″))
out.append(” Sepuluh”);
else
{
out.append(getSatuan(tmp));
out.append(” Belas”);
}
}
else
{
for(int i=1;i<=paramInput.length();i++)
{
if(i==paramInput.length())
out.append(getSatuan(paramInput.substring(i-1,i)));
else
{
out.append(getSatuan(paramInput.substring(i-1,i)));
out.append(” Puluh”);
}
}
}

return out.toString();
}

private static String getSatuan(String paramInput)
{
String out = “”;

if(paramInput.equals(“0″))
out = “”;
else if(paramInput.equals(“1″))
out = ” Satu”;
else if(paramInput.equals(“2″))
out = ” Dua”;
else if(paramInput.equals(“3″))
out = ” Tiga”;
else if(paramInput.equals(“4″))
out = ” Empat”;
else if(paramInput.equals(“5″))
out = ” Lima”;
else if(paramInput.equals(“6″))
out = ” Enam”;
else if(paramInput.equals(“7″))
out = ” Tujuh”;
else if(paramInput.equals(“8″))
out = ” Delapan”;
else
out = ” Sembilan”;

return out;
}

}

About these ads
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: