import java.io.*; import java.util.ArrayList; /** * Purdue CS180 - Summer 22 - Project 5 - User Class *

* This file contains all the methods that any user of this program will use, * excluding methods for specific roles. It is the parent class of * Customer and Seller * * @author Ivan Yang, Jun Shern Lim, Shaofeng Yuan * @version 31st July 2022 */ public class User { private String fullName; //user's full name private String email; //user's email private String password; //user's password private String userType; //type of user: seller or customer public User(String fullName, String email, String password, String userType) { this.fullName = fullName; this.email = email; this.password = password; this.userType = userType; } //just to initialize a User object to access its methods public User() { } //generates a unique ID (an int >= 0) for a User public int generateUniqueID(ArrayList takenIDs) { int uniqueID = 0; while (takenIDs.contains(uniqueID)) { uniqueID++; } return uniqueID; } //verify that the user's sign up email is unique, can also use to check if account exists public static boolean isEmailUnique(String email) throws IOException { synchronized (MessagingServer.USER_DETAILS_GATEKEEPER) { try (BufferedReader br = new BufferedReader(new FileReader("loginDetails.txt"))) { String line; do { line = br.readLine(); if (line == null) { break; } if (line.contains(email)) { return false; } } while (true); } catch (FileNotFoundException e) { //file not found means it's the first user -> return true return true; } } return true; } public static boolean isEmailValid(String email) { if (email.indexOf('@') == -1) { return false; } int indexAt = email.indexOf('@'); String domain = email.substring(indexAt + 1); if (!(email.contains("@") && domain.contains("."))) { return false; } return !email.contains(","); } public static int isNameValid(String name) { if (name.equals("")) { return 1; } if (name.contains(",") || name.contains("@") || name.contains("\\")) { return 2; } return 0; } public static boolean isPasswordValid(String passwordToCheck) { if (passwordToCheck.length() < 3) { return false; } return !passwordToCheck.contains(","); } //check credentials of a user by comparing data to a file. will also initialize the fields of a user object //returns: //0 - if email is not found //1 - if password is incorrect //2 - if everything is correct public int checkCredentials(String emailToCheck, String passwordToCheck) throws IOException { String line; synchronized (MessagingServer.USER_DETAILS_GATEKEEPER) { try (BufferedReader br = new BufferedReader(new FileReader("loginDetails.txt"))) { do { line = br.readLine(); if (line == null) { //if email is not found, then return false br.close(); return 0; } if (line.split(",")[1].equals(emailToCheck)) { //ensures the next line is not read, will be easy to check password br.close(); break; } } while (true); } catch (FileNotFoundException e) { //email not found since file does not exist return 0; } } boolean passwordCheck; String[] splitLine = line.split(","); passwordCheck = splitLine[2].equals(passwordToCheck); if (!passwordCheck) { return 1; } //initializes the user's name and user type fullName = splitLine[0]; userType = splitLine[3]; return 2; } public String getEmail() { return email; } public String getPassword() { return password; } public String getFullName() { return fullName; } public String getUserType() { return userType; } public void setFullName(String fullName) { this.fullName = fullName; } public void setPassword(String password) { this.password = password; } public void setEmail(String email) { this.email = email; } public void setUserType(String userType) { this.userType = userType; } //returns number of new messages for a particular user //myID is either customerID or sellerID (don't need storeID cuz it's just sellerID + storeName, so it's enough //to check sellerID) public static int getNewMsgCountTotal(String myID) throws IOException { String line; int newMsgCount = 0; try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.indexOf(myID) == 0) { newMsgCount += Integer.parseInt(line.split(",")[1]); } line = br.readLine(); } } catch (FileNotFoundException e) { //if file is not found, there will be 0 new messages return 0; } return newMsgCount; } //returns the number of new messages a store receives public static int getNewMsgCountPerStore(String myID, String theirID) throws IOException { String line; int newMsgCount = 0; try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.split(",")[0].equals(myID + "_" + theirID + ".txt")) { newMsgCount += Integer.parseInt(line.split(",")[1]); } line = br.readLine(); } } catch (FileNotFoundException e) { //if file is not found, there will be 0 new messages return 0; } return newMsgCount; } //returns the number of new messages a user receives public static int getNewMsgCountPerUser(String myID, String theirID) throws IOException { String line; int newMsgCount = 0; try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.indexOf(myID) == 0 && line.contains(myID) && line.contains(theirID)) { newMsgCount += Integer.parseInt(line.split(",")[1]); } line = br.readLine(); } } catch (FileNotFoundException e) { //if file is not found, there will be 0 new messages return 0; } return newMsgCount; } //returns the number of new disappearing messages in total public static int getDisapMsgCountTotal(String myID) throws IOException { String line; int disappearingMsgCount = 0; try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.indexOf(myID) == 0) { disappearingMsgCount += Integer.parseInt(line.split(",")[2]); } line = br.readLine(); } } catch (FileNotFoundException e) { //if file is not found, there will be 0 new messages return 0; } return disappearingMsgCount; } //returns the number of new disappearing messages a store receives public static int getDisapMsgCountPerStore(String myID, String theirID) throws IOException { String line; int disappearingMsgCount = 0; try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.indexOf(myID + "_" + theirID) == 0) { disappearingMsgCount += Integer.parseInt(line.split(",")[2]); } line = br.readLine(); } } catch (FileNotFoundException e) { //if file is not found, there will be 0 new messages return 0; } return disappearingMsgCount; } //returns the number of new disappearing messages a user receives public static int getDisapMsgCountPerUser(String myID, String theirID) throws IOException { String line; int disappearingMsgCount = 0; try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.indexOf(myID) == 0 && line.contains(myID) && line.contains(theirID)) { disappearingMsgCount += Integer.parseInt(line.split(",")[2]); } line = br.readLine(); } } catch (FileNotFoundException e) { //if file is not found, there will be 0 new messages return 0; } return disappearingMsgCount; } //removes number of new messages, ie sets it to 0 //myID is either customerID or storeID //storeID is either customerID or storeID public static void removeNewMsgCount(String myID, String theirID) throws IOException { String line; //line to read in String file = ""; //updated contents of file String fileName = String.format("%s_%s.txt", myID, theirID); //file name with potential new messages try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.split(",")[0].indexOf(fileName) == 0) { //replace new message count with disappearing message count line = line.split(",")[0] + "," + 0 + "," + line.split(",")[2]; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //if file not found, that means there are no new messages to remove return; } try (PrintWriter pw = new PrintWriter(new FileOutputStream("newMsgCount.txt"))) { pw.print(file); } } //removes number of new disappearing messages, ie sets it to 0 public static void removeDisMsgCount(String myID, String theirID) throws IOException { String line; //line to read in String file = ""; //updated contents of file String fileName = String.format("%s_%s.txt", myID, theirID); //file name with potential new messages try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { line = br.readLine(); while (line != null) { if (line.split(",")[0].indexOf(fileName) == 0) { //replace old count with new one, dis count = 0 line = line.split(",")[0] + "," + line.split(",")[1] + "," + 0; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //if file not found, that means there are no new messages to remove return; } try (PrintWriter pw = new PrintWriter(new FileOutputStream("newMsgCount.txt"))) { pw.print(file); } } public static ArrayList getMsg(String myID, String theirID) throws IOException { ArrayList messages = new ArrayList<>(); synchronized (MessagingServer.NEW_MSG_GATEKEEPER) { removeNewMsgCount(myID, theirID); removeDisMsgCount(myID, theirID); } String type = "customer"; if (!(myID.split("_")[myID.split("_").length - 1].equals("_C"))) type = "store"; try (var br = new BufferedReader(new FileReader(String.format("%s_%s.txt", myID, theirID)))) { String line1 = br.readLine(); String line2 = br.readLine(); while (line2 != null) { messages.add(new Message(line1.split(",")[0], line1.split(",")[1], line2, line1.split(",")[2], false, false)); //lineNumber++; line1 = br.readLine(); line2 = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything } try (var br = new BufferedReader(new FileReader(String.format("%s_%s_new.txt", myID, theirID)))) { String line1 = br.readLine(); String line2 = br.readLine(); while (line2 != null) { if (line1.split(",")[3].equals("D")) { messages.add(new Message(line1.split(",")[0], line1.split(",")[1], line2, line1.split(",")[2], true, true)); //lineNumber++; } else { messages.add(new Message(line1.split(",")[0], line1.split(",")[1], line2, line1.split(",")[2], false, true)); } line1 = br.readLine(); line2 = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything } return (messages); } public static ArrayList getMsg(String fileName) throws IOException { ArrayList messages = new ArrayList<>(); if (fileName.substring(fileName.length() - 7, fileName.length()).equals("new.txt")) { try (var br = new BufferedReader(new FileReader(fileName))) { String line1 = br.readLine(); String line2 = br.readLine(); while (line2 != null) { if (line1.split(",")[3].equals("D")) { messages.add(new Message(line1.split(",")[0], line1.split(",")[1], line2, line1.split(",")[2], true, true)); //lineNumber++; } else { messages.add(new Message(line1.split(",")[0], line1.split(",")[1], line2, line1.split(",")[2], false, true)); } line1 = br.readLine(); line2 = br.readLine(); } } catch (FileNotFoundException e) { //don't do anything } } else { try (var br = new BufferedReader(new FileReader(fileName))) { String line1 = br.readLine(); String line2 = br.readLine(); while (line2 != null) { messages.add(new Message(line1.split(",")[0], line1.split(",")[1], line2, line1.split(",")[2], false, false)); //lineNumber++; line1 = br.readLine(); line2 = br.readLine(); } } catch (FileNotFoundException e) { //don't do anything } } return messages; } //sends the message to a store/customer, the contents are written to a file public static void sendMsg(Message toSend, String receiverID) throws IOException { // Getting new message String senderName = toSend.getSenderName(); String senderID = toSend.getSenderID(); // Printing message in both files try (var pw = new PrintWriter(new FileOutputStream(String.format("%s_%s_new.txt", senderID, receiverID), true))) { if (toSend.isDisappearing()) { pw.printf("%s,%s,%s,D\n", senderName, senderID, toSend.getDateTime()); } else { pw.printf("%s,%s,%s,N\n", senderName, senderID, toSend.getDateTime()); } pw.printf("%s\n", toSend.getMessageContents()); } try (var pw = new PrintWriter(new FileOutputStream(String.format("%s_%s_new.txt", receiverID, senderID), true))) { if (toSend.isDisappearing()) { pw.printf("%s,%s,%s,D\n", senderName, senderID, toSend.getDateTime()); } else { pw.printf("%s,%s,%s,N\n", senderName, senderID, toSend.getDateTime()); } pw.printf("%s\n", toSend.getMessageContents()); } trackNewMsg(1, 0, senderID, receiverID); } //flushes messages from new message file to actual message storage public static void flushMsg(String senderID, String receiverID) throws IOException { int numLines = 0; //total number of lines read from files String line; String nextLine; try (BufferedReader br = new BufferedReader(new FileReader(String.format( "%s_%s_new.txt", senderID, receiverID)))) { line = br.readLine(); nextLine = br.readLine(); while (nextLine != null) { numLines++; if (line.split(",")[3].equals("N")) { // Printing message in both files try (var pw = new PrintWriter(new FileOutputStream( String.format("%s_%s.txt", senderID, receiverID), true))) { //pw.printf("%s,%s,%s\n", senderName, senderID, MessagingApp.getCurrentDateTime()); pw.printf("%s\n%s\n", line, nextLine); } // try (var pw = new PrintWriter(new FileOutputStream( // String.format("%s_%s.txt", receiverID, senderID), // true))) { // //pw.printf("%s,%s,%s\n", senderName, senderID, MessagingApp.getCurrentDateTime()); // pw.printf("%s\n%s\n", line, nextLine); // } } line = br.readLine(); nextLine = br.readLine(); } } catch (FileNotFoundException e) { System.out.println("File to be flushed does not exist!"); //continue; } try (PrintWriter pw = new PrintWriter( new FileOutputStream(String.format("%s_%s_new.txt", senderID, receiverID)))) { pw.print(""); } } //tracks the number of new messages a store/customer receives private static void trackNewMsg(int newCount, int disapCount, String senderID, String receiverID) throws IOException { // Read file String file = ""; int getNewCount; int getDisapCount; boolean neww = false; String toAdd = String.format("%s_%s.txt,%d,%d\n", receiverID, senderID, newCount, disapCount); try (var br = new BufferedReader(new FileReader("newMsgCount.txt"))) { String line = br.readLine(); while (line != null) { if (line.split(",")[0].equals(String.format("%s_%s.txt", receiverID, senderID))) { getNewCount = Integer.parseInt(line.split(",")[1]); getNewCount += newCount; getDisapCount = Integer.parseInt(line.split(",")[2]); getDisapCount += disapCount; line = line.split(",")[0] + "," + getNewCount + "," + getDisapCount; neww = true; } file += line + "\n"; line = br.readLine(); } //if we could not find a line to add new messages to, append it to the end of file if (!neww) file += toAdd; } catch (FileNotFoundException e) { //if no file is found, just append new file contents with toAdd file += toAdd; } // Write file try (var pw = new PrintWriter((new FileOutputStream("newMsgCount.txt")))) { pw.printf(file); } } public static Message[] messagesToModify(String myID, String theirID) throws IOException { ArrayList messages = getMsg(String.format("%s_%s.txt", myID, theirID)); ArrayList toReturn = new ArrayList<>(); for (Message message : messages) { if (message.getSenderID().equals(myID)) { toReturn.add(message); } } Message[] messagesToReturn = new Message[toReturn.size()]; toReturn.toArray(messagesToReturn); return messagesToReturn; } public static void editMsg(String editedMsg, Message messageToEdit, String myID, String theirID) throws IOException { int count = 0; ArrayList messages = getMsg(String.format("%s_%s.txt", myID, theirID)); for (Message message : messages) { if (message.isEquals(messageToEdit)) { message.setMessageContents(editedMsg + " (edited)"); } } try (PrintWriter pw = new PrintWriter(new FileOutputStream(String.format("%s_%s.txt", myID, theirID)))) { for (Message message : messages) { pw.printf("%s,%s,%s,%s\n%s\n", message.getSenderName(), message.getSenderID(), message.getDateTime(), message.isDisappearing() ? "D" : "N", message.getMessageContents()); } } ArrayList messagesTheir1 = getMsg(String.format("%s_%s_new.txt", theirID, myID)); for (Message message : messagesTheir1) { if (message.isEquals(messageToEdit)) { message.setMessageContents(editedMsg + " (edited)"); count++; } } if (count == 1) { try (PrintWriter pw = new PrintWriter( new FileOutputStream(String.format("%s_%s_new.txt", theirID, myID)))) { for (Message message : messagesTheir1) { pw.printf("%s,%s,%s,%s\n%s\n", message.getSenderName(), message.getSenderID(), message.getDateTime(), message.isDisappearing() ? "D" : "N", message.getMessageContents()); } } return; } ArrayList messagesTheir2 = getMsg(String.format("%s_%s.txt", theirID, myID)); for (Message message : messagesTheir2) { if (message.isEquals(messageToEdit)) { message.setMessageContents(editedMsg + " (edited)"); } } try (PrintWriter pw = new PrintWriter( new FileOutputStream(String.format("%s_%s.txt", theirID, myID)))) { for (Message message : messagesTheir2) { pw.printf("%s,%s,%s,%s\n%s\n", message.getSenderName(), message.getSenderID(), message.getDateTime(), message.isDisappearing() ? "D" : "N", message.getMessageContents()); } } } //delete message public static void deleteMsg(Message messageToEdit, String myID, String theirID) throws IOException { ArrayList messages = getMsg(String.format("%s_%s.txt", myID, theirID)); for (Message message : messages) { if (message.isEquals(messageToEdit)) { messages.remove(message); break; } } try (PrintWriter pw = new PrintWriter(new FileOutputStream(String.format("%s_%s.txt", myID, theirID)))) { for (Message message : messages) { pw.printf("%s,%s,%s,%s\n%s\n", message.getSenderName(), message.getSenderID(), message.getDateTime(), message.isDisappearing() ? "D" : "N", message.getMessageContents()); } } } //blocks a user by adding their details to a file called blocked.txt public static void blockUser(String myID, String theirID) throws FileNotFoundException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } synchronized (MessagingServer.BLOCK_GATEKEEPER) { try (PrintWriter pw = new PrintWriter(new FileOutputStream("blocked.txt", true))) { //format is blocker,blockee -> ie, I block them pw.printf("%s,%s\n", myID, theirID); } } } //unblocks a user by removing their details from blocked.txt public static void unblockUser(String myID, String theirID) throws IOException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } //basically deleting blocking line from file String file = ""; //new file contents String toDelete = String.format("%s,%s", myID, theirID); //line in file to delete synchronized (MessagingServer.BLOCK_GATEKEEPER) { try (BufferedReader br = new BufferedReader(new FileReader("blocked.txt"))) { String line = br.readLine(); while (line != null) { //format is blocker,blockee -> ie, I block them, so its always gonna be line.split("_")[1] if (line.equals(toDelete)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } try (PrintWriter pw = new PrintWriter(new FileOutputStream("blocked.txt"))) { pw.print(file); } } } //checks whether I am blocked public static boolean isBlocked(String myID, String theirID) throws IOException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } //get the string to check whether I blocked them String isBlocked = String.format("%s,%s", theirID, myID); synchronized (MessagingServer.BLOCK_GATEKEEPER) { try (BufferedReader br = new BufferedReader(new FileReader("blocked.txt"))) { String line = br.readLine(); while (line != null) { if (line.equals(isBlocked)) { return true; } line = br.readLine(); } } catch (FileNotFoundException e) { //if there's no blocked file, no users are blocked return false; } } //if couldn't find isBlocked string in file, then return false return false; } //checks whether they are blocked public static boolean areTheyBlocked(String myID, String theirID) throws IOException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } //get the string to check whether I blocked them synchronized (MessagingServer.BLOCK_GATEKEEPER) { String isBlocked = String.format("%s,%s", myID, theirID); try (BufferedReader br = new BufferedReader(new FileReader("blocked.txt"))) { String line = br.readLine(); while (line != null) { if (line.equals(isBlocked)) { return true; } line = br.readLine(); } } catch (FileNotFoundException e) { //if there's no blocked file, no users are blocked return false; } } //if couldn't find isBlocked string in file, then return false return false; } //blocks a user by adding their details to a file called blocked.txt public static void becomeInvisible(String myID, String theirID) throws FileNotFoundException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } try (PrintWriter pw = new PrintWriter(new FileOutputStream("invisible.txt", true))) { //format is enabler,enablee -> ie, I block them pw.printf("%s,%s\n", myID, theirID); } } //become visible to a user by removing their details from invisible.txt public static void becomeVisible(String myID, String theirID) throws IOException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } //basically deleting blocking line from file String file = ""; //new file contents String toDelete = String.format("%s,%s", myID, theirID); try (BufferedReader br = new BufferedReader(new FileReader("invisible.txt"))) { String line = br.readLine(); while (line != null) { //format is enabler,enablee -> ie, I block them, so its always gonna be line.split("_")[1] if (line.equals(toDelete)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } try (PrintWriter pw = new PrintWriter(new FileOutputStream("invisible.txt"))) { pw.print(file); } } //checks whether I am invisible to another user public static boolean isInvisible(String myID, String theirID) throws IOException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("CUSTOMER") == 0) { theirID = theirID.split("_")[0]; } else { myID = myID.split("_")[0]; } //get the string to check whether they are invisible to me String amInvisible = String.format("%s,%s", myID, theirID); try (BufferedReader br = new BufferedReader(new FileReader("invisible.txt"))) { String line = br.readLine(); while (line != null) { if (line.equals(amInvisible)) { return true; } line = br.readLine(); } } catch (FileNotFoundException e) { //if there's no invisible file, no users are invisible return false; } //if couldn't find amInvisible string in file, then return false return false; } //checks whether another user is invisible to me public static boolean areTheyInvisible(String myID, String theirID) throws IOException { //first get sellerID from storeID, need to check who is customer and who is store/seller if (myID.indexOf("SELLER") == 0) { myID = myID.split("_")[0]; } //get the string to check whether they are invisible to me String areTheyInvisible = String.format("%s,%s", theirID, myID); try (BufferedReader br = new BufferedReader(new FileReader("invisible.txt"))) { String line = br.readLine(); while (line != null) { if (line.equals(areTheyInvisible)) { return true; } line = br.readLine(); } } catch (FileNotFoundException e) { //if there's no invisible file, no users are invisible return false; } //if couldn't find amInvisible string in file, then return false return false; } //deletes user account and remove all text message files, store details, and login details associated with //the account public static boolean deleteUserAccount(String userID) throws IOException { String file = ""; //new contents to be written to a file synchronized (MessagingServer.USER_DETAILS_GATEKEEPER) { //removes content in loginDetails try (BufferedReader br = new BufferedReader(new FileReader("loginDetails.txt"))) { String line; line = br.readLine(); while (line != null) { if (line.split(",")[4].equals(userID)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything if file is not found } try (PrintWriter pw = new PrintWriter(new FileOutputStream("loginDetails.txt"))) { pw.print(file); } } //reset file contents file = ""; synchronized (MessagingServer.STORES_GATEKEEPER) { //removes store contents try (BufferedReader br = new BufferedReader(new FileReader("stores.txt"))) { String line; line = br.readLine(); while (line != null) { if (line.split(",")[line.split(",").length - 3].equals(userID)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything if file is not found } try (PrintWriter pw = new PrintWriter(new FileOutputStream("stores.txt"))) { pw.print(file); } } synchronized (MessagingServer.NEW_MSG_GATEKEEPER) { file = ""; //reset file contents //removes new message contents try (BufferedReader br = new BufferedReader(new FileReader("newMsgCount.txt"))) { String line; line = br.readLine(); while (line != null) { if (line.contains(userID)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything if file is not found } try (PrintWriter pw = new PrintWriter(new FileOutputStream("newMsgCount.txt"))) { pw.print(file); } } //removes blocked file contents file = ""; //reset file contents synchronized (MessagingServer.BLOCK_GATEKEEPER) { try (BufferedReader br = new BufferedReader(new FileReader("blocked.txt"))) { String line; line = br.readLine(); while (line != null) { if (line.contains(userID)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything if file is not found } try (PrintWriter pw = new PrintWriter(new FileOutputStream("blocked.txt"))) { pw.print(file); } } //removes invisible file contents file = ""; //reset file contents synchronized (MessagingServer.INVISIBLE_GATEKEEPER) { try (BufferedReader br = new BufferedReader(new FileReader("invisible.txt"))) { String line; line = br.readLine(); while (line != null) { if (line.contains(userID)) { line = br.readLine(); continue; } file += line + "\n"; line = br.readLine(); } } catch (FileNotFoundException e) { //don't need to do anything if file is not found } try (PrintWriter pw = new PrintWriter(new FileOutputStream("invisible.txt"))) { pw.print(file); } } //deletes all message files involving the account to be deleted String workingDirectory = System.getProperty("user.dir"); //get the current working directory File files = new File(workingDirectory); String[] filesInDirectory = files.list(); if (filesInDirectory != null) { for (String fileName : filesInDirectory) { if (fileName.contains(userID)) { File toDelete = new File(fileName); //if can't delete file, return false if (!toDelete.delete()) { System.out.printf("Could not delete message file: %s\n", fileName); return false; } } } } //remove ID number from array int idToRemove = Integer.parseInt(userID.substring(userID.length() - 1)); if (userID.contains("SELLER")) { MessagingServer.getUsedSellerIDs().remove((Integer) idToRemove); } else if (userID.contains("CUSTOMER")) { MessagingServer.getUsedCustomerIDs().remove((Integer) idToRemove); //remove the customer gatekeeper int index = 0; for (int i = 0; i < MessagingServer.CUSTOMER_GATEKEEPERS.size(); i++) { if (MessagingServer.CUSTOMER_GATEKEEPERS.get(i).getID() == idToRemove) { index = i; break; } } MessagingServer.CUSTOMER_GATEKEEPERS.remove(index); } else { return false; } return true; } }