-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindName.java
193 lines (157 loc) · 4.78 KB
/
FindName.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
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class FindName extends JInternalFrame implements ActionListener {
private JPanel jpFind = new JPanel();
private JLabel lbNo, lbName, lbDate, lbBal;
private JTextField txtNo, txtName, txtDate, txtBal;
private JButton btnFind, btnCancel;
private int count = 0;
private int rows = 0;
private int total = 0;
//String Type Array use to Load Records From File.
private String records[][] = new String [500][6];
private FileInputStream fis;
private DataInputStream dis;
FindName () {
//super(Title, Resizable, Closable, Maximizable, Iconifiable)
super ("Search Customer [By Name]", false, true, false, true);
setSize (350, 235);
jpFind.setLayout (null);
lbNo = new JLabel ("Account No:");
lbNo.setForeground (Color.black);
lbNo.setBounds (15, 20, 80, 25);
lbName = new JLabel ("Person Name:");
lbName.setForeground (Color.black);
lbName.setBounds (15, 55, 80, 25);
lbDate = new JLabel ("Last Transaction:");
lbDate.setForeground (Color.black);
lbDate.setBounds (15, 90, 100, 25);
lbBal = new JLabel ("Balance:");
lbBal.setForeground (Color.black);
lbBal.setBounds (15, 125, 80, 25);
txtNo = new JTextField ();
txtNo.setHorizontalAlignment (JTextField.RIGHT);
txtNo.setEnabled (false);
txtNo.setBounds (125, 20, 200, 25);
txtName = new JTextField ();
txtName.setBounds (125, 55, 200, 25);
txtDate = new JTextField ();
txtDate.setEnabled (false);
txtDate.setBounds (125, 90, 200, 25);
txtBal = new JTextField ();
txtBal.setHorizontalAlignment (JTextField.RIGHT);
txtBal.setEnabled (false);
txtBal.setBounds (125, 125, 200, 25);
//Aligning The Buttons.
btnFind = new JButton ("Search");
btnFind.setBounds (20, 165, 120, 25);
btnFind.addActionListener (this);
btnCancel = new JButton ("Cancel");
btnCancel.setBounds (200, 165, 120, 25);
btnCancel.addActionListener (this);
//Adding the All the Controls to Panel.
jpFind.add (lbNo);
jpFind.add (txtNo);
jpFind.add (lbName);
jpFind.add (txtName);
jpFind.add (lbDate);
jpFind.add (txtDate);
jpFind.add (lbBal);
jpFind.add (txtBal);
jpFind.add (btnFind);
jpFind.add (btnCancel);
//Adding Panel to Window.
getContentPane().add (jpFind);
populateArray (); //Load All Existing Records in Memory.
//In the End Showing the New Account Window.
setVisible (true);
}
//Function use By Buttons of Window to Perform Action as User Click Them.
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnFind) {
if (txtName.getText().equals ("")) {
JOptionPane.showMessageDialog (this, "Please! Provide Name of Customer to Search.",
"BankSystem - EmptyField", JOptionPane.PLAIN_MESSAGE);
txtName.requestFocus();
}
else {
rows = 0;
populateArray (); //Load All Existing Records in Memory.
findRec (); //Finding if Account No. Exist or Not.
}
}
if (obj == btnCancel) {
txtClear ();
setVisible (false);
dispose();
}
}
//Function use to load all Records from File when Application Execute.
void populateArray () {
try {
fis = new FileInputStream ("Bank.dat");
dis = new DataInputStream (fis);
//Loop to Populate the Array.
while (true) {
for (int i = 0; i < 6; i++) {
records[rows][i] = dis.readUTF ();
}
rows++;
}
}
catch (Exception ex) {
total = rows;
if (total == 0) {
JOptionPane.showMessageDialog (null, "Records File is Empty.\nEnter Records First to Display.",
"BankSystem - EmptyFile", JOptionPane.PLAIN_MESSAGE);
btnEnable ();
}
else {
try {
dis.close();
fis.close();
}
catch (Exception exp) { }
}
}
}
//Function use to Find Record by Matching the Contents of Records Array with ID TextBox.
void findRec () {
boolean found = false;
for (int x = 0; x < total; x++) {
if (records[x][1].equalsIgnoreCase (txtName.getText())) {
found = true;
showRec (x);
break;
}
}
if (found == false) {
JOptionPane.showMessageDialog (this, "Customer " + txtName.getText () + " doesn't Exist.",
"BankSystem - WrongNo", JOptionPane.PLAIN_MESSAGE);
txtClear ();
}
}
//Function which display Record from Array onto the Form.
public void showRec (int intRec) {
txtNo.setText (records[intRec][0]);
txtName.setText (records[intRec][1]);
txtDate.setText (records[intRec][2] + ", " + records[intRec][3] + ", " + records[intRec][4]);
txtBal.setText (records[intRec][5]);
}
//Function use to Clear all TextFields of Window.
void txtClear () {
txtNo.setText ("");
txtName.setText ("");
txtDate.setText ("");
txtBal.setText ("");
txtName.requestFocus ();
}
//Function use to Lock Controls of Window.
void btnEnable () {
txtName.setEnabled (false);
btnFind.setEnabled (false);
}
}