Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package myfirstapplication;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JTextArea;
- /**
- *
- * @author Cameron
- */
- public class CustomerList {
- ArrayList <Customer> Clients = new ArrayList();
- DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
- public CustomerList() {
- // Clients = null;
- }
- public void SaveToFile(){
- FileWriter writer = null;
- try {
- //clear file
- Utility cf = new Utility();
- String filename = "clients.txt";
- cf.clearTheFile(filename);
- writer = new FileWriter(filename, true);
- System.out.println(Clients+"..."); //new customer is loaded
- for (Customer Client : Clients) {
- //write is null
- writer.write(Client.getFirstName()+System.getProperty("line.separator"));
- writer.write(Client.getSurname()+System.getProperty("line.separator"));
- writer.write(dateFormat.format(Client.getDOB())+System.getProperty("line.separator"));
- writer.write(dateFormat.format(Client.getCustomerSince())+System.getProperty("line.separator"));
- writer.write(Client.getHomeAddress().stringoutput()+System.getProperty("line.separator"));
- writer.write("##"+System.getProperty("line.separator"));
- //write to file
- //add hash
- }
- writer.flush();
- writer.close();
- writer = null;
- //flush and close writer
- } catch (IOException ex) {
- Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- }
- }
- public void LoadFromFile(){
- try {
- String record;
- FileReader reader;
- Customer customer;
- IAddress address;
- reader = new FileReader("clients.txt");
- BufferedReader bin = new BufferedReader(reader);
- record = new String();
- while((record = bin.readLine()) != null){
- String forname = record;
- String surname = bin.readLine();
- String dob = bin.readLine();
- Date dateofbirth = dateFormat.parse(dob);
- String customersince = bin.readLine();
- Date customerSince = dateFormat.parse(customersince);
- String addressname = bin.readLine();
- int addresshouseno = Integer.parseInt(bin.readLine());
- String addressstreet = bin.readLine();
- String addresstown = bin.readLine();
- String addresspostcode = bin.readLine();
- String addressarea = bin.readLine();
- String addresscountry = bin.readLine();
- address = new IAddress(addressname, addressstreet, addresshouseno, addressarea, addresspostcode, addresstown, addresscountry);
- customer = new Customer(forname, surname, dateofbirth, customerSince, address);
- Clients.add(customer);
- String hash = bin.readLine();
- }
- bin.close();
- bin = null;
- } catch (FileNotFoundException ex) {
- Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ParseException ex) {
- Logger.getLogger(CustomerList.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void AddCustomer(Customer newCustomer){
- Clients.add(newCustomer);
- }
- public void DeleteCustomer(Customer newCustomer){
- Clients.remove(newCustomer);
- }
- public void RemoveBySurnameandForename(String Surname, String forename){
- for(int i = 0; i < Clients.size(); i++){
- if(Clients.get(i).getSurname().equals(Surname) && Clients.get(i).getFirstName().equals(forename)){
- Clients.remove(i);
- }
- }
- }
- public void Display(JTextArea jClientsTextArea){
- System.out.println(Clients);
- for(int i = 0; i < Clients.size(); i++){
- Clients.get(i).Display(jClientsTextArea);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement