Repository navigation
Expand file tree
/
Copy pathConfirmData.java
More file actions
264 lines (240 loc) · 10.5 KB
/
Copy pathConfirmData.java
File metadata and controls
264 lines (240 loc) · 10.5 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
public class ConfirmData extends Pelanggan {
static int[] counter = {0}; static String[][] dataa = new String[10][9]; static boolean flag=false; static int index1=0, index2=0;
public ConfirmData(String nama, long nik, long nohp, String tAwal, String tAkhir, int harga, int jumlah, String kursi) {
Scanner sc;
//check if data.txt null ato ga
File file = new File("data.txt");
if(file.length()==0){
flag=true;
counter[0]++;
}
else {
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
while(reader.readLine() !=null){
counter[0]++;
}
} catch (IOException d) {
System.err.println(d);
}
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
int o = 0;
String s;
while ((s = reader.readLine()) != null && o < dataa.length) {
sc = new Scanner(s);
int u = 0;
while (sc.hasNext() && u < 9) {
dataa[o][u] = sc.next();
u++;
}
o++;
}
// counter[0] = Integer.parseInt(dataa[index1-1][0]);
}catch (IOException d) {
System.err.println(d);
}
}
// Frame setup
JFrame frame = new JFrame("Confirm Data");
frame.setSize(925, 600);
frame.setLayout(null);
frame.setBackground(Color.darkGray);
frame.getContentPane().setBackground(Color.decode("#9a8c98"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Panel setup
JPanel panel = new JPanel();
panel.setBounds(0, 55, 910, 185);
panel.setBackground(Color.decode("#c9ada7"));
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.setLayout(null);
frame.add(panel);
//Warning cm bisa pencet sekali
JLabel warn = new JLabel("Data telah ter-input");
warn.setForeground(Color.red);
warn.setFont(new Font("Tahoma", Font.PLAIN, 14));
warn.setBounds(367,530,200,30);
warn.setVisible(false);
frame.add(warn);
//totalharga
int Tharga = jumlah*harga;
// Column titles
String[] columns = {"No", "Nama", "NIK Penumpang", "No HP", "Titik Awal", "Titik Akhir", "Jumlah Tiket", "Total Harga", "Kursi"};
String[][] data1 = new String[counter[0]][9];
for(int i=0; i<counter[0]; i++){
for(int j=0; j<9; j++){
data1[i][j] = " ";
}
}
//tombol tambah data
JButton add = new JButton("Tambah Data");
add.setForeground(Color.decode("#f9dcc4"));
add.setBounds(350, 515,200, 30);
add.setBackground(Color.decode("#4a4e69"));
add.setVisible(true);
frame.add(add);
//tombol balek ke homepage
JButton back = new JButton("Home Page");
back.setBounds(50, 515,200, 30);
back.setForeground(Color.decode("#f9dcc4"));
back.setBackground(Color.decode("#4a4e69"));
back.setVisible(true);
frame.add(back);
//tombol clear data
JButton clear = new JButton("Clear Data");
clear.setBounds(650, 515, 200, 30);
clear.setForeground(Color.decode("#f9dcc4"));
clear.setBackground(Color.decode("#4a4e69"));
clear.setVisible(true);
// biar tabel gbs di edit
DefaultTableModel model = new DefaultTableModel(data1, columns) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // No cell is editable
}
};
for (int i = 0; i < counter[0]; i++) {
for (int j = 0; j < 9; j++) {
model.setValueAt(dataa[i][j], i, j);
}
}
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//action yang bakalan dilakuin klo file data.txt kosong
if(flag){
index2++;
model.setValueAt(String.valueOf(counter[0]+1), 0, 0);
model.setValueAt(nama, 0, 1);
model.setValueAt(String.valueOf(nik), 0, 2);
model.setValueAt(String.valueOf(nohp), 0, 3);
model.setValueAt(tAwal, 0, 4);
model.setValueAt(tAkhir, 0, 5);
model.setValueAt(jumlah, 0, 6);
model.setValueAt(Tharga, 0, 7 );
model.setValueAt(kursi, 0, 8 );
try(PrintWriter writer = new PrintWriter(new FileWriter("data.txt", true))){
writer.print(counter[0]+" "); writer.print(nama+" "); writer.print(nik+" "); writer.print(nohp+" ");
writer.print(tAwal+" "); writer.print(tAkhir+" "); writer.print(jumlah+" "); writer.print(Tharga+" ");
writer.print(kursi);
writer.println();
} catch(IOException d){
System.err.println(d);
}
counter[0]++;
}
else{
if(index2>0) {
warn.setVisible(true);
}
else{
//nambahin data baru ke dalem file data.txt
counter[0]++;
index2++;
String[] data = {String.valueOf(counter[0]), nama, String.valueOf(nik), String.valueOf(nohp), tAwal, tAkhir, String.valueOf(jumlah), String.valueOf(Tharga), kursi };
model.addRow(data);
try (PrintWriter writer = new PrintWriter(new FileWriter("data.txt", true))) {
writer.print(counter[0] + " ");
writer.print(nama + " ");
writer.print(nik + " ");
writer.print(nohp + " ");
writer.print(tAwal + " ");
writer.print(tAkhir + " ");
writer.print(jumlah + " ");
writer.print(Tharga + " ");
writer.print(kursi);
writer.println();
} catch (IOException d) {
System.err.println(d);
}
}
}
}
});
// Create table with data and column names
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 250, 890, 250);
frame.add(scrollPane);
//Labels
JLabel title = new JLabel("Data Penumpang");
title.setBounds(380, 10, 400, 30);
title.setFont(new Font("Arial", Font.BOLD, 25));
title.setForeground(Color.decode("#f9dcc4"));
JLabel name = new JLabel("Nama Penumpang:");
name.setBounds(10, 40, 150, 20);
JTextField isiNama = new JTextField(nama);
isiNama.setEditable(false);
isiNama.setBounds(170,40,200,20);
JLabel nik1 = new JLabel("NIK Penumpang:");
nik1.setBounds(10, 70, 150, 20);
JTextField isiNik = new JTextField(String.valueOf(nik));
isiNik.setEditable(false);
isiNik.setBounds(170,70,200,20);
JLabel no = new JLabel("No HP:");
no.setBounds(10, 100, 150, 20);
JTextField isiNo = new JTextField(String.valueOf(nohp));
isiNo.setEditable(false);
isiNo.setBounds(170,100,200,20);
JLabel titikAwal = new JLabel("Titik Awal:");
titikAwal.setBounds(10, 130, 150, 20);
JTextField isiTAwal = new JTextField(tAwal);
isiTAwal.setEditable(false);
isiTAwal.setBounds(170,130,200,20);
JLabel titikAkhir = new JLabel("Titik Akhir:");
titikAkhir.setBounds(550, 130, 150, 20);
JTextField isiTAkhir = new JTextField(tAkhir);
isiTAkhir.setEditable(false);
isiTAkhir.setBounds(640,130,200,20);
JLabel jumlahTiket = new JLabel("Jumlah Tiket:");
jumlahTiket.setBounds(550, 40, 150, 20);
JTextField isiJumlah = new JTextField(String.valueOf(jumlah));
isiJumlah.setEditable(false);
isiJumlah.setBounds(640,40,200,20);
JLabel hargaTiket = new JLabel("Harga Tiket:");
hargaTiket.setBounds(550, 70, 150, 20);
JTextField isiHarga = new JTextField(String.valueOf(harga));
isiHarga.setEditable(false);
isiHarga.setBounds(640,70,200,20);
JLabel harga1 = new JLabel("Total Harga:");
harga1.setBounds(550, 100, 150, 20);
JTextField isiHarga1 = new JTextField();
isiHarga1.setText(String.valueOf(Tharga));
isiHarga1.setEditable(false);
isiHarga1.setBounds(640,100,200,20);
//back button action
back.addActionListener(e -> {
index2=0;
counter[0]=0;
flag=false;
new HomePage();
frame.setVisible(false);
});
//clear button action
clear.addActionListener(e -> {
for(int i = model.getRowCount() - 1; i >= 0; i--){
model.removeRow(i);
}
try(PrintWriter writer = new PrintWriter(new FileWriter("data.txt", false))){
writer.print("");
}catch(IOException d){
System.err.println(d);
}
});
// Add components to frame
frame.add(title); panel.add(titikAwal); frame.add(clear);
panel.add(name); panel.add(titikAkhir);
panel.add(nik1); panel.add(jumlahTiket);
panel.add(no); panel.add(harga1);
panel.add(isiTAwal); panel.add(isiTAkhir);
panel.add(jumlahTiket); panel.add(isiJumlah);
panel.add(isiHarga); panel.add(isiNama);
panel.add(isiNik); panel.add(isiNo);
panel.add(isiHarga1); panel.add(hargaTiket);
frame.setVisible(true);
}
}