Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.17 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.LinkedList;
  6.  
  7. public class SouthbankLibrary extends JFrame {
  8.  
  9. // Method: main
  10. // Opens the GUI. That's about it...
  11. public static void main(String[] args) {
  12. // Create new instance of the GUI Object.
  13. SouthbankLibrary mainFrame = new SouthbankLibrary();
  14. mainFrame.setVisible(true);
  15. mainFrame.setResizable(false);
  16. }
  17.  
  18. //------------------------------------------------------------------------------------------------------------------
  19. // Constructor of the GUI Object
  20. public SouthbankLibrary() {
  21. // Set the frame characteristics
  22. setTitle( "Macrosoft Staff" );
  23. setSize(1000, 800);
  24.  
  25. // Create a JPanel to hold all the GUI components.
  26. final JPanel mainPanel = new JPanel();
  27. mainPanel.setLayout( new FlowLayout(FlowLayout.CENTER) );
  28. getContentPane().add(mainPanel);
  29.  
  30. // Fill the JPanel with the main menu components.
  31. buildMenuGUI(mainPanel);
  32. }
  33.  
  34. //------------------------------------------------------------------------------------------------------------------
  35. // Populate the main menu with buttons.
  36. // Called in the GUI Constructor, and when back to menu button is pressed.
  37. public static void buildMenuGUI(final JPanel mainPanel) {
  38. // Clear the mainPanel
  39. mainPanel.removeAll();
  40.  
  41. // Set up buttons, with their dimensions, to fill the whole window.
  42. JButton listStaffMenuButton = new JButton("LIST ALL STAFF MEMBERS");
  43. listStaffMenuButton.setPreferredSize(new Dimension(1000, 250));
  44. JButton searchStaffMenuButton = new JButton("SEARCH STAFF MEMBERS");
  45. searchStaffMenuButton.setPreferredSize(new Dimension(1000, 250));
  46. JButton addStaffMenuButton = new JButton("ADD STAFF MEMBER");
  47. addStaffMenuButton.setPreferredSize(new Dimension(1000, 250));
  48.  
  49. // Add them to the GUI.
  50. mainPanel.add(listStaffMenuButton);
  51. mainPanel.add(searchStaffMenuButton);
  52. mainPanel.add(addStaffMenuButton);
  53. mainPanel.revalidate();
  54. mainPanel.repaint();
  55.  
  56. // Do stuff when each button is pressed...
  57. // When the list button is pressed
  58. listStaffMenuButton.addActionListener(new ActionListener() {
  59. public void actionPerformed(ActionEvent e) {
  60. buildListGUI(mainPanel);
  61. }
  62. });
  63.  
  64. // When the search button is pressed
  65. searchStaffMenuButton.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent e) {
  67. buildSearchGUI(mainPanel);
  68. }
  69. });
  70.  
  71. // When the add button is pressed
  72. addStaffMenuButton.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74. buildAddGUI(mainPanel);
  75. }
  76. });
  77. }
  78.  
  79.  
  80. //------------------------------------------------------------------------------------------------------------------
  81. // Method: buildListGUI by George
  82. // Clear the current GUI window, and fill with the list of staff members.
  83. // Called in the buildMenuGUI method.
  84. public static void buildListGUI(final JPanel mainPanel) {
  85. // Clear the mainPanel
  86. mainPanel.removeAll();
  87.  
  88. // Load the file every time the List is rebuilt, to ensure it is up-to-date.
  89. String[][]staffData = loadFile();
  90.  
  91. // Create columns names
  92. String columnNames[] = { "First Name", "Last Name", "Email", "Country", "Gender", "Language" };
  93.  
  94. // Create a new table, using staffData as the data, and columnNames as the column names (duh...)
  95. JTable table = new JTable(staffData, columnNames) {
  96. // Stop the table from being user-editable.
  97. public boolean isCellEditable(int row,int column) {
  98. return false;
  99. }
  100. };
  101.  
  102. // Add the table to a scrolling pane, and size to fit neatly in the window.
  103. JScrollPane scrollPane = new JScrollPane(table);
  104. scrollPane.setPreferredSize(new Dimension(990, 670));
  105.  
  106. // Make a button to return to the menu.
  107. JButton returnToMenuButton = new JButton("RETURN TO MAIN MENU");
  108. returnToMenuButton.setPreferredSize(new Dimension(1000, 95));
  109.  
  110. // Add the table and the button to the GUI window.
  111. mainPanel.add(returnToMenuButton);
  112. mainPanel.add(scrollPane);
  113. mainPanel.revalidate();
  114. mainPanel.repaint();
  115.  
  116. // When the return to menu button is pressed
  117. returnToMenuButton.addActionListener(new ActionListener() {
  118. public void actionPerformed(ActionEvent e) {
  119. buildMenuGUI(mainPanel);
  120. }
  121. });
  122. }
  123.  
  124. //------------------------------------------------------------------------------------------------------------------
  125. // Method: buildSearchGUI by George
  126. // Clear the current GUI window, and fill with the search box and results table.
  127. // Called in the buildMenuGUI method.
  128. public static void buildSearchGUI(final JPanel mainPanel) {
  129. // Clear the mainPanel
  130. mainPanel.removeAll();
  131.  
  132. // Create the label for the text field.
  133. final JLabel searchTextFieldLabel = new JLabel("Search by Last Name: ");
  134. searchTextFieldLabel.setPreferredSize(new Dimension(140, 40));
  135.  
  136. // Create the text field for the search term.
  137. final JTextField searchTextField = new JTextField();
  138. searchTextField.setPreferredSize(new Dimension(760, 40));
  139.  
  140. // Create the search button.
  141. final JButton searchButton = new JButton("Search");
  142. searchButton.setPreferredSize(new Dimension(80, 40));
  143.  
  144. // Create the table for search results.
  145. // Load the file every time the List is rebuilt, to ensure it is up-to-date.
  146. final String[][] staffData = loadFile();
  147. // Create columns names
  148. String columnNames[] = { "First Name", "Last Name", "Email", "Country", "Gender", "Language" };
  149. // Create a new table, using staffData as the data, and columnNames as the column names (duh...)
  150. final JTable table = new JTable(staffData, columnNames) {
  151. // Stop the table from being user-editable.
  152. public boolean isCellEditable(int row,int column) {
  153. return false;
  154. }
  155. };
  156.  
  157. // Add the table to a scrolling pane, and size to fit neatly in the window.
  158. final JScrollPane scrollPane = new JScrollPane(table);
  159. scrollPane.setPreferredSize(new Dimension(990, 620));
  160.  
  161. // Make a button to return to the menu.
  162. final JButton returnToMenuButton = new JButton("RETURN TO MAIN MENU");
  163. returnToMenuButton.setPreferredSize(new Dimension(1000, 95));
  164.  
  165. // Add the components to the GUI window.
  166. mainPanel.add(returnToMenuButton);
  167. mainPanel.add(searchTextFieldLabel);
  168. mainPanel.add(searchTextField);
  169. mainPanel.add(searchButton);
  170. mainPanel.add(scrollPane);
  171. mainPanel.revalidate();
  172. mainPanel.repaint();
  173.  
  174. // When the search button is pressed.
  175. searchButton.addActionListener(new ActionListener() {
  176. public void actionPerformed(ActionEvent e) {
  177. mainPanel.removeAll();
  178. String searchTerm = searchTextField.getText().toLowerCase();
  179. String[][] searchResults = searchArray(staffData, searchTerm);
  180. // Create columns names
  181. String columnNames[] = { "First Name", "Last Name", "Email", "Country", "Gender", "Language" };
  182. // Create a new table, using staffData as the data, and columnNames as the column names (duh...)
  183. JTable table = new JTable(searchResults, columnNames) {
  184. // Stop the table from being user-editable.
  185. public boolean isCellEditable(int row,int column) {
  186. return false;
  187. }
  188. };
  189.  
  190. // Add the table to a scrolling pane, and size to fit neatly in the window.
  191. JScrollPane scrollPane = new JScrollPane(table);
  192. scrollPane.setPreferredSize(new Dimension(990, 620));
  193. mainPanel.add(returnToMenuButton);
  194. mainPanel.add(searchTextFieldLabel);
  195. mainPanel.add(searchTextField);
  196. mainPanel.add(searchButton);
  197. mainPanel.add(scrollPane);
  198. mainPanel.revalidate();
  199. mainPanel.repaint();
  200. }
  201. });
  202.  
  203. // When the return to menu button is pressed.
  204. returnToMenuButton.addActionListener(new ActionListener() {
  205. public void actionPerformed(ActionEvent e) {
  206. buildMenuGUI(mainPanel);
  207. }
  208. });
  209. }
  210.  
  211. //------------------------------------------------------------------------------------------------------------------
  212. // Method: buildAddGUI by George
  213. // Clear the current GUI window, and fill with the add form.
  214. // Called in the buildMenuGUI method.
  215. public static void buildAddGUI (final JPanel mainPanel) {
  216. // Clear the mainPanel
  217. mainPanel.removeAll();
  218.  
  219. // Make a button to return to the menu.
  220. JButton returnToMenuButton = new JButton("RETURN TO MAIN MENU");
  221. returnToMenuButton.setPreferredSize(new Dimension(1000, 95));
  222.  
  223. // Set up the labels and corresponding input fields.
  224. JLabel addFirstNameFieldLabel = new JLabel("First Name: ");
  225. addFirstNameFieldLabel.setPreferredSize(new Dimension(100, 40));
  226. final JTextField addFirstNameField = new JTextField();
  227. addFirstNameField.setPreferredSize(new Dimension(850, 40));
  228.  
  229. JLabel addLastNameFieldLabel = new JLabel("Last Name: ");
  230. addLastNameFieldLabel.setPreferredSize(new Dimension(100, 40));
  231. final JTextField addLastNameField = new JTextField();
  232. addLastNameField.setPreferredSize(new Dimension(850, 40));
  233.  
  234. JLabel addEmailFieldLabel = new JLabel("Email: ");
  235. addEmailFieldLabel.setPreferredSize(new Dimension(100, 40));
  236. final JTextField addEmailField = new JTextField();
  237. addEmailField.setPreferredSize(new Dimension(850, 40));
  238.  
  239. JLabel addCountryFieldLabel = new JLabel("Country: ");
  240. addCountryFieldLabel.setPreferredSize(new Dimension(100, 40));
  241. final JTextField addCountryField = new JTextField();
  242. addCountryField.setPreferredSize(new Dimension(850, 40));
  243.  
  244. JLabel addGenderFieldLabel = new JLabel("Gender: ");
  245. addGenderFieldLabel.setPreferredSize(new Dimension(100, 40));
  246. String[] genderChoices = {"Male", "Female"};
  247. final JComboBox addGenderField = new JComboBox(genderChoices);
  248. addGenderField.setPreferredSize(new Dimension(850, 40));
  249.  
  250. JLabel addLanguageFieldLabel = new JLabel("Language: ");
  251. addLanguageFieldLabel.setPreferredSize(new Dimension(100, 40));
  252. final JTextField addLanguageField = new JTextField();
  253. addLanguageField.setPreferredSize(new Dimension(850, 40));
  254.  
  255. // Create the add staff button
  256. JButton addStaffButton = new JButton("ADD STAFF MEMBER");
  257. addStaffButton.setPreferredSize(new Dimension(1000, 160));
  258.  
  259. // Add everything to the GUI window.
  260. mainPanel.add(returnToMenuButton);
  261. mainPanel.add(addFirstNameFieldLabel);
  262. mainPanel.add(addFirstNameField);
  263. mainPanel.add(addLastNameFieldLabel);
  264. mainPanel.add(addLastNameField);
  265. mainPanel.add(addEmailFieldLabel);
  266. mainPanel.add(addEmailField);
  267. mainPanel.add(addCountryFieldLabel);
  268. mainPanel.add(addCountryField);
  269. mainPanel.add(addGenderFieldLabel);
  270. mainPanel.add(addGenderField);
  271. mainPanel.add(addLanguageFieldLabel);
  272. mainPanel.add(addLanguageField);
  273. mainPanel.add(addStaffButton);
  274. mainPanel.revalidate();
  275. mainPanel.repaint();
  276.  
  277. // When the add button is pressed.
  278. addStaffButton.addActionListener(new ActionListener() {
  279. public void actionPerformed(ActionEvent e) {
  280. writeToFile(addFirstNameField.getText(),addLastNameField.getText(),addEmailField.getText(),addCountryField.getText(),addGenderField.getSelectedItem().toString(),addLanguageField.getText());
  281. }
  282. });
  283.  
  284. // When the return to menu button is pressed.
  285. returnToMenuButton.addActionListener(new ActionListener() {
  286. public void actionPerformed(ActionEvent e) {
  287. buildMenuGUI(mainPanel);
  288. }
  289. });
  290. }
  291.  
  292. //------------------------------------------------------------------------------------------------------------------
  293. // Method: writeToFile by Matt
  294. // Adds the parameters to the data array and then calls addFile to write this to the csv
  295. public static void writeToFile (String firstName, String lastName, String email, String country, String gender, String language)
  296. {
  297. // Load the old version of the file.
  298. String[][] staffData = loadFile();
  299. // Make a new array that is one index larger.
  300. String[][] newStaffData = new String[staffData.length + 1][6];
  301. // Copy the old array to the new one.
  302. for (int i = 0; i < staffData.length; i ++)
  303. {
  304. for (int j = 0; j < 6; j++)
  305. {
  306. newStaffData[i][j] = staffData[i][j];
  307. }
  308. }
  309.  
  310. //Add to the new 2D array
  311. System.out.println(firstName + " " + lastName + " " + email + " " + country + " " + gender + " " +language);
  312. newStaffData[newStaffData.length - 1][0] = firstName;
  313. newStaffData[newStaffData.length - 1][1] = lastName;
  314. newStaffData[newStaffData.length - 1][2] = email;
  315. newStaffData[newStaffData.length - 1][3] = country;
  316. newStaffData[newStaffData.length - 1][4] = (gender.charAt(0) + "").toUpperCase();
  317. newStaffData[newStaffData.length - 1][5] = language;
  318.  
  319. //Rewrite the file
  320. addFile(newStaffData);
  321. }
  322.  
  323. //------------------------------------------------------------------------------------------------------------------
  324. // Method: addFile by Matt
  325. // Writes the contents of the parameter 2D array to the STAFF.csv file.
  326. public static void addFile(String[][] newStaffData)
  327. {
  328. try
  329. {
  330. PrintWriter pw = new PrintWriter(new FileWriter ("/Users/MatthewClisby/Documents/Comp Sci/Programs/JAVA OUTPUTS/STAFF.csv"));
  331.  
  332. // Write each row of the 2d array to the file in a comma separated line.
  333. for(int i = 0; i < newStaffData.length; i++)
  334. {
  335. pw.println(newStaffData[i][0] + "," + newStaffData[i][1] + "," + newStaffData[i][2] + "," + newStaffData[i][3] + "," + newStaffData[i][4] + "," + newStaffData[i][5]);
  336. }
  337. pw.close();
  338. }
  339. catch (IOException e)
  340. {
  341. // output to console if there is a problem with writing
  342. System.out.println("Something went wrong with writing to the file.");
  343. }
  344. }
  345.  
  346. //------------------------------------------------------------------------------------------------------------------
  347. // Method: searchData by Matt
  348. // Searches the last names of the given array for the search term parameter.
  349. // Returns the results in a 2D array.
  350. public static String[][] searchArray (String[][] staffData, String searchTerm)
  351. {
  352. // Count the number of results of the search.
  353. int found = 0;
  354. for(int i = 0; i < staffData.length; i++)
  355. {
  356. if(staffData[i][1].toLowerCase().contains(searchTerm))
  357. {
  358. found++;
  359. }
  360. }
  361.  
  362. // Set up a 2d array with the right number of slots for the results
  363. String[][] searchArrayResults = new String[found][6];
  364. int counter = 0;
  365. for(int i = 0; i < staffData.length; i++)
  366. {
  367. if(staffData[i][1].toLowerCase().contains(searchTerm))
  368. {
  369. searchArrayResults[counter][0] = staffData[i][0];
  370. searchArrayResults[counter][1] = staffData[i][1];
  371. searchArrayResults[counter][2] = staffData[i][2];
  372. searchArrayResults[counter][3] = staffData[i][3];
  373. searchArrayResults[counter][4] = staffData[i][4];
  374. searchArrayResults[counter][5] = staffData[i][5];
  375. counter++;
  376. }
  377. }
  378. return searchArrayResults;
  379. }
  380.  
  381.  
  382. //------------------------------------------------------------------------------------------------------------------
  383. // Method: loadFile by Matt
  384. // Read in the csv file and split into a 2D array.
  385. // Returns the data in a 2D array.
  386. public static String[][] loadFile()
  387. {
  388. LinkedList<String> rawData = new LinkedList<String>();
  389.  
  390. // try-catch for IO Exceptions.
  391. try
  392. {
  393. // Set up the BufferedReader.
  394. FileReader reader = new FileReader("/Users/MatthewClisby/Documents/Comp Sci/Programs/JAVA OUTPUTS/STAFF.csv");
  395. BufferedReader br = new BufferedReader (reader);
  396.  
  397. // Put each line of the file into a LinkedList, to allow for varying lengths.
  398. String line;
  399. while ((line = br.readLine()) != null)
  400. {
  401. rawData.add(line);
  402. }
  403. br.close();
  404. }
  405. catch (IOException e)
  406. {
  407. // Output error message to console if there was a problem
  408. System.out.println("There was a problem with reading the file.");
  409. }
  410.  
  411. // Move the data from the LinkedList to the 2D array for use with JTable.
  412. String[][] staffData = new String[rawData.size()][6];
  413. for (int i = 0; i < rawData.size(); i++)
  414. {
  415. String[] splitLine = rawData.get(i).split(",");
  416. for (int j = 0; j < 6; j++)
  417. {
  418. staffData[i][j] = splitLine[j];
  419. }
  420. }
  421. return staffData;
  422. }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement