Guest User

Untitled

a guest
May 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 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. }
Add Comment
Please, Sign In to add comment