-
Notifications
You must be signed in to change notification settings - Fork 0
/
vkProtocol.java
109 lines (94 loc) · 3.8 KB
/
vkProtocol.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
/** Version 2.0
**/
import java.net.*;
import java.io.*;
public class vkProtocol {
private static final int WAITING = 0;
private static final int DISPLAYINGTC = 1;
private static final int DISPLAYINGRESOURCES = 2;
private static final int DOWNLOADING = 3;
private static final int FINISHED = 4;
private static final int NUMRESOURCES = 4;
private int state = WAITING;
private String[] resources = { "iTune", "ZoneAlarm", "WinRar", "Audacity" };
private int[] downloadCounter = {0,0,0,0}; // Download counter for all resources
private String errorMessage = "Invalid input! Please try again!";
public String processInput(String theInput) {
String theOutput = null;
String theMessage = "";
switch(state){
case WAITING :
theMessage = "Here are the terms of reference. Do you accept? Yes or No";
state = DISPLAYINGTC;
theOutput = Integer.toString(state) + "_" + theMessage;
break;
case DISPLAYINGTC :
if (theInput.equalsIgnoreCase("Yes")) {
theOutput = "";
for(int i=0; i< resources.length; i ++){
theMessage = theMessage + resources[i] + "_" + Integer.toString(downloadCounter[i]) + "_";
}
state = DISPLAYINGRESOURCES;
theOutput = Integer.toString(state) + "_" + theMessage;
}
else if (theInput.equalsIgnoreCase("No")){
theMessage = "Bye.";
state = WAITING;
theOutput = Integer.toString(state) + "_" + theMessage;
}
else{
theMessage = errorMessage;
theOutput = Integer.toString(state) + "_" + theMessage;
}
break;
case (DISPLAYINGRESOURCES) :
boolean found = false; //To identify whethere the resource had been found.
try{
int resourceLocation = Integer.parseInt(theInput) - 1; //The resource that the user want.
for (int i=0; i< resources.length; i++){
if (resources[resourceLocation] == resources[i]) {
downloadCounter[i] += 1; // Increase counter by 1
theMessage = "You are downloading " + resources[i];
state = DOWNLOADING;
theOutput = Integer.toString(state) + "_" + theMessage ;
found = true;
break;
}
}
if (!found){
theMessage = errorMessage;
theOutput = Integer.toString(state) + "_" + theMessage;
}
break;
}catch (Exception e){
theMessage = errorMessage;
theOutput = theOutput = Integer.toString(state) + "_" + theMessage;
break;
}
case (DOWNLOADING) :
theMessage = "Enter BYE to exit, and ENTER to continue.";
state = FINISHED;
theOutput = Integer.toString(state) + "_" + theMessage;
break;
case (FINISHED) :
if (theInput.equalsIgnoreCase("enter")) {
for(int i=0; i< resources.length; i ++){
theMessage = theMessage + resources[i] + "_" + Integer.toString(downloadCounter[i]) + "_";
}
state = DISPLAYINGRESOURCES;
theOutput = Integer.toString(state) + "_" + theMessage;
}
else if (theInput.equalsIgnoreCase("bye")){
theMessage = "Bye.";
state = WAITING;
theOutput = Integer.toString(state) + "_" + theMessage;
}
else {
theMessage= errorMessage;
theOutput = Integer.toString(state) + "_" + theMessage;
}
break;
}
return theOutput;
}
}