-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyPattern.java
More file actions
282 lines (237 loc) · 9.2 KB
/
Copy pathStrategyPattern.java
File metadata and controls
282 lines (237 loc) · 9.2 KB
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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
/*
----------------------------------------------------------------
Mason Dinardi & Conner Cross
Homework 2 - Implementation of Strategy Pattern
September 18, 2019
As we were asked to submit just one java file, all of our
classes appear in this single file. In a better implementation,
each class would have it's own file.
----------------------------------------------------------------
*/
//----------------------------------------------------------------
//main class and driver method
public class StrategyPattern {
public static void main(String args[]) {
//deletes previous output file
File file = new File("./output.txt");
if(file.delete())
{
System.out.println("Output file deleted successfully");
}
else
{
System.out.println("Failed to delete the output file");
}
//simple data to be used in this example
String[] data = {"dog", "cat", "fish"};
//main loop, outer loop allows user to create new databases
//inner loop allows user to change storage strategy on database
String input;
do {
boolean flag = true;
//get user input for database type
Scanner reader = new Scanner(System.in);
System.out.println("\nWhat type of database would you like? (Relational, NoSQL, Graph, or enter 'done' to end): ");
input = reader.next().toLowerCase();
//instantiate specified database type, or end program execution
Database database = new Database();
switch (input) {
case "relational":
database = new RelationalDB();
break;
case "nosql":
database = new NoSQLDB();
break;
case "graph":
database = new GraphDB();
break;
case "done" :
flag = false;
writeToOutput("\n--------------------------------" +
"\nFinished Execution");
break;
default:
System.out.println("Input unrecognized.");
flag = false;
}
//write database header to output file
if (flag) {
writeToOutput("\n--------------------------------" +
"\nDatabase type: " + input.toUpperCase());
//call default storage strategy for database type
database.store(data);
//allow user to change storage strategy
String choice;
do {
System.out.println("Would you like to select a different storage strategy? (Y/N)");
choice = reader.next().toLowerCase();
if (choice.equals("y")) {
boolean flag2 = true;
System.out.println("Enter the new storage strategy: (Table, Document, Node)");
String storageStrategy = reader.next().toLowerCase();
switch (storageStrategy) {
case "table":
database.setStore(new TableStore());
break;
case "document":
database.setStore(new DocumentStore());
break;
case "node":
database.setStore(new NodeStore());
break;
default:
System.out.println("Strategy unrecognized.");
flag2 = false;
}
//call store method for newly set storage strategy
if(flag2)
database.store(data);
}
} while (!choice.equals("n"));
}
} while(!input.equals("done"));
}
//method to append line to output file
public static void writeToOutput(String textToAppend) {
try {
BufferedWriter writer = new BufferedWriter(
new FileWriter("./output.txt", true) //Set true for append mode
);
writer.newLine(); //Add new line
writer.write(textToAppend);
writer.close();
} catch (IOException e) {
System.out.println("Error writing to the output file");
}
}
}
//----------------------------------------------------------------
//----------------------------------------------------------------
// Parent Database class and child classes for each database type
//Parent Database class
class Database {
IDatabaseStore ds;
public void store(String[] data){}
public void setStore(IDatabaseStore storeStrategy){}
}
//Relational database class
class RelationalDB extends Database {
//constructor
public RelationalDB() {
ds = new TableStore();
}
// method to call store method of set store strategy
// store() would be a unique implementation in real use
public void store(String[] data) {
ds.store(data);
System.out.println("\nData stored using the " + ds.toString() + "strategy.");
}
public void setStore(IDatabaseStore storeStrategy) {
ds = storeStrategy;
}
}
//NoSQL database class
class NoSQLDB extends Database {
//constructor
public NoSQLDB() {
ds = new DocumentStore();
}
// method to call store method of set store strategy
// store() would be a unique implementation in real use
public void store(String[] data) {
ds.store(data);
System.out.println("\nData stored using the " + ds.toString() + "strategy.");
}
//method to set store strategy
public void setStore(IDatabaseStore storeStrategy) {
ds = storeStrategy;
}
}
//Graph database class
class GraphDB extends Database {
//constructor
public GraphDB() {
ds = new NodeStore();
}
// method to call store method of set store strategy
// store() would be a unique implementation in real use
public void store(String[] data) {
ds.store(data);
System.out.println("\nData stored using the " + ds.toString() + "strategy.");
}
//method to set store strategy
public void setStore(IDatabaseStore storeStrategy) {
ds = storeStrategy;
}
}
//----------------------------------------------------------------
//----------------------------------------------------------------
//Strategy Interface and strategy classes for each storage strategy
//Interface IDatabaseStore
interface IDatabaseStore {
void store(String[] data);
}
//Document store strategy
//writes array of data to output.txt
class TableStore implements IDatabaseStore {
//implementation of store method for Table store
public void store(String[] data) {
try {
BufferedWriter writer = new BufferedWriter(
new FileWriter("./output.txt", true)
);
writer.newLine(); //Add new line
writer.write("\nData stored using the Table Store strategy\n" + Arrays.toString(data));
writer.close();
} catch (IOException e) {
System.out.println("Error writing to the output file");
}
}
//method to return string representation of class
public String toString(){return "Table Store ";}
}
//Document store strategy
//writes array of data to output.txt
class DocumentStore implements IDatabaseStore {
//implementation of store method for Document store
public void store(String[] data) {
try {
BufferedWriter writer = new BufferedWriter(
new FileWriter("./output.txt", true) //Set true for append mode
);
writer.newLine(); //Add new line
writer.write("\nData stored using the Document Store strategy\n" + Arrays.toString(data));
writer.close();
} catch (IOException e) {
System.out.println("Error writing to the output file");
}
}
//method to return string representation of class
public String toString(){return "Document Store ";}
}
//Node store strategy
//writes array of data to output.txt
class NodeStore implements IDatabaseStore {
//implementation of store method for Node store
public void store(String[] data) {
try {
BufferedWriter writer = new BufferedWriter(
new FileWriter("./output.txt", true) //Set true for append mode
);
writer.newLine(); //Add new line
writer.write("\nData stored using the Node Store strategy\n" + Arrays.toString(data));
writer.close();
} catch (IOException e) {
System.out.println("Error writing to the output file");
}
}
//method to return string representation of class
public String toString(){return "Node Store ";}
}
//----------------------------------------------------------------