Messaging-App / src / Seller.java
Seller.java
Raw
import java.io.*;
import java.util.ArrayList;

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

public class Seller extends User {

    private String sellerID;
    private static final String FILE_NAME = "stores.txt";
    private ArrayList<Store> stores = new ArrayList<>();


    public Seller(String fullName, String email, String password, String userType)
            throws IOException {
        super(fullName, email, password, userType);
        int id = generateUniqueID(MessagingServer.getUsedSellerIDs());
        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)) {
                        sellerID = line.split(",")[4];
                    }
                    line = br.readLine();
                }
            }
        } else {
            MessagingServer.addToUsedSellerIDs(id);
            sellerID = "SELLER" + id;
        }
        this.readStore();

    }

    public String getSellerID() {
        return sellerID;
    }

    public void setStores(ArrayList<Store> stores) {
        this.stores = stores;
    }
    public ArrayList<Store> getStores() {
        return stores;
    }

//    public void createStore(Scanner scanner) throws IOException {
//        String storeName;
//        String storeDescription;
//
//        while (true) {
//            System.out.println("Please enter a name for your store (-1 to return):");
//            storeName = scanner.nextLine();
//            if (storeName.equals("-1")) {
//                return;
//            }
//            if (!Store.isStoreNameUnique(fileName, storeName)) {
//                System.out.println("That store name is already taken! Please try another one.");
//                continue;
//            }
//
//            if (storeName.equals(sellerID)) {
//                System.out.println("Your store name cannot be the same as your sellerID");
//                continue;
//            }
//
//            if (storeName.equals("")) {
//                System.out.println("A store name cannot be empty!");
//                continue;
//            }
//            break;
//        }
//
//        while (true) {
//            System.out.println("Please enter a brief description for your store:");
//            storeDescription = scanner.nextLine();
//
//            if (storeDescription.equals("")) {
//                System.out.println("A store description cannot be empty!");
//                continue;
//            }
//            break;
//        }
//
//
//        Store newStore = new Store(storeName, storeDescription, sellerID);
//        stores.add(newStore);
//        newStore.writeStoreToFile();
//    }

    public static String getFileName() {
        return FILE_NAME;
    }

    public void readStore() throws IOException {
        synchronized (MessagingServer.STORES_GATEKEEPER) {
            try {
                File f = new File(FILE_NAME);
                FileReader fr = new FileReader(f);
                BufferedReader bfr = new BufferedReader(fr);
                while (true) {
                    String line = bfr.readLine();
                    if (line == null) {
                        break;
                    }

                    if (line.split(",")[2].equals(sellerID)) {
                        stores.add(new Store(line.split(",")[0],
                                line.split(",")[1], line.split(",")[2], line.split(",")[4]));
                    }
                }

                bfr.close();
            } catch (FileNotFoundException e) {
                //no need to do anything if file is not found
            }
        }


    }

}