Wednesday, October 31, 2012

Konversi Bilangan ke Terbilang menggunakan Switch-Case JAVA

 /**
 *
 * @author ERFAN FEBRIANTORO, NIM.1210652040
 * @campuss TEKNIK INFORMATIKA - UNMUH JEMBER
 */
import javax.swing.JOptionPane;
public class Konversi_Angka_ke_Terbilang {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String angka, input, welcome, ulangi, warning, bye, terbilang,
                    terbilang1 = "", terbilang2 = "", terbilang3 = "", terbilang4 = "", terbilang5 = "",
                    terbilang6 = "", terbilang7 = "", terbilang8 = "", terbilang9 = "";
            welcome = "Selamat Datang."
                    + "\nAnda sedang menjalankan Aplikasi JAVA Sederhana"
                    + "\nMengkonversi Angka Menjadi Terbilang."
                    + "\nPresented by :"
                    + "\nerfan2040.blogspot.com (c) 2012";
            input = "Masukkan angka yang Anda inginkan :";
            bye = "Terima kasih telah mencoba Aplikasi ini."
                    + "\nJika Anda suka,"
                    + "\nDon't forget to LIKE this post or SHARE it."
                    + "\nPresented by :"
                    + "\nerfan2040.blogspot.com (c) 2012";
            terbilang = null;
            ulangi = "Maaf, Aplikasi ini hanya mensupport bilangan antara"
                    + "\n 0 (Nol) - 1.000.000.000 (Satu Milyar)."
                    + "\nULANGI LAGI . . .";
            warning = "Maaf, data yang Anda inputkan SALAH atau BUKAN ANGKA."
                    + "\nTERIMA KASIH.";
          
        try {
            double a, b, c, d, e, f, g, h, i, j, angkaMu;
          
            int ratusJt, puluhJt, satJt, ratusRb, puluhRb, satRb, ratus, puluh, sat, hasil;
          
            JOptionPane.showMessageDialog(null, welcome);

            do {
                angka = JOptionPane.showInputDialog(null, input);
                angkaMu = Double.parseDouble(angka);
                if ((angkaMu > 0) & (angkaMu <= 999999999)) {
                    a = angkaMu % 10;
                    b = angkaMu % 100;
                    c = angkaMu % 1000;
                    d = angkaMu % 10000;
                    e = angkaMu % 100000;
                    f = angkaMu % 1000000;
                    g = angkaMu % 10000000;
                    h = angkaMu % 100000000;

                    sat = (int) a;
                    puluh = (int) ((b - a) / 10);
                    ratus = (int) ((c - b) / 100);
                    satRb = (int) ((d - c) / 1000);
                    puluhRb = (int) ((e - d) / 10000);
                    ratusRb = (int) ((f - e) / 100000);
                    satJt = (int) ((g - f) / 1000000);
                    puluhJt = (int) ((h - g) / 10000000);
                    ratusJt = (int) ((angkaMu - h) / 100000000);
                    hasil = (int) angkaMu;

                    //Perhitungan pada wilayah Ratusan Juta :
                    switch (ratusJt) {
                        case 1:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Seratus Juta ";
                            } else {
                                terbilang1 = "Seratus ";
                            }
                            break;
                        case 2:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Dua Ratus Juta ";
                            } else {
                                terbilang1 = "Dua Ratus ";
                            }
                            break;
                        case 3:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Tiga Ratus Juta ";
                            } else {
                                terbilang1 = "Tiga Ratus ";
                            }
                            break;
                        case 4:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Empat Ratus Juta ";
                            } else {
                                terbilang1 = "Empat Ratus ";
                            }
                            break;
                        case 5:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Lima Ratus Juta ";
                            } else {
                                terbilang1 = "Lima Ratus ";
                            }
                            break;
                        case 6:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Enam Ratus Juta ";
                            } else {
                                terbilang1 = "Enam Ratus ";
                            }
                            break;
                        case 7:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Tujuh Ratus Juta ";
                            } else {
                                terbilang1 = "Tujuh Ratus ";
                            }
                            break;
                        case 8:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Delapan Ratus Juta ";
                            } else {
                                terbilang1 = "Delapan Ratus ";
                            }
                            break;
                        case 9:
                            if ((puluhJt == 0) & (satJt == 0)) {
                                terbilang1 = "Sembilan Ratus Juta ";
                            } else {
                                terbilang1 = "Sembilan Ratus ";
                            }
                            break;
                    }

                    //Perhitungan pada wilayah Puluhan Juta :
                    switch (puluhJt) {
                        case 1:
                            if (satJt == 0) {
                                terbilang2 = "Sepuluh Juta ";
                            } else if (satJt == 1) {
                                terbilang2 = "Sebelas Juta ";
                            }
                            break;
                        case 2:
                            if (satJt == 0) {
                                terbilang2 = "Dua Puluh Juta ";
                            } else {
                                terbilang2 = "Dua Puluh ";
                            }
                            break;
                        case 3:
                            if (satJt == 0) {
                                terbilang2 = "Tiga Puluh Juta ";
                            } else {
                                terbilang2 = "Tiga Puluh ";
                            }
                            break;
                        case 4:
                            if (satJt == 0) {
                                terbilang2 = "Empat Puluh Juta ";
                            } else {
                                terbilang2 = "Empat Puluh ";
                            }
                            break;
                        case 5:
                            if (satJt == 0) {
                                terbilang2 = "Lima Puluh Juta ";
                            } else {
                                terbilang2 = "Lima Puluh ";
                            }
                            break;
                        case 6:
                            if (satJt == 0) {
                                terbilang2 = "Enam Puluh Juta ";
                            } else {
                                terbilang2 = "Enam Puluh ";
                            }
                            break;
                        case 7:
                            if (satJt == 0) {
                                terbilang2 = "Tujuh Puluh Juta ";
                            } else {
                                terbilang2 = "Tujuh Puluh ";
                            }
                            break;
                        case 8:
                            if (satJt == 0) {
                                terbilang2 = "Delapan Puluh Juta ";
                            } else {
                                terbilang2 = "Delapan Puluh ";
                            }
                            break;
                        case 9:
                            if (satJt == 0) {
                                terbilang2 = "Sembilan Puluh Juta ";
                            } else {
                                terbilang2 = "Sembilan Puluh ";
                            }
                            break;
                    }

                    //Perhitungan pada wilayah Satuan Juta :
                    switch (satJt) {
                        case 2:
                            if (puluhJt == 1) {
                                terbilang3 = "Dua Belas Juta ";
                            } else {
                                terbilang3 = "Dua Juta ";
                            }
                            break;
                        case 3:
                            if (puluhJt == 1) {
                                terbilang3 = "Tiga Belas Juta ";
                            } else {
                                terbilang3 = "Tiga Juta ";
                            }
                            break;
                        case 4:
                            if (puluhJt == 1) {
                                terbilang3 = "Empat Belas Juta ";
                            } else {
                                terbilang3 = "Empat Juta ";
                            }
                            break;
                        case 5:
                            if (puluhJt == 1) {
                                terbilang3 = "Lima Belas Juta ";
                            } else {
                                terbilang3 = "Lima Juta ";
                            }
                            break;
                        case 6:
                            if (puluhJt == 1) {
                                terbilang3 = "Enam Belas Juta ";
                            } else {
                                terbilang3 = "Enam Juta ";
                            }
                            break;
                        case 7:
                            if (puluhJt == 1) {
                                terbilang3 = "Tujuh Belas Juta ";
                            } else {
                                terbilang3 = "Tujuh Juta ";
                            }
                            break;
                        case 8:
                            if (puluhJt == 1) {
                                terbilang3 = "Delapan Belas Juta ";
                            } else {
                                terbilang3 = "Delapan Juta ";
                            }
                            break;
                        case 9:
                            if (puluhJt == 1) {
                                terbilang3 = "Sembilan Belas Juta ";
                            } else {
                                terbilang3 = "Sembilan Juta ";
                            }
                            break;
                    }

                    //Perhitungan pada wilayah Ratusan Ribu :
                    switch (ratusRb) {
                        case 1:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Seratus Ribu ";
                            } else {
                                terbilang4 = "Seratus ";
                            }
                            break;
                        case 2:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Dua Ratus Ribu ";
                            } else {
                                terbilang4 = "Dua Ratus ";
                            }
                            break;
                        case 3:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Tiga Ratus Ribu ";
                            } else {
                                terbilang4 = "Tiga Ratus ";
                            }
                            break;
                        case 4:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Empat Ratus Ribu ";
                            } else {
                                terbilang4 = "Empat Ratus ";
                            }
                            break;
                        case 5:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Lima Ratus Ribu ";
                            } else {
                                terbilang4 = "Lima Ratus ";
                            }
                            break;
                        case 6:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Enam Ratus Ribu ";
                            } else {
                                terbilang4 = "Enam Ratus ";
                            }
                            break;
                        case 7:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Tujuh Ratus Ribu ";
                            } else {
                                terbilang4 = "Tujuh Ratus ";
                            }
                            break;
                        case 8:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Delapan Ratus Ribu ";
                            } else {
                                terbilang4 = "Delapan Ratus ";
                            }
                            break;
                        case 9:
                            if ((puluhRb == 0) & (satRb == 0)) {
                                terbilang4 = "Sembilan Ratus Ribu ";
                            } else {
                                terbilang4 = "Sembilan Ratus ";
                            }
                            break;
                    }

                    //Perhitungan pada wilayah Puluhan Ribu :
                    switch (puluhRb) {
                        case 1:
                            if (satRb == 0) {
                                terbilang5 = "Sepuluh Ribu ";
                            } else if (satJt == 1) {
                                terbilang5 = "Sebelas Ribu ";
                            }
                            break;
                        case 2:
                            if (satRb == 0) {
                                terbilang5 = "Dua Puluh Ribu ";
                            } else {
                                terbilang5 = "Dua Puluh ";
                            }
                            break;
                        case 3:
                            if (satRb == 0) {
                                terbilang5 = "Tiga Puluh Ribu ";
                            } else {
                                terbilang5 = "Tiga Puluh ";
                            }
                            break;
                        case 4:
                            if (satRb == 0) {
                                terbilang5 = "Empat Puluh Ribu ";
                            } else {
                                terbilang5 = "Empat Puluh ";
                            }
                            break;
                        case 5:
                            if (satRb == 0) {
                                terbilang5 = "Lima Puluh Ribu ";
                            } else {
                                terbilang5 = "Lima Puluh ";
                            }
                            break;
                        case 6:
                            if (satRb == 0) {
                                terbilang5 = "Enam Puluh Ribu ";
                            } else {
                                terbilang5 = "Enam Puluh ";
                            }
                            break;
                        case 7:
                            if (satRb == 0) {
                                terbilang5 = "Tujuh Puluh Ribu ";
                            } else {
                                terbilang5 = "Tujuh Puluh ";
                            }
                            break;
                        case 8:
                            if (satRb == 0) {
                                terbilang5 = "Delapan Puluh Ribu ";
                            } else {
                                terbilang5 = "Delapan Puluh ";
                            }
                            break;
                        case 9:
                            if (satRb == 0) {
                                terbilang5 = "Sembilan Puluh Ribu ";
                            } else {
                                terbilang5 = "Sembilan Puluh ";
                            }
                            break;
                    }

                    //Perhitungan pada wilayah Satuan Ribuan :
                    switch (satRb) {
                        case 2:
                            if (puluhRb == 1) {
                                terbilang6 = "Dua Belas Ribu ";
                            } else {
                                terbilang6 = "Dua Ribu ";
                            }
                            break;
                        case 3:
                            if (puluhRb == 1) {
                                terbilang6 = "Tiga Belas Ribu ";
                            } else {
                                terbilang6 = "Tiga Ribu ";
                            }
                            break;
                        case 4:
                            if (puluhRb == 1) {
                                terbilang6 = "Empat Belas Ribu ";
                            } else {
                                terbilang6 = "Empat Ribu ";
                            }
                            break;
                        case 5:
                            if (puluhRb == 1) {
                                terbilang6 = "Lima Belas Ribu ";
                            } else {
                                terbilang6 = "Lima Ribu ";
                            }
                            break;
                        case 6:
                            if (puluhRb == 1) {
                                terbilang6 = "Enam Belas Ribu ";
                            } else {
                                terbilang6 = "Enam Ribu ";
                            }
                            break;
                        case 7:
                            if (puluhRb == 1) {
                                terbilang6 = "Tujuh Belas Ribu ";
                            } else {
                                terbilang6 = "Tujuh Ribu ";
                            }
                            break;
                        case 8:
                            if (puluhRb == 1) {
                                terbilang6 = "Delapan Belas Ribu ";
                            } else {
                                terbilang6 = "Delapan Ribu ";
                            }
                            break;
                        case 9:
                            if (puluhRb == 1) {
                                terbilang6 = "Sembilan Belas Ribu ";
                            } else {
                                terbilang6 = "Sembilan Ribu ";
                            }
                            break;
                    }

                    //Perhitungan pada wilayah Ratusan :
                    switch (ratus) {
                        case 1:
                            terbilang7 = "Seratus ";
                            break;
                        case 2:
                            terbilang7 = "Dua Ratus ";
                            break;
                        case 3:
                            terbilang7 = "Tiga Ratus ";
                            break;
                        case 4:
                            terbilang7 = "Empat Ratus ";
                            break;
                        case 5:
                            terbilang7 = "Lima Ratus ";
                            break;
                        case 6:
                            terbilang7 = "Enam Ratus ";
                            break;
                        case 7:
                            terbilang7 = "Tujuh Ratus ";
                            break;
                        case 8:
                            terbilang7 = "Delapan Ratus ";
                            break;
                        case 9:
                            terbilang7 = "Sembilan Ratus ";
                            break;
                    }

                    //Perhitungan pada wilayah Puluhan :
                    switch (puluh) {
                        case 1:
                            if (sat == 0) {
                                terbilang8 = "Sepuluh ";
                            } else if (sat == 1) {
                                terbilang8 = "Sebelas ";
                            }
                            break;
                        case 2:
                            terbilang8 = "Dua Puluh ";
                            break;
                        case 3:
                            terbilang8 = "Tiga Puluh ";
                            break;
                        case 4:
                            terbilang8 = "Empat Puluh ";
                            break;
                        case 5:
                            terbilang8 = "Lima Puluh ";
                            break;
                        case 6:
                            terbilang8 = "Enam Puluh ";
                            break;
                        case 7:
                            terbilang8 = "Tujuh Puluh ";
                            break;
                        case 8:
                            terbilang8 = "Delapan Puluh ";
                            break;
                        case 9:
                            terbilang8 = "Sembilan Puluh ";
                            break;
                    }

                    //Perhitungan pada wilayah Satuan :
                    switch (sat) {
                        case 1:
                            if (puluh != 1) {
                                terbilang9 = "Satu ";
                            }
                            break;
                        case 2:
                            if (puluh == 1) {
                                terbilang9 = "Dua Belas ";
                            } else {
                                terbilang9 = "Dua ";
                            }
                            break;
                        case 3:
                            if (puluh == 1) {
                                terbilang9 = "Tiga Belas ";
                            } else {
                                terbilang9 = "Tiga ";
                            }
                            break;
                        case 4:
                            if (puluh == 1) {
                                terbilang9 = "Empat Belas ";
                            } else {
                                terbilang9 = "Empat ";
                            }
                            break;
                        case 5:
                            if (puluh == 1) {
                                terbilang9 = "Lima Belas ";
                            } else {
                                terbilang9 = "Lima ";
                            }
                            break;
                        case 6:
                            if (puluh == 1) {
                                terbilang9 = "Enam Belas ";
                            } else {
                                terbilang9 = "Enam ";
                            }
                            break;
                        case 7:
                            if (puluh == 1) {
                                terbilang9 = "Tujuh Belas ";
                            } else {
                                terbilang9 = "Tujuh ";
                            }
                            break;
                        case 8:
                            if (puluh == 1) {
                                terbilang9 = "Delapan Belas ";
                            } else {
                                terbilang9 = "Delapan ";
                            }
                            break;
                        case 9:
                            if (puluh == 1) {
                                terbilang9 = "Sembilan Belas ";
                            } else {
                                terbilang9 = "Sembilan ";
                            }
                            break;
                    }

                    JOptionPane.showMessageDialog(null, "Angka yang anda inputkan adalah :"
                            + "\n" + hasil + ""
                            + "\nDikonversikan menjadi :"
                            + "\n" + terbilang1 + terbilang2 + terbilang3 + terbilang4 + terbilang5
                            + terbilang6 + terbilang7 + terbilang8 + terbilang9);

                    JOptionPane.showMessageDialog(null, bye);
                    break;
                } else {
                    JOptionPane.showMessageDialog(null, ulangi);
                }
            } while (true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, warning);
        }
    }
}

No comments:

Post a Comment