-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibraryDatabase.java
More file actions
244 lines (204 loc) · 8.02 KB
/
Copy pathLibraryDatabase.java
File metadata and controls
244 lines (204 loc) · 8.02 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
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class LibraryDatabase extends JFrame implements ActionListener{
JTextField txtSearchBy;
JLabel lblSearchBy;
JComboBox<String> comboSearchBy, searchResults;
JButton btnAddBook, btnModifyBook, btnListAll;
JTextArea textAreaList;
public LibraryDatabase() {
setTitle("Library Database");
getContentPane().setLayout(null);
JLabel lblSearchBy = new JLabel("Search By: ");
lblSearchBy.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSearchBy.setBounds(10, 11, 89, 14);
getContentPane().add(lblSearchBy);
JComboBox comboSearchBy = new JComboBox();
comboSearchBy.setBounds(124, 10, 150, 20);
getContentPane().add(comboSearchBy);
comboSearchBy.addItem("Title");
comboSearchBy.addItem("Author");
comboSearchBy.addItem("ISBN");
comboSearchBy.addItem("Date (In yyyy/mm/dd format");
comboSearchBy.addItem("Rating Greater than");
comboSearchBy.addItem("Rating Less than");
JButton btnSearch = new JButton("Search");
btnSearch.setBounds(10, 67, 264, 23);
getContentPane().add(btnSearch);
final JTextField txtSearchBy = new JTextField();
txtSearchBy.setText("Enter Search Value Here:");
txtSearchBy.setBounds(10, 36, 264, 20);
getContentPane().add(txtSearchBy);
txtSearchBy.setColumns(10);
JComboBox searchResults = new JComboBox();
searchResults.setBounds(0, 101, 284, 20);
getContentPane().add(searchResults);
JButton btnAddBook = new JButton("Add Book");
btnAddBook.setFont(new Font("Tahoma", Font.BOLD, 8));
btnAddBook.setBounds(10, 327, 86, 23);
btnAddBook.addActionListener(this);
getContentPane().add(btnAddBook);
JButton btnModifyBook = new JButton("Modify");
btnModifyBook.setFont(new Font("Tahoma", Font.BOLD, 8));
btnModifyBook.setBounds(94, 327, 94, 23);
getContentPane().add(btnModifyBook);
JButton btnListAll = new JButton("List ");
btnListAll.setFont(new Font("Tahoma", Font.BOLD, 8));
btnListAll.setBounds(185, 327, 89, 23);
getContentPane().add(btnListAll);
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Scanner scanner = null;
try {
scanner = new Scanner(new File ("database.dat"));
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}
// Start a line count and declare a string to hold our current line.
int linecount = 0;
// Loop through each line, storing the line into our buffer. while incrementing linecount.
List<String> list=new ArrayList<>();
StringBuilder buffer = new StringBuilder();
while(scanner.hasNextLine()){
linecount++;
list.add(scanner.nextLine());
buffer.append(list);
buffer.append('\n');
String Buffer = buffer.toString();
// cleaning up actual buffer output (removing square brackets)
Buffer = Buffer.replaceAll("\\[", "").replaceAll("\\]","");
String[] splitArray = Buffer.split("\\s+");
String search = txtSearchBy.getText();
for(int i = 0; i < splitArray.length; i++) {
if(search.equals(splitArray[i])){
System.out.println(Buffer);
}
}
}
}});
}
public static void main(String[] args) {
LibraryDatabase app = new LibraryDatabase();
app.setSize(300,400);
app.setVisible(true);
app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
class ChildWindowAdd extends JFrame implements ActionListener {
LibraryDatabase parent;
Container c;
JLabel lblTitle, lblaName, lblISBN, lblDate,lbl1,lbl5,lbl10;
JTextField txtTitle, txtaName, txtISBN, txtDate;
JButton btnSave;
JSlider sRating;
JCheckBox chkSci, chkFant, chkRomance, chkAction, chkThriller, chkHorror;
public ChildWindowAdd(LibraryDatabase parent){
this.parent = parent;
getContentPane().setLayout(null);
txtTitle = new JTextField();
txtTitle.setBounds(46, 29, 146, 20);
getContentPane().add(txtTitle);
txtTitle.setColumns(10);
JLabel lblTitle = new JLabel("Book Title");
lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblTitle.setBounds(36, 11, 72, 14);
getContentPane().add(lblTitle);
JLabel lblaName = new JLabel("Author Name");
lblaName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblaName.setBounds(36, 60, 89, 14);
getContentPane().add(lblaName);
txtaName = new JTextField();
txtaName.setBounds(46, 85, 146, 20);
getContentPane().add(txtaName);
txtaName.setColumns(10);
JLabel lblIsbnNumber = new JLabel("ISBN Number");
lblIsbnNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblIsbnNumber.setBounds(36, 116, 86, 14);
getContentPane().add(lblIsbnNumber);
txtISBN = new JTextField();
txtISBN .setBounds(46, 143, 146, 20);
getContentPane().add(txtISBN );
txtISBN .setColumns(10);
JLabel lblDate = new JLabel("Date Added yyyy/mm/dd");
lblDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDate.setBounds(36, 174, 177, 20);
getContentPane().add(lblDate);
txtDate = new JTextField();
txtDate.setBounds(46, 199, 146, 20);
getContentPane().add(txtDate);
txtDate.setColumns(10);
JPanel genreP = new JPanel();
genreP.setBounds(239, 29, 158, 194);
genreP.setBorder(new TitledBorder(null, "Genre's", TitledBorder.LEADING, TitledBorder.TOP, null, null));
getContentPane().add(genreP);
genreP.setLayout(null);
final JCheckBox chkSci = new JCheckBox("Sci - Fi");
chkSci.setBounds(23, 22, 97, 23);
genreP.add(chkSci);
final JCheckBox chkFant = new JCheckBox("Fantasy");
chkFant.setBounds(23, 48, 97, 23);
genreP.add(chkFant);
final JCheckBox chkRomance = new JCheckBox("Romance");
chkRomance.setBounds(23, 74, 97, 23);
genreP.add(chkRomance);
final JCheckBox chkAction = new JCheckBox("Action");
chkAction.setBounds(23, 100, 97, 23);
genreP.add(chkAction);
final JCheckBox chkThriller = new JCheckBox("Thriller");
chkThriller.setBounds(23, 126, 97, 23);
genreP.add(chkThriller);
final JCheckBox chkHorror = new JCheckBox("Horror");
chkHorror.setBounds(23, 152, 97, 23);
genreP.add(chkHorror);
final JSlider sRating = new JSlider();
sRating.setBounds(118, 280, 200, 26);
getContentPane().add(sRating);
JLabel lbl1 = new JLabel("1");
lbl1.setBounds(118, 257, 7, 14);
getContentPane().add(lbl1);
JLabel lbl10 = new JLabel("10");
lbl10.setBounds(301, 257, 17, 14);
getContentPane().add(lbl10);
JLabel lbl5 = new JLabel("5");
lbl5.setBounds(214, 257, 7, 14);
getContentPane().add(lbl5);
JButton btnSave = new JButton("Save Book");
btnSave.addActionListener(this);
btnSave.setBounds(164, 317, 110, 23);
getContentPane().add(btnSave);
JLabel lblRating = new JLabel("Rate the book");
lblRating.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblRating.setBounds(118, 226, 95, 20);
getContentPane().add(lblRating);
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PrintWriter fileWriter = null;
try {
fileWriter = new PrintWriter(new BufferedWriter(new FileWriter("database.dat", true)));
} catch (IOException e) {
e.printStackTrace();
}
fileWriter.println( txtTitle.getText()+" , "+txtaName.getText()+" , "+txtISBN.getText()+" , "+txtDate.getText()+" , "+String.valueOf(chkSci.isSelected())+" , "+String.valueOf(chkFant.isSelected())+" , "+String.valueOf(chkRomance.isSelected())+" , "+String.valueOf(chkAction.isSelected())+" , "+String.valueOf(chkThriller.isSelected())+" , "+String.valueOf(chkHorror.isSelected())+" , "+sRating.getValue());
fileWriter.close();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
parent.setVisible(true);
this.dispose();
}
}
public void actionPerformed(ActionEvent e) {
ChildWindowAdd child = new ChildWindowAdd(this);
this.setVisible(true);
child.setVisible(true);
child.setSize(450,400);
child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}