From ae8041ad0b45bace07ae924b4749ea6e060dd069 Mon Sep 17 00:00:00 2001
From: Michael Mesquita <mannolaur@gmail.com>
Date: Thu, 9 Dec 2021 15:15:44 -0500
Subject: [PATCH] Bugged, text files cannot be found by ShopSystem."

---
 src/main/java/com/SOUPcorp/app/App.java       |  2 +-
 .../java/com/SOUPcorp/app/ShopSystem.java     | 31 +++++++++++++------
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/SOUPcorp/app/App.java b/src/main/java/com/SOUPcorp/app/App.java
index 6cdb99d..7b3a21b 100644
--- a/src/main/java/com/SOUPcorp/app/App.java
+++ b/src/main/java/com/SOUPcorp/app/App.java
@@ -1,7 +1,7 @@
 package com.SOUPcorp.app;
 
 /**
- * Hello world!
+ * Main App Class for running the program.
  *
  */
 public class App {
diff --git a/src/main/java/com/SOUPcorp/app/ShopSystem.java b/src/main/java/com/SOUPcorp/app/ShopSystem.java
index 02f70f0..6fa574f 100644
--- a/src/main/java/com/SOUPcorp/app/ShopSystem.java
+++ b/src/main/java/com/SOUPcorp/app/ShopSystem.java
@@ -3,10 +3,16 @@
 import java.io.*;
 import java.util.ArrayList;
 import java.util.Map;
+import java.nio.file.Paths;
+import java.nio.file.Path;
 
 public class ShopSystem {
     public static ShopSystem instance = new ShopSystem();
     public Inventory inventory = new Inventory();
+    private static final String DATA_DIR = FirstTimeSetup.getDataDir();
+    private static final String UI_PATH = Paths.get(DATA_DIR, "UserInfo.txt").toString();
+    private static final String OH_PATH = Paths.get(DATA_DIR, "OrderHistory.txt").toString();
+    private static final String PROD_PATH = Paths.get(DATA_DIR, "Products.txt").toString();
 
     /**
      * ShopSystem constructor
@@ -16,6 +22,7 @@ private ShopSystem() {
 
     /**
      * instance getter
+     * 
      * @return ShopSystem instance (Singleton pattern)
      */
     public static ShopSystem getInstance() {
@@ -24,11 +31,12 @@ public static ShopSystem getInstance() {
 
     /**
      * adds user to UserInfo file
+     * 
      * @param userInfo String (username, password, userType)
      * @throws FileNotFoundException
      */
     public void addUser(String userInfo) throws FileNotFoundException {
-        try (FileWriter out = new FileWriter("data/UserInfo.txt", true)) {
+        try (FileWriter out = new FileWriter(UI_PATH, true)) {
             out.write(userInfo + "\n");
         } catch (IOException e) {
             e.printStackTrace();
@@ -37,12 +45,13 @@ public void addUser(String userInfo) throws FileNotFoundException {
 
     /**
      * fetches products from Products file
+     * 
      * @return ArrayList products
      */
     public static ArrayList<String> fetchProducts() {
         ArrayList<String> products = new ArrayList<>();
 
-        try (BufferedReader br = new BufferedReader(new FileReader("data/Products.txt"))) {
+        try (BufferedReader br = new BufferedReader(new FileReader(PROD_PATH))) {
             String line;
             while ((line = br.readLine()) != null) {
                 products.add(line);
@@ -55,18 +64,18 @@ public static ArrayList<String> fetchProducts() {
 
     /**
      * updates quantity of product in Products file
-     * @param name Name of product to be updated
+     * 
+     * @param name        Name of product to be updated
      * @param newQuantity New quantity of updated product
      */
     public void updateProducts(String name, int newQuantity) {
         ArrayList<String> products = fetchProducts();
-        try (FileWriter out = new FileWriter("data/Products.txt")) {
+        try (FileWriter out = new FileWriter(PROD_PATH)) {
             for (String product : products) {
                 String[] split = product.split("\\s+");
                 if (split[0].equals(name)) {
                     out.write(split[0] + " " + split[1] + " " + newQuantity + "\n");
-                }
-                else {
+                } else {
                     out.write(product + "\n");
                 }
             }
@@ -77,12 +86,13 @@ public void updateProducts(String name, int newQuantity) {
 
     /**
      * verifies that login user exists by checking UserInfo file
+     * 
      * @param username Name of user trying to login
      * @param password Password of user trying to login
      * @return String userType or Invalid if user doesn't exist
      */
     public String verifyLogin(String username, String password) {
-        try (BufferedReader br = new BufferedReader(new FileReader("data/UserInfo.txt"))) {
+        try (BufferedReader br = new BufferedReader(new FileReader(UI_PATH))) {
             String line;
             while ((line = br.readLine()) != null) {
                 String[] split = line.split("\\s+");
@@ -98,6 +108,7 @@ public String verifyLogin(String username, String password) {
 
     /**
      * fetches current inventory of products
+     * 
      * @return Map inventory
      */
     public Map<Item, Integer> fetchInventory() {
@@ -106,10 +117,11 @@ public Map<Item, Integer> fetchInventory() {
 
     /**
      * writes new order details to OrderHistory file
+     * 
      * @param orderDetails Name and CC number of customer
      */
     public void updateOrderHistory(String orderDetails) {
-        try (FileWriter out = new FileWriter("data/OrderHistory.txt", true)) {
+        try (FileWriter out = new FileWriter(OH_PATH, true)) {
             out.write(orderDetails + "\n");
         } catch (IOException e) {
             e.printStackTrace();
@@ -118,12 +130,13 @@ public void updateOrderHistory(String orderDetails) {
 
     /**
      * reads order history from OrderHistory file
+     * 
      * @return ArrayList orderHistory
      */
     public ArrayList<String> fetchOrderHistory() {
         ArrayList<String> orderHistory = new ArrayList<>();
 
-        try (BufferedReader br = new BufferedReader(new FileReader("data/OrderHistory.txt"))) {
+        try (BufferedReader br = new BufferedReader(new FileReader(OH_PATH))) {
             String line;
             while ((line = br.readLine()) != null) {
                 orderHistory.add(line);
