Skip to content

Instantly share code, notes, and snippets.

@gunungloli666
Created February 11, 2016 05:57
Show Gist options
  • Save gunungloli666/92baffcc776bca5c18d6 to your computer and use it in GitHub Desktop.
Save gunungloli666/92baffcc776bca5c18d6 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.HashMap;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class BilanganToKalimat extends Application{
public String printBilanganToKalimat(int a ){
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1, "satu");
map1.put(2, "dua");
map1.put(3, "tiga");
map1.put(4, "empat");
map1.put(5, "lima");
map1.put(6, "enam");
map1.put(7, "tujuh");
map1.put(8, "delapan");
map1.put(9, "sembilan");
map1.put( 0 , "");
HashMap<Integer, String> map2 = new HashMap<>();
map2.put(0 , "" );
map2.put(1, "puluh" );
map2.put(2, "ratus" );
map2.put(3, "ribu" );
map2.put(4, "puluh ribu" );
map2.put(5, "ratus ribu" );
map2.put(6, "juta");
map2.put(7, "puluh juta");
map2.put(8, "ratus juta");
map2.put(9, "miliar");
map2.put(10, "puluh miliar");
map2.put(11, "ratus miliar");
int m, n;
String st = Integer.toString(a);
n = m = st.length() - 1;
StringBuilder builder = new StringBuilder();
ArrayList<String> lst = new ArrayList<>();
while( n >=0 ){
int c = st.codePointAt(n);
c = c - 48;
builder.append(map1.get(c));
builder.append(" ");
if( c!= 0){
String sebutan = map2.get(m - n);
String[] sss = sebutan.split("\\s");
if(sss.length == 2){
if(!lst.contains(sss[sss.length - 1])){
builder.append(sebutan);
lst.add(sss[sss.length - 1]);
}else{
for( int j = 0; j < sss.length - 1; j++)
builder.append(sss[j] + " ");
}
}else{
builder.append(sebutan);
lst.add(sebutan);
}
builder.append("_");
}
n--;
}
String temp[] = builder.toString().split("\\_");
StringBuilder bt = new StringBuilder();
for( int i = temp.length - 1; i>= 0; i-- ){
bt.append(temp[i]);
bt.append(" ") ;
}
return bt. toString();
}
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
AnchorPane pane = new AnchorPane();
pane.setPrefSize(400, 400);
primaryStage.setScene(new Scene(pane));
primaryStage.show();
initLayout(pane);
}
Text area = new Text();
private void initLayout(AnchorPane pane){
VBox box = new VBox();
box.setSpacing(10);
TextField input = new TextField();
input.setPrefSize(150, 30);
box.getChildren().add(input) ;
Button button = new Button("CONVERT");
button.setPrefSize(150, 30);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
String c = input.getText();
try{
int a = Integer.parseInt(c);
String hasil = printBilanganToKalimat(a);
area.setText(hasil);
}catch(Exception e){}
}
});
box.getChildren().add(button);
HBox hbox = new HBox();
hbox.setTranslateX(5);
hbox.setTranslateY(5);
hbox.setSpacing(10);
hbox.getChildren().add(box);
box.getChildren().add(area);
pane.getChildren().add(hbox);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment