Guest User

Untitled

a guest
Dec 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. // write all customers in the array list
  2. // to the file
  3. for (Customer c : customers) {
  4.  
  5. public class CustomerTextFile implements CustomerDAO{
  6. private static final String FIELD_SEP = "t";
  7. private static final Path customersPath = Paths.get("customers.txt");
  8. private static final File customersFile = customersPath.toFile();
  9. private ArrayList<Customer> customers = getCustomers();
  10.  
  11.  
  12. // prevent instantiation of the class
  13. CustomerTextFile() {}
  14.  
  15.  
  16. public ArrayList<Customer> getCustomers() {
  17. // if the customers file has already been read, don't read it again
  18. if (customers != null) {
  19. return customers;
  20. }
  21.  
  22. customers = new ArrayList<>();
  23.  
  24. if (Files.exists(customersPath)) { // prevent the FileNotFoundException
  25. try (BufferedReader in = new BufferedReader(
  26. new FileReader(customersFile))) {
  27. // read all customers stored in the file
  28. // into the array list
  29. String line;
  30. while ((line = in.readLine()) != null) {
  31. String[] columns = line.split(FIELD_SEP);
  32. String firstName = columns[0];
  33. String lastName = columns[1];
  34. String email = columns[2];
  35.  
  36. Customer p = new Customer(
  37. firstName, lastName, email);
  38.  
  39. customers.add(p);
  40. }
  41. } catch (IOException e) {
  42. System.out.println(e);
  43. return null;
  44. }
  45. }
  46. return customers;
  47. }
  48.  
  49. public Customer getCustomer(String email) {
  50. for (Customer c : customers) {
  51. if (c.getEmail().equals(email)) {
  52. return c;
  53. }
  54. }
  55. return null;
  56. }
  57.  
  58. public boolean addCustomer(Customer c) {
  59. customers.add(c);
  60. return saveCustomers();
  61. }
  62.  
  63. public boolean deleteCustomer(Customer c) {
  64. customers.remove(c);
  65. return saveCustomers();
  66. }
  67.  
  68. public boolean updateCustomer(Customer newCustomer) {
  69. // get the old customer and remove it
  70. Customer oldCustomer = getCustomer(newCustomer.getEmail());
  71. int i = customers.indexOf(oldCustomer);
  72. customers.remove(i);
  73.  
  74. // add the updated customer
  75. customers.add(i, newCustomer);
  76.  
  77. return saveCustomers();
  78. }
  79.  
  80. private static boolean saveCustomers() {
  81. try (PrintWriter out = new PrintWriter(
  82. new BufferedWriter(
  83. new FileWriter(customersFile)))) {
  84.  
  85. // write all customers in the array list
  86. // to the file
  87. for (Customer c : customers) {
  88. out.print(c.getFirstName() + FIELD_SEP);
  89. out.print(c.getLastName() + FIELD_SEP);
  90. out.println(c.getEmail());
  91. }
  92. } catch (IOException e) {
  93. System.out.println(e);
  94. return false;
  95. }
  96.  
  97. return true;
  98. }
  99. }
Add Comment
Please, Sign In to add comment