/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
10 public class FileManager {
11
12
13 private File [] directories = {new File("inventory"), new File("transaction")};
14
15
16
17
18
19
20
21
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
68
69
70
71
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
88
89
90
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
111
112
113
114
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
140
141
142
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
151
152
153
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
163
164
165
166 public void moveToTransactions(Shoes shoe, int index) throws Exception {
167 addToShoeArray("transaction", shoe);
168 removeShoe("inventory", index);
169 }
170 }