Guest User

Untitled

a guest
May 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. public LoyaltySchemeMenu() {
  2.  
  3.  
  4.  
  5. dataFile = new File("C:\\Users/James/Downloads/CustomerLoyaltySchemeFullSolution/customerData.dat");
  6. readDataFromFile();
  7. listModel = new DefaultListModel();
  8. //customers = new CustomerData().createTestData();
  9. refreshListModel();
  10. initComponents();
  11. }
  12.  
  13. public static void refreshListModel() {
  14. String[] names = customers.getCustomerNames();
  15. listModel.clear();
  16.  
  17. for (String n : names) {
  18. listModel.addElement(n);
  19. }
  20. }
  21.  
  22. private void readDataFromFile() {
  23. try {
  24. inputFile = new FileInputStream(dataFile);
  25. objectInput = new ObjectInputStream(inputFile);
  26. customers = (CustomerList) objectInput.readObject();
  27. objectInput.close();
  28. } catch (IOException e) {
  29. JOptionPane.showMessageDialog(this, e.getMessage(),
  30. "Cannot read from file", JOptionPane.ERROR_MESSAGE);
  31. } catch (ClassNotFoundException e) {
  32.  
  33.  
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. private void writeDataToFile() {
  39. try {
  40. outputFile = new FileOutputStream(dataFile);
  41. objectOutput = new ObjectOutputStream(outputFile);
  42. objectOutput.writeObject(customers);
  43. objectOutput.flush();
  44. objectOutput.close();
  45. } catch (IOException e) {
  46. JOptionPane.showMessageDialog(this, e.getMessage(),
  47. "Cannot Write to File", JOptionPane.ERROR_MESSAGE);
  48. }
  49. }
  50.  
  51. private void deleteCustomer() {
  52.  
  53. int position = custListbox.getSelectedIndex();
  54. if (position == -1) {
  55. JOptionPane.showMessageDialog(this,
  56. "Please select a customer",
  57. "No selection", JOptionPane.ERROR_MESSAGE);
  58.  
  59. } else {
  60. int confirm = JOptionPane.showConfirmDialog(this,
  61. "Are you sure you want to delete "
  62. + customers.getCustomerAt(position).getFullName(),
  63. "Confirm delete", JOptionPane.YES_NO_OPTION);
  64. if (confirm == JOptionPane.YES_OPTION) {
  65. customers.removeCustomerAt(position);
  66. refreshListModel();
  67. clearMainForm();
  68. }
  69. }
  70.  
  71.  
  72.  
  73. }
  74.  
  75. private void clearMainForm() {
  76.  
  77. nameField.setText("");
  78. telField.setText("");
  79. emailField.setText("");
  80. curPointsField.setText("");
  81. ltPointsField.setText("");
  82. }
  83.  
  84. public static void refreshDetails() {
  85.  
  86. nameField.setText(selected.getFullName());
  87. telField.setText(selected.getPhoneNo());
  88. emailField.setText(selected.getEmailAddress().getAddress());
  89. }
  90.  
  91. public static void updatePointsFields() {
  92.  
  93. LoyaltySchemeMenu.curPointsField.setText(Integer.toString(LoyaltySchemeMenu.selected.getCurrentPoints()));
  94. LoyaltySchemeMenu.ltPointsField.setText(Integer.toString(LoyaltySchemeMenu.selected.getLifetimePoints()));
  95. }
  96.  
  97. private void btnSaveCloseUcdActionPerformed(java.awt.event.ActionEvent evt) {
  98.  
  99. newSurname = newSnameFieldUcd.getText();
  100. newFname = newFnameFieldUcd.getText();
  101. newTelNo = newTelNoFieldUcd.getText();
  102. newEmail = newEmailFieldUcd.getText();
  103.  
  104.  
  105. if (!newSurname.equals("") && !newFname.equals("") && !newTelNo.equals("") && !newEmail.equals("")) {
  106. if (EmailAddress.validEmail(newEmail)) {
  107.  
  108. LoyaltySchemeMenu.selected.setSurname(newSurname);
  109. LoyaltySchemeMenu.selected.setFirstName(newFname);
  110. LoyaltySchemeMenu.selected.setPhoneNo(newTelNo);
  111. LoyaltySchemeMenu.selected.changeEmailAddress(newEmail);
  112.  
  113. LoyaltySchemeMenu.refreshDetails();
  114. JOptionPane.showMessageDialog(this, "Details updated for account: " + LoyaltySchemeMenu.selected.getFullName());
  115. this.setVisible(false);
  116. } else {
  117. JOptionPane.showMessageDialog(this, "Invalid Email Address Please Re-Enter", "Invalid format", JOptionPane.ERROR_MESSAGE);
  118. }
  119. } else {
  120. JOptionPane.showMessageDialog(this, "All fields must contain a value", "Invalid entry", JOptionPane.ERROR_MESSAGE);
  121. }
  122. }
  123.  
  124. private void btnCancelUcdActionPerformed(java.awt.event.ActionEvent evt) {
  125.  
  126. this.setVisible(false);
  127. }
  128.  
  129. private void btnResetUcdActionPerformed(java.awt.event.ActionEvent evt) {
  130.  
  131. newSnameFieldUcd.setText("");
  132. newFnameFieldUcd.setText("");
  133. newTelNoFieldUcd.setText("");
  134. newEmailFieldUcd.setText("");
  135. }
Add Comment
Please, Sign In to add comment