-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transaction.java
412 lines (350 loc) · 17.8 KB
/
Transaction.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
////////// Abigail H. Shriver //////////
////////// CSci 4243 Senior Design Project //////////
////////// Self-Tracking E-currency //////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
*/
import java.io.IOException;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class Transaction {
public static boolean attemptTransaction(Currency currency, Request request){
//Check to see if the requester has the permission to be a spender of the currency
if(currency.reciever.equals(request.userID)){
System.out.println("Authorized User!!! Continuing to verfiy fund use....");
}else{
System.out.println("User is not authorized to spend funds.... \n XXXXXXXXXXXXXXXXXXXX Transaction DENIED XXXXXXXXXXXXXXXXXXXX");
return false;
}
//Check to see if there are funds avaliable
if(checkAmAvali(currency.amLeft, request.amount)){
System.out.println("Sufficient Funds avaliable!!! Continuing to verfiy fund use....");
//If there are funds avaliable check to see if the request meets the restrictions
int checkRestriction = checkRestriction(currency.restrict, request.restrict);
if(checkRestriction == 1){
executeTransaction(currency, request);
System.out.println("Valid transaction... !!!!!!!!!!!!!!!! Transaction APPROVED !!!!!!!!!!!!!!!! ");
System.out.println("\t$"+currency.amLeft + " funds are left for this currency with the following restrictions: ");
System.out.println("\t\t"+ currency.restrict);
currency.split1reciever(currency, request.amount, request.userID);
return true;
}else{// Funds are not being used as restricted
System.out.println("Invalid use of funds... XXXXXXXXXXXXXXXXXXXX Transaction DENIED XXXXXXXXXXXXXXXXXXXX");
System.out.println("\t Restriction path REQUIRED for these funds is:");
System.out.println("\t \t" + currency.restrict);
System.out.println("\t Restriction path REQUESTED for this transaction: ");
System.out.println("\t\t"+ request.restrict);
if(checkRestriction == -2){
System.out.println("\t\tRequest was NOT along the required path!");
}else{
System.out.println("\t\tRequest was NOT specific enough in its restriction. ");
}
return false;
}
}else{//Not enough funds are avaliable
System.out.println("Insufficient funds... XXXXXXXXXXXXXXXXXXXX Transaction DENIED XXXXXXXXXXXXXXXXXXXX");
System.out.println("\t Funds avaliable to spend: $" + currency.amLeft);
System.out.println("\t Funds requested: $"+ request.amount);
double amountNeeded = request.amount - currency.amLeft;
System.out.println("\t Need $" + amountNeeded + " to execute transaction request.");
return false;
}
}
public static int checkRestriction(ArrayList<String> allocatorRestriction, ArrayList<String> requestCategory){
//-1 means that the request is not specific enough
//-2 means that the request is down the wrong path
//1 means the request is valid
if(requestCategory.size() < allocatorRestriction.size()){
return -1;
}
for(int i =0; i< allocatorRestriction.size(); i++){
if( (allocatorRestriction.get(i).equals(requestCategory.get(i)) ) == false){
return -2;
}
}
return 1;
}
public static boolean checkAmAvali(double avaliFunds, double requestAm){
double temp = avaliFunds-requestAm;
if(temp >= 0){
return true;
}else{
return false;
}
}
public static void executeTransaction(Currency currency, Request request){
//update value left in currency
currency.amLeft = currency.amLeft - request.amount;
//Send notifications to designate notifiees
return ;
}
public static void main(String[] args) {
System.out.println();
RestrictiveTree current = new RestrictiveTree();
//Split Test Number 0
System.out.println("********************************************************************");
System.out.println("*********************** SPLIT TEST ***********************");
System.out.println("********************************************************************");
System.out.println();
Currency testCurrency0 = new Currency(
"sender1",
"testReciever",
new ArrayList<String> (),
100000.00,
10.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
testCurrency0.split2recievers(testCurrency0, 50, "reciever1", "reciever2");
//Test Number 1 -- Should be valid -- Does not use all of the funds
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("********************************************************************");
System.out.println("*********************** TRANSACTION ONE TEST ***********************");
System.out.println("********************************************************************");
System.out.println("/// Should be valid -- Should have $40.00 left after request///");
System.out.println();
Currency testCurrency = new Currency(
"sender1",
"testReciever",
new ArrayList<String> (),
100.00,
10.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
Request testRequest = new Request(
"testReciever",
testCurrency,
50.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines"))
);
attemptTransaction(testCurrency, testRequest);
//Test Number 2 -- Should be valid -- continuation of first test -- should use all of the funds
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("*********************** TRANSACTION TWO TEST ***********************");
System.out.println("********************************************************************");
System.out.println("///Should be valid -- Testing second request on same currency -- should have $0.0 funds after///");
System.out.println();
Request testRequest2 = new Request(
"testReciever",
testCurrency,
40.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines"))
);
attemptTransaction(testCurrency, testRequest2);
//Test Number 3 -- Should have insufficient funds -- zero funds avaliable -- continuation of first two
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************** TRANSACTION THREE TEST **********************");
System.out.println("********************************************************************");
System.out.println("///Should have insufficient funds -- funds were used by the previous two transactions///");
System.out.println();
attemptTransaction(testCurrency, testRequest2);
//Test Number 4 -- Should have insufficient funds -- funds avaliable but the request costs more -- new
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("*********************** TRANSACTION FOUR TEST **********************");
System.out.println("********************************************************************");
System.out.println("///Should have insufficient funds -- funds are avalibale, but the request costs more///");
System.out.println();
Currency testCurrency1 = new Currency(
"sender2",
"testReciever1",
new ArrayList<String> (),
100.00,
0.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
Request testRequest3 = new Request(
"testReciever1",
testCurrency1,
120.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines"))
);
attemptTransaction(testCurrency1, testRequest3);
//Test Number 5 -- Should have invalid restriction -- allocator further restricted than what the requestor specified
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************** TRANSACTION FIVE TEST ***********************");
System.out.println("********************************************************************");
System.out.println("///Should have invalid restriction -- the allocator made more specific restrictions \n ///than what the requestor specified ///");
System.out.println();
Currency testCurrency2 = new Currency(
"sender3",
"testReciever2",
new ArrayList<String> (),
500.00,
0.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
Request testRequest4 = new Request(
"testReciever2",
testCurrency2,
120.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment"))
);
attemptTransaction(testCurrency2, testRequest4);
//Test Number 6 -- Should have invalid restriction -- paths do not match
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("*********************** TRANSACTION SIX TEST ***********************");
System.out.println("********************************************************************");
System.out.println("///Should have invalid restriction -- path of allocation and request do NOT match ///");
System.out.println();
Currency testCurrency3 = new Currency(
"sender4",
"testReciever3",
new ArrayList<String> (),
500.00,
0.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
Request testRequest5 = new Request(
"testReciever3",
testCurrency3,
120.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","donations", "admin"))
);
attemptTransaction(testCurrency3, testRequest5);
//Test number 7 -- Should have invalid requestor
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************** TRANSACTION SEVEN TEST **********************");
System.out.println("********************************************************************");
System.out.println("/// Should be invlaid -- Invalid Requestor ///");
System.out.println();
Currency testCurrency4 = new Currency(
"sender4",
"testReciever10",
new ArrayList<String> (),
500.00,
0.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
Request testRequest6 = new Request(
"testReciever323424",
testCurrency4,
120.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree" ))
);
attemptTransaction(testCurrency4, testRequest6);
//Test number 8 -- The ability to modify a currencies restrictions
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************** TRANSACTION EIGHT TEST **********************");
System.out.println("********************************************************************");
System.out.println("/// Should be valid -- the modifier is the original allocator ///");
System.out.println();
// testCurrency4.addPartModifier("sender4");
testCurrency4.modifyAddRestriction("sender4", "donations");
//Test number 9 -- The ability to modify a currencies restrictions
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************** TRANSACTION NINE TEST **********************");
System.out.println("********************************************************************");
System.out.println("/// Should be invalid -- the modifier is NOT the original allocator or a designated modifer///");
System.out.println();
testCurrency4.modifyAddRestriction("sender5", "admin");
//Test number 10 -- The ability to modify a currencies restrictions
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("*********************** TRANSACTION TEN TEST ***********************");
System.out.println("********************************************************************");
System.out.println("/// Should be invalid -- the modifier is NOT the original allocator, BUT is a designated partialModifier \n however, designated partialModifiers can only further restrict funds ///");
System.out.println();
testCurrency4.addPartModifier("SenderPartial");
testCurrency4.modifyDeleteRestriction("SenderPartial", "donations");
//Test number 11 -- The ability to modify a currencies restrictions
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************* TRANSACTION ELEVEN TEST **********************");
System.out.println("********************************************************************");
System.out.println("/// Should be VALID -- the modifier is NOT the original allocator, BUT is a designated partialModifier -- designated partialModifiers can only further restrict funds ///");
System.out.println();
testCurrency4.addPartModifier("Sender5");
testCurrency4.modifyAddRestriction("Sender5", "admin");
//Test number 12 -- The ability to modify a currencies restrictions
try{
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("********************************************************************");
System.out.println("********************* TRANSACTION TWELEVE TEST **********************");
System.out.println("********************************************************************");
System.out.println("/// Should be VALID -- the modifier is NOT the original allocator, BUT is a designated fullModifier ///");
System.out.println();
testCurrency4.addFullModifier("SenderFullAccess");
testCurrency4.modifyDeleteRestriction("SenderFullAccess", "admin");
return;
}
}