/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/CS IA Shoe Reselling Aryan April 6th /src/FileManager.java
  1 import java.io.BufferedReader;
  2 import javax.swing.*;
  3 import java.io.File;
  4 import java.io.FileWriter;
  5 import java.io.IOException;
  6 import java.io.PrintWriter;
  7 import java.util.Scanner;
  8 
  9 //This class focuses on how the program would save and retrieve data from the user's device
 10 public class FileManager {
 11 
 12 
 13     private File [] directories = {new File("inventory"), new File("transaction")};//Creates an array of new files on the users device under the specific names.
 14 
 15 
 16     //Adds the shoes to either the inventory file, or retreives it from the inventory file and moves it to the transaction table, 
 17     //according to the decision made by the user in the program.
 18     
 19     //Pre-conditions: The necessary directory chosen (whether the inventory table or the transaction table). Also, the list of shoes, 
 20     //to input the specific data attributes.
 21     //Post-conditions: Save a text file to the users device in the desired file/table.
 22     public void addToShoeArray(String targetDirectory, Shoes shoe) throws IOException {
 23         if(targetDirectory.equals("inventory")) {
 24             int fileIndex = directories[0].listFiles().length-1;
 25             File file = new File("inventory/"+fileIndex+".txt");
 26             file.createNewFile();
 27 
 28             addTextToFile(shoe.getName(), file);
 29             addTextToFile(shoe.getSize(), file);
 30             addTextToFile(shoe.getBuyingPrice(), file);
 31             addTextToFile(shoe.getSellingPrice(), file);
 32             addTextToFile(shoe.getShoeDateBoughtDay(), file);
 33             addTextToFile(shoe.getShoeDateBoughtMonth(), file);
 34             addTextToFile(shoe.getShoeDateBoughtYear(), file);
 35             addTextToFile(shoe.getBoughtDate(), file);
 36             addTextToFile(shoe.getShoeDateSoldDay(), file);
 37             addTextToFile(shoe.getShoeDateSoldMonth(), file);
 38             addTextToFile(shoe.getShoeDateSoldYear(), file);
 39             addTextToFile(shoe.getCollaboration(), file);
 40             addTextToFile(shoe.getBrand(), file);
 41             addTextToFile(shoe.getProfitability(), file);
 42             addTextToFile(shoe.getGender(), file);
 43 
 44         } else if(targetDirectory.equals("transaction")) {
 45             int fileIndex = directories[1].listFiles().length-1;
 46             File file = new File("transaction/"+fileIndex+".txt");
 47             file.createNewFile();
 48 
 49             addTextToFile(shoe.getName(), file);
 50             addTextToFile(shoe.getSize(), file);
 51             addTextToFile(shoe.getBuyingPrice(), file);
 52             addTextToFile(shoe.getSellingPrice(), file);
 53             addTextToFile(shoe.getShoeDateBoughtDay(), file);
 54             addTextToFile(shoe.getShoeDateBoughtMonth(), file);
 55             addTextToFile(shoe.getShoeDateBoughtYear(), file);
 56             addTextToFile(shoe.getBoughtDate(), file);
 57             addTextToFile(shoe.getShoeDateSoldDay(), file);
 58             addTextToFile(shoe.getShoeDateSoldMonth(), file);
 59             addTextToFile(shoe.getShoeDateSoldYear(), file);
 60             addTextToFile(shoe.getCollaboration(), file);
 61             addTextToFile(shoe.getBrand(), file);
 62             addTextToFile(shoe.getProfitability(), file);
 63             addTextToFile(shoe.getGender(), file);
 64         }
 65     }
 66 
 67     //Deletes a shoe from the necessary table.
 68     
 69     //Pre-conditions: Takes in the specific table that the user wants the data to be deleted from, and the specific place in the file of that
 70     //text file.
 71     //Post-conditions: Takes away the necessary shoe at the index for the user, from the table.
 72     public void removeShoe(String targetDirectory, int index) throws Exception {
 73         getFile(targetDirectory, index).delete();
 74         if(targetDirectory.equals("inventory")) {
 75             File [] directoryFiles = directories[0].listFiles();
 76             for (int i = 0; i < arrayCount(targetDirectory); i++) {
 77                 directoryFiles[i+1].renameTo(new File(directories[0].getAbsolutePath()+i+".txt"));
 78             }
 79         } else if(targetDirectory.equals("transaction")) {
 80             File [] directoryFiles = directories[1].listFiles();
 81             for (int i = 0; i < arrayCount(targetDirectory); i++) {
 82                 directoryFiles[i+1].renameTo(new File(directories[1].getAbsolutePath()+i+".txt"));
 83             }
 84         }
 85     }
 86 
 87     //Sort of an error if the file that is trying to be "gotten" does not "exist", otherwise gets the specific file desired.
 88     
 89     //Pre-conditions: The directory/file to be gone through, and the specific text file in the directory that is trying to be found.
 90     //Post-conditions: Gets the file in the directory, otherwise presents error message
 91     private File getFile(String targetDirectory, int index) throws Exception {
 92         Scanner scanner;
 93         if(targetDirectory.equals("inventory")) {
 94             if(index > directories[0].listFiles().length-1) {
 95                 throw new Exception(index+" is too big!");
 96             } else {
 97                 return directories[0].listFiles()[index+1];
 98             }
 99 
100         } else if(targetDirectory.equals("transaction")) {
101             if(index > directories[1].listFiles().length-1) {
102                 throw new Exception(index+" is too big!");
103             } else {
104                 return directories[1].listFiles()[index+1];
105             }
106         }
107         return null;
108     }
109 
110     //Gets the specific shoe from the file, and all its attributes.
111     
112     //Pre-conditions: The directory/file to be gone through, and the specific text file with the shoe and its 
113     //attributes in the directory that is trying to be found.
114     //Post-conditions: Gets all the specific attributes of the shoe from the text file, so essentially everything about the shoe
115     public Shoes getShoe(String targetDirectory, int index) throws Exception {
116         Scanner scanner = new Scanner(System.in);
117         if(targetDirectory.equals("inventory")) {
118             if(index > arrayCount(targetDirectory)) {
119                 throw new Exception(index+" is too big!");
120             } else {
121                 File file = directories[0].listFiles()[index+1];
122                 scanner = new Scanner(file);
123                 
124                 return new Shoes(scanner.next(), Double.parseDouble(scanner.next()),Double.parseDouble(scanner.next()), Double.parseDouble(scanner.next()), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), scanner.next(), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), scanner.next(), scanner.next(), Double.parseDouble(scanner.next()), scanner.next());
125             }
126 
127         } else if(targetDirectory.equals("transaction")) {
128             if(index > arrayCount(targetDirectory)) {
129                 throw new Exception(index+" is too big!");
130             } else {
131                 File file = directories[1].listFiles()[index+1];
132                 scanner = new Scanner(file);
133                 return new Shoes(scanner.next(), Double.parseDouble(scanner.next()),Double.parseDouble(scanner.next()), Double.parseDouble(scanner.next()), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), scanner.next(), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()), scanner.next(), scanner.next(), Double.parseDouble(scanner.next()), scanner.next());
134             }
135         }
136         return null;
137     }
138 
139     //Add the text of attributes to the text file.
140     
141     //Pre-conditions: The text needed to be inputted, and the file.
142     //Post-conditions: File Writer prints in the text to the text file.
143     private void addTextToFile(Object text, File file) throws IOException {
144         FileWriter fw = new FileWriter(file.getAbsolutePath(), true);
145         PrintWriter pr = new PrintWriter(fw);
146         pr.println(text.toString());
147         pr.close();
148     }
149     
150     //The number of files in each directory
151     
152     //Pre-conditions: The directory that has the arrays, who's number of files are trying to be found
153     //Post-conditions: The number of files in either the inventory or transaction directory, as desired.
154     public int arrayCount(String targetDirectory) {
155         if(targetDirectory.equals("inventory")) {
156             return directories[0].listFiles().length-1;
157         } else {
158             return directories[1].listFiles().length-1;
159         }
160     }
161 
162     //Move the shoe from the inventory table to the transaction table
163     
164     //Pre-conditions: Get the index of where the shoe is stored in the inventory directory
165     //Post-conditions: Adds the shoe to the transaction directory, and removes it from the inventory.
166     public void moveToTransactions(Shoes shoe, int index) throws Exception {
167         addToShoeArray("transaction", shoe);
168         removeShoe("inventory", index);
169     }
170 }