Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package myfirstapplication;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.text.DateFormat;
  14. import java.text.ParseException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.swing.JTextArea;
  21.  
  22. /**
  23. *
  24. * @author Cameron
  25. */
  26. public class CustomerList {
  27.  
  28.  
  29. ArrayList <Customer> Clients = new ArrayList();
  30. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  31.  
  32. public CustomerList() {
  33.  
  34. // Clients = null;
  35. }
  36.  
  37.  
  38.  
  39. public void SaveToFile(){
  40.  
  41. FileWriter writer = null;
  42. try {
  43. //clear file
  44. Utility cf = new Utility();
  45. String filename = "clients.txt";
  46. cf.clearTheFile(filename);
  47. writer = new FileWriter(filename, true);
  48. System.out.println(Clients+"..."); //new customer is loaded
  49. for (Customer Client : Clients) {
  50. //write is null
  51.  
  52. writer.write(Client.getFirstName()+System.getProperty("line.separator"));
  53. writer.write(Client.getSurname()+System.getProperty("line.separator"));
  54. writer.write(dateFormat.format(Client.getDOB())+System.getProperty("line.separator"));
  55. writer.write(dateFormat.format(Client.getCustomerSince())+System.getProperty("line.separator"));
  56. writer.write(Client.getHomeAddress().stringoutput()+System.getProperty("line.separator"));
  57. writer.write("##"+System.getProperty("line.separator"));
  58.  
  59.  
  60. //write to file
  61. //add hash
  62. }
  63.  
  64. writer.flush();
  65. writer.close();
  66. writer = null;
  67. //flush and close writer
  68. } catch (IOException ex) {
  69. Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
  70. } finally {
  71.  
  72. }
  73. }
  74.  
  75.  
  76.  
  77.  
  78. public void LoadFromFile(){
  79.  
  80.  
  81. try {
  82. String record;
  83. FileReader reader;
  84. Customer customer;
  85. IAddress address;
  86.  
  87. reader = new FileReader("clients.txt");
  88. BufferedReader bin = new BufferedReader(reader);
  89. record = new String();
  90. while((record = bin.readLine()) != null){
  91.  
  92. String forname = record;
  93. String surname = bin.readLine();
  94. String dob = bin.readLine();
  95. Date dateofbirth = dateFormat.parse(dob);
  96. String customersince = bin.readLine();
  97. Date customerSince = dateFormat.parse(customersince);
  98.  
  99. String addressname = bin.readLine();
  100. int addresshouseno = Integer.parseInt(bin.readLine());
  101. String addressstreet = bin.readLine();
  102. String addresstown = bin.readLine();
  103. String addresspostcode = bin.readLine();
  104. String addressarea = bin.readLine();
  105. String addresscountry = bin.readLine();
  106.  
  107.  
  108. address = new IAddress(addressname, addressstreet, addresshouseno, addressarea, addresspostcode, addresstown, addresscountry);
  109. customer = new Customer(forname, surname, dateofbirth, customerSince, address);
  110. Clients.add(customer);
  111. String hash = bin.readLine();
  112.  
  113. }
  114.  
  115.  
  116. bin.close();
  117. bin = null;
  118. } catch (FileNotFoundException ex) {
  119. Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
  120. } catch (IOException ex) {
  121. Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
  122. } catch (ParseException ex) {
  123. Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
  124. }
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133. public void AddCustomer(Customer newCustomer){
  134.  
  135. Clients.add(newCustomer);
  136. }
  137.  
  138. public void DeleteCustomer(Customer newCustomer){
  139.  
  140. Clients.remove(newCustomer);
  141. }
  142. public void RemoveBySurnameandForename(String Surname, String forename){
  143.  
  144. for(int i = 0; i < Clients.size(); i++){
  145. if(Clients.get(i).getSurname().equals(Surname) && Clients.get(i).getFirstName().equals(forename)){
  146. Clients.remove(i);
  147. }
  148. }
  149.  
  150. }
  151.  
  152. public void Display(JTextArea jClientsTextArea){
  153.  
  154. System.out.println(Clients);
  155.  
  156. for(int i = 0; i < Clients.size(); i++){
  157. Clients.get(i).Display(jClientsTextArea);
  158. }
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement