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

/**
 * Purdue CS180 - Summer 22 - Project 5 - Store Class
 *
 * This class stores all the fields and methods a seller can
 * use to operate their store
 *
 * @author Ivan Yang, Jun Shern Lim, Shaofeng Yuan
 *
 * @version 31st July 2022
 */

public class Store {
    private String storeName;            //name of store
    private String storeDescription;     //description of store
    private String sellerID;              //seller associated with store
    private String storeID;               //store's uniqueID
    private String sellerName;            //seller's name
    private LocalDateTime mostRecentMsgRecvd = LocalDateTime.MIN;   //store's most recent message received time
    private LocalDateTime mostRecentMsgSent = LocalDateTime.MIN;    //store's most recent message sent time


    public Store(String storeName, String storeDescription, String sellerID, String sellerName) {
        this.storeName = storeName;
        this.storeDescription = storeDescription;
        this.sellerID = sellerID;
        this.sellerName = sellerName;
        storeID = generateUniqueStoreID();
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public void setMostRecentMsgRecvd(LocalDateTime mostRecentMsgRecvd) {
        this.mostRecentMsgRecvd = mostRecentMsgRecvd;
    }

    public LocalDateTime getMostRecentMsgRecvd() {
        return mostRecentMsgRecvd;
    }

    public LocalDateTime getMostRecentMsgSent() {
        return mostRecentMsgSent;
    }

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

    public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    public String getSellerID() {
        return sellerID;
    }

    public void setSellerID(String sellerID) {
        this.sellerID = sellerID;
    }

    public String getStoreDescription() {
        return storeDescription;
    }

    public void setStoreDescription(String storeDescription) {
        this.storeDescription = storeDescription;
    }

    public void setStoreID(String storeID) {
        this.storeID = storeID;
    }

    public String getStoreID() {
        return storeID;
    }


    //Receives a Store object and write its info into the stores file.
    public void writeStoreToFile() throws IOException {
        synchronized (MessagingServer.STORES_GATEKEEPER) {
            File f = new File(Seller.getFileName());
            FileOutputStream fos = new FileOutputStream(f, true);
            PrintWriter pw = new PrintWriter(fos);

            pw.printf("%s,%s,%s,%s,%s\n", storeName, storeDescription, sellerID, storeID, sellerName);
            pw.close();
        }

    }

    //checks if a store name is unique
    public static boolean isStoreNameUnique(String storeName) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("stores.txt"))) {
            String line;
            line = br.readLine();

            while (line != null) {
                String[] lineSplit = line.split(",");
                if (storeName.equalsIgnoreCase(lineSplit[0])) {
                    return false;
                }
                line = br.readLine();
            }

        } catch (FileNotFoundException e) {
            //if no file is found, the store must be unique since it's the first store -> return true
            return true;
        }
        return true;
    }

    //generates a unique storeID
    public String generateUniqueStoreID() {
        String[] splitStoreName = storeName.split(" ");
        String returnStoreName = String.join("", splitStoreName);
        return String.format("%s_%s", sellerID, returnStoreName.toUpperCase());
    }

    //returns an integer array list of message counts received by all customers from one store
    public ArrayList<Integer> getStoreMessageCounts(ArrayList<Customer> customers) throws IOException {
        ArrayList<Integer> count = new ArrayList<>();
        int curCount = 0;
        for (Customer customer : customers) {
            synchronized (Objects.requireNonNull(MessagingServer.getCustomerGatekeeper(customer.getID()))) {
                ArrayList<Message> messages = User.getMsg(
                        String.format("%s_%s.txt", storeID, customer.getCustomerID()));
                if (messages.size() != 0) {
                    for (Message message : messages) {
                        if (!message.getSenderID().equals(storeID)) {
                            curCount += 1;
                        }
                    }
                }
                messages = User.getMsg(String.format("%s_%s_new.txt", storeID, customer.getCustomerID()));
                if (messages.size() != 0) {
                    for (Message message : messages) {
                        if (!message.getSenderID().equals(storeID)) {
                            curCount += 1;
                        }
                    }
                }
            }

            count.add(curCount);
            curCount = 0;
        }

        if (count.size() != customers.size()) {
            System.out.println("Error occurred in getStoreStats");
            return null;
        }

        return count;
    }

    //returns a string of sorted store statistics
    public static String[] sortStoreStats(String[] storeStats, ArrayList<Integer> count,
                                              int sortOrder) {

        // Sorting message with respect to sort order
        Collections.sort(count);
        String customerMessages = "";
        if (sortOrder == 3) {
            Collections.reverse(count);
        }


        //copy storeStats
        String[] copyStoreStats = storeStats.clone();

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