-
Notifications
You must be signed in to change notification settings - Fork 1
/
processingMethods.java
298 lines (279 loc) · 8.87 KB
/
processingMethods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
/**
* Processing Methods hold functions for processing inputs and outputs of the software
*
* @author Kimberly Lalmansingh
*
*/
public class ProcessingMethods {
//global data members
private BookStorage currentUserStorage = null;
private Search titles = null;
BufferedWriter receipt = null;
BufferedWriter outFile = null;
//constructor
public ProcessingMethods(BookStorage currentUserStorage){
//get the currently sign in users storage
this.currentUserStorage = currentUserStorage;
}
//process input arguments
public void processArgs(String[] args){
//input and output file names
String inputFile = "";
String outputFile = "";
boolean input = false;
boolean output=false;
for(int i=0; i<args.length; i++){
//input file
if(args[i].equals("-i")){
if(args[i+1].startsWith("-")){
//system will exit if not input file is given
System.out.println("Please enter a textfile. Exiting software now.");
System.exit(0);
}
else{
inputFile = args[i+1];
input=true;
i++;
}
}
//read a url file
else if(args[i].equals("-u")){
readUrls(args[i+1]);
i++;
}
//create output
else if(args[i].equals("-o")){
outputFile = args[i+1];
output=true;
i++;
}
}
if(output){
createOutput(outputFile); //will create an output file if output flag was present
}
if(input){
readInputFile(inputFile); //will read in the inputs if input flag was present
}
}
//process the input file
public void readInputFile(String inputFile){
ArrayList<String> inputs = new ArrayList<String>(); //stores inputs
//will hold the titles that will allow the searching, it cannot have spaces
ArrayList<String> titlesToSearch = new ArrayList<String>();
File inFile = new File(inputFile);
String title;
//read in from input file and store in array list
try {
Scanner inputTitle = new Scanner(inFile);
//read all titles from input file
while(inputTitle.hasNext()){
title = inputTitle.nextLine();
inputs.add(title);
try {
//write to output file
outFile.write("Added to Searching list: " + title + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
inputTitle.close();
//will modify the titles and store them as required to allow searching
titlesToSearch = removeWhiteSpace(inputs);
//process the titles
processDataInputs(titlesToSearch);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private ArrayList<String> removeWhiteSpace(ArrayList<String> titles) {
//remove white spaces and adds "+" between spaces to allow searching
ArrayList<String> newTitles = new ArrayList<String>();
String newTitle = null;
for(int i=0; i<titles.size(); i++){
newTitle = titles.get(i).replaceAll("\\s+","+");
//add these titles to an array list with the new modifications
newTitles.add(newTitle);
}
return newTitles;
}
public void processDataInputs(ArrayList<String> inputs) {
//process searching the titles individually
titles = new Search(currentUserStorage);
for(int i=0; i<inputs.size(); i++){
titles.titleSearch(inputs.get(i), outFile);
}
titles.parseIndividualItems();
}
//for advanced search, will check if book is found or return if
//book is not found
public int searchNewBook(String title, String isbn, String author, String genre) {
int checkSearch = 0;
try {
//write to outfile
outFile.write("Advanced Search on: Title: " + title + " ISBN: " + isbn + " Author: " + author + " Genre: " + genre + "\n");
} catch (IOException e) {
e.printStackTrace();
}
//remove white spaces and adds "+" between spaces to allow searching
title = title.replaceAll("\\s+","+");
//check to see if paramters given by user exists as a book
checkSearch = titles.advancedBookSearch(title, isbn);
if(checkSearch==1){
try {
//if this book does not exist, inform user
outFile.write("No book found for previous Advanced Search" + "\n");
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
return 0;
}
public void loadLogFile(String inputFile){
//load log file for admin uses
File inFile = new File(inputFile);
String isbn, title, author, genre, price, field, value, command = null;
try {
//write to outfile
outFile.write("Rebuilding Data from LogFile: "+ inputFile + "\n");
} catch (IOException e) {
e.printStackTrace();
}
try {
//read in each command in log file
Scanner data = new Scanner(inFile);
//break up info using "/"
data.useDelimiter("/|\\n");
while(data.hasNext()){
command = data.next();
//if something was inserted
if(command.equalsIgnoreCase("insert")){
isbn = data.next();
title = data.next();
author = data.next();
genre = data.next();
price = data.next();
currentUserStorage.storeData(isbn, title, author, genre, price);
}
//if something was deleted
else if(command.equalsIgnoreCase("delete")){
isbn = data.next();
currentUserStorage.deleteData(isbn);
}
//if something was modified
else if(command.equalsIgnoreCase("modify")){
isbn = data.next();
field = data.next();
value = data.next();
currentUserStorage.modifyData(isbn, field, value);
}
}
data.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void generateReceipt() {
try {
//create a receipt with the currently stored book in the
//users storage
FileWriter yourReceipt = new FileWriter("receipt.txt");
receipt = new BufferedWriter(yourReceipt);
receipt.write("BOOKSHELF \n");
receipt.write("Sold by: Kimberly \n");
receipt.write("In partnership with: Barnes & Noble \n\n");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
receipt.write("Date and Time: " + timestamp + " \n\n");
double total = 0.0;
double tax = 1.0825;
String price = "";
String str;
//go through every item in users storage
Hashtable<String, Book> boughtBooks;
boughtBooks = currentUserStorage.getBooks();
Set<String> keys = boughtBooks.keySet();
Iterator<String> itr = keys.iterator();
receipt.write("Number of items bought: " + currentUserStorage.getItems() + "\n\n");
while (itr.hasNext()) {
//write all book information
str = itr.next();
receipt.write("BOOK: " + boughtBooks.get(str).getTitle() +"\n");
receipt.write("AUTHOR: " + boughtBooks.get(str).getAuthor() +"\n");
receipt.write("ISBN: " + boughtBooks.get(str).getIsbn() +"\n");
receipt.write("GENRE: " + boughtBooks.get(str).getGenre() +"\n");
if(boughtBooks.get(str).getPrice().equals("")){
receipt.write("PRICE: " + "$0.00" +"\n");
}
else{
//calculate price of all the books in total
receipt.write("PRICE: " + boughtBooks.get(str).getPrice() +"\n");
price = boughtBooks.get(str).getPrice();
price = price.substring(1, price.length());
total = total + Double.parseDouble(price);
}
receipt.write("------------\n");
}
receipt.write("----------------------------------------------------\n");
receipt.write("SUBTOTAL: $" + total + "\n");
receipt.write("TAX " + tax + "\n");
DecimalFormat totalInDecimal = new DecimalFormat(".##");
double totalWithTax = total*tax;
String result = "";
result = totalInDecimal.format(totalWithTax);
receipt.write("TOTAL $" + result + "\n");
receipt.write("----------------------------------------------------\n");
receipt.write("THANK YOU! \n\n");
} catch (IOException e) {
e.printStackTrace();
}
}
//getters and setters for receipt file
public BufferedWriter getReceipt(){
return receipt;
}
public BufferedWriter getOutFile(){
return outFile;
}
//read in url information
private void readUrls(String inputFile){
File inFile = new File(inputFile);
ArrayList<String> urls = new ArrayList<String>();
try {
Scanner url = new Scanner(inFile);
while(url.hasNext()){
urls.add(url.next());
}
url.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Search urlSearch = new Search(currentUserStorage);
urlSearch.getUrlInfo(urls);
}
//create an output file if flag was present
public void createOutput(String outputFile){
try {
outFile = new BufferedWriter(new FileWriter(outputFile));
} catch (IOException e) {
e.printStackTrace();
}
}
//call function to read url of an image to be displayed on GUI
public BufferedImage showFeaturedItem() {
BufferedImage img = titles.featuredItem();
return img;
}
}