-
Notifications
You must be signed in to change notification settings - Fork 0
/
PencarianController.java
77 lines (71 loc) · 2.45 KB
/
PencarianController.java
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
/*
* 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.
*/
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* @author Mumpa
*/
public class PencarianController implements Initializable {
@FXML
private TextField textData, textArrayData, textCari;
@FXML
private Button buttonTambah, buttonCari;
@FXML
private TextArea textHasil;
private PencarianArray pencarianArray;
//kapasitas array
private final int KAPASITAS_ARRAY = 20;
//index array yang akan di tembahkan
private int currentIndex = 0;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
//membuat object array (instance)
pencarianArray = new PencarianArray(KAPASITAS_ARRAY);
}
@FXML
private void tambahArrayData(){
//mengambil nilai dari input textfield + konversi string ke int
int dataArray = Integer.parseInt(textData.getText());
//setiap menambahkan data kearray, tambahkan index + 1
pencarianArray.setData(this.currentIndex++, dataArray);
//hapus text di textfield dan focus
textData.clear();
textData.requestFocus();
//tampilkan data
this.tampilkanArrayData();
}
@FXML
private void cariData(){
//mengambil nilai dari input textfield + konversi string ke int
int nilaiCari = Integer.parseInt(textCari.getText());
int hasilCari = pencarianArray.cari(nilaiCari);
//tampilkan pesan sesuai
String text = hasilCari != - 1 ? nilaiCari+" pada index: "+String.valueOf(hasilCari) : "<not found>";
textHasil.appendText(text+"\n");
//hapus text di textfield dan focus
textCari.clear();
textCari.requestFocus();
}
private void tampilkanArrayData(){
//mengambil data dari array
int dataArray[] = pencarianArray.getData();
//menambilkan array sbg string
textArrayData.setText(Arrays.toString(dataArray));
}
}