user1635518
user1635518

Reputation:

Manipulating text files in java

I want to display all the customers stored in text files, but one customer is stored in many text files and he is displayed more then once. How to eliminate duplicates of these customers? I have tried even with HashSet but didn't work. Here is my code.

public class ReadFile {

private int countCustomer = 0;  
private File path;
private List<Customer> customers = new ArrayList<Customer>();

public ReadFile(File file_path) {

    path = file_path;

}

public String[] openFile() throws IOException {

    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr); 
    int numberOfLines = readLines();
    String[] textData = new String[numberOfLines];
    for (int i = 0; i < numberOfLines; i++)
    {
        textData[i] = br.readLine();

    }

    br.close(); 
    return textData;
}

public void arrayCustomer(){

    try{
        String[] array = openFile();    
        for (int j=0; j< array.length;j++)   
        {
            String str = array[j];  
            String[] temp;
          String delimiter = " ";   

          temp = str.split(delimiter);

          String category = temp[0];  
          double balance = Double.parseDouble(temp[1]);   
          int moveNumber = Integer.parseInt(temp[2]);    
          int accountNumber = Integer.parseInt(temp[3]); 

          if (!isIn(new Customer(category, balance, moveNumber, accountNumber)))
          {
        customers.add(new Customer(category, balance, moveNumber, accountNumber)); 
        countCustomer++;
          }
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }

}

public boolean isIn(Customer customer){

    for (int i = 0; i < customers.size(); i++){
        if (customers.get(i).getAccountNumber() == customer.getAccountNumber())
        {
            return true;
        }
    }
    return false;
}

public void listCustomers() {
    for (Customer customer : customers) {
        System.out.print("Category: " + customer.getCategory());
        System.out.print(" Balance: " + customer.getBalance());
        System.out.print(" Moves: " + customer.getMoveNumber());
        System.out.println(" Account number: " + customer.getAccountNumber());
    }
}


public int readLines() throws IOException {

    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);

    int numberOfLines = 0;
    String aLine;

    while((aLine = br.readLine()) != null) 
    {
        numberOfLines++;
    }
        br.close();
        return numberOfLines;
}



// The class with the main method

public class FileList {

public static Scanner scan;
public static ReadFile f;
public static final File folder = new File("C:/My_dir");

public static void main(String[] args) {

    Logger logger = Logger.getLogger("textfiles");
    scan = new Scanner(System.in);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++)
    {   
        File fileEntry = listOfFiles[i];

        if (!fileEntry.isDirectory()) 
        {
            try{
                f = new ReadFile(fileEntry);
                f.openFile();
                f.arrayCustomer();
                f.listCustomers();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
        }
        else
            {
            logger.log(Level.INFO, "The directory is empty!");
            }
    }

Upvotes: 1

Views: 370

Answers (1)

dty
dty

Reputation: 18998

A Set should do the trick if your Customer is correctly defined. You need to make sure your Customer has both an equals() and hashCode() object which operate on the same set of fields and can correctly identify whether two customers are "the same" or not as defined by your business criteria.

Upvotes: 2

Related Questions