Messaging-App / src / Customer.java
Customer.java
Raw
import java.io.*;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Purdue CS180 - Summer 22 - Project 5 - Customer Class
 *
 * This file contains all the methods that a customer will use, including methods
 * inherited from its parent class, User.
 *
 * @author Ivan Yang, Jun Shern Lim, Shaofeng Yuan
 *
 * @version 31st July 2022
 */

public class Customer extends User {
    //test
    private String customerID;
    private LocalDateTime mostRecentMsgSent = null;
    private final int id; //customer's id number -> used for gatekeeping


    public Customer(String fullName, String email, String password, String userType) throws IOException {
        super(fullName, email, password, userType);
        id = generateUniqueID(MessagingServer.getUsedCustomerIDs());
        if (!User.isEmailUnique(email)) {
            try (BufferedReader br =  new BufferedReader(new FileReader("loginDetails.txt"))) {
                String line;
                line = br.readLine();
                while (line != null) {
                    if (line.split(",")[1].equals(email)) {
                        customerID = line.split(",")[4];
                    }
                    line = br.readLine();
                }
            }
        } else {
            MessagingServer.addToUsedCustomerIDs(id);
            customerID = "CUSTOMER" + id;
        }

    }


    public int getID() {
        return id;
    }

    public LocalDateTime getMostRecentMsgSent() {
        return mostRecentMsgSent;
    }
    public void setMostRecentMsgSent(LocalDateTime mostRecentMsgSent) {
        this.mostRecentMsgSent = mostRecentMsgSent;
    }

    public String getCustomerID() {
        return customerID;
    }

    public ArrayList<Integer> getNumMsgSentToStores(ArrayList<Store> stores) throws IOException {
        ArrayList<Integer> countOfMessages = new ArrayList<>();
        int curCount = 0;   //current count of messages sent to a store
        for (Store store : stores) {
            synchronized (Objects.requireNonNull(MessagingServer.getCustomerGatekeeper(id))) {
                ArrayList<Message> messages = User.getMsg(String.format("%s_%s.txt", customerID, store.getStoreID()));
                if (messages.size() != 0) {
                    for (Message message : messages) {
                        if (message.getSenderID().equals(customerID)) {
                            curCount++;
                        }
                    }
                }
                messages = User.getMsg(String.format("%s_%s_new.txt", customerID, store.getStoreID()));
                if (messages.size() != 0) {
                    for (Message message : messages) {
                        if (message.getSenderID().equals(customerID)) {
                            curCount++;
                        }
                    }
                }
            }
            countOfMessages.add(curCount);
            curCount = 0;
        }
        return countOfMessages;
    }

    public ArrayList<Integer> getNumMsgReceivedFromStores(ArrayList<Store> stores) throws IOException {
        ArrayList<Integer> countOfMessages = new ArrayList<>();
        int curCount = 0;   //current count of messages received by a store
        for (Store store : stores) {
            synchronized (Objects.requireNonNull(MessagingServer.getCustomerGatekeeper(id))) {
                ArrayList<Message> messages = User.getMsg(String.format("%s_%s.txt", customerID, store.getStoreID()));
                if (messages.size() != 0) {
                    for (Message message : messages) {
                        if (!message.getSenderID().equals(customerID)) {
                            curCount++;
                        }
                    }
                }
                messages = User.getMsg(String.format("%s_%s_new.txt", customerID, store.getStoreID()));
                if (messages.size() != 0) {
                    for (Message message : messages) {
                        if (!message.getSenderID().equals(customerID)) {
                            curCount++;
                        }
                    }
                }
            }
            countOfMessages.add(curCount);
            curCount = 0;
        }
        return countOfMessages;
    }

    //returns a sorted array of messages received from stores
    public static String[] sortMessagesReceived(ArrayList<Integer> countOfMessagesReceived,
                                         String[] messagesReceivedFromStores,
                                         int sortOrder) {
        // Sorting message with respect to sort order
        Collections.sort(countOfMessagesReceived);
        String customerMessages = "";
        if (sortOrder == 6) {
            Collections.reverse(countOfMessagesReceived);
        }

        String[] copyMessagesReceived = messagesReceivedFromStores.clone();

        // Putting the messages back into one string and returning it
        String[] sorted = new String[countOfMessagesReceived.size()];
        int indexToRemove = 0;
        for (int i = 0; i < countOfMessagesReceived.size(); i++) {
            for (int j = 0; j < copyMessagesReceived.length; j++) {
                if (copyMessagesReceived[j].equals("")) {
                    continue;
                }
                String[] split = copyMessagesReceived[j].split( " ");
                //compare the value in count (sorted) to the value in the string
                if (split[split.length - 1].equals(String.valueOf(countOfMessagesReceived.get(i)))) {
                    sorted[i] = copyMessagesReceived[j];
                    indexToRemove = j;
                    break;
                }
            }
            copyMessagesReceived[indexToRemove] = "";
        }
        return sorted;
    }


    //returns a sorted array of messages sent to stores
    public static String[] sortMessagesSent(ArrayList<Integer> countOfMessagesSent,
                                         String[] messagesSentToStores,
                                         int sortOrder) {
        // Sorting message with respect to sort order
        Collections.sort(countOfMessagesSent);
        String customerMessages = "";
        if (sortOrder == 3) {
            Collections.reverse(countOfMessagesSent);
        }

        String[] copyOfMessagesSent = messagesSentToStores.clone();

        // Putting the messages back into one string and returning it
        String[] sorted = new String[countOfMessagesSent.size()];
        int indexToRemove = 0;
        for (int i = 0; i < countOfMessagesSent.size(); i++) {
            for (int j = 0; j < copyOfMessagesSent.length; j++) {
                if (copyOfMessagesSent[j].equals("")) {
                    continue;
                }
                String[] split = copyOfMessagesSent[j].split( " ");
                //compare the value in count (sorted) to the value in the string
                if (split[split.length - 1].equals(String.valueOf(countOfMessagesSent.get(i)))) {
                    sorted[i] = copyOfMessagesSent[j];
                    indexToRemove = j;
                    break;
                }
            }
            copyOfMessagesSent[indexToRemove] = "";
        }
        return sorted;
    }




}