-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
187 lines (171 loc) · 5 KB
/
Main.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
package application;
import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
//Adam Hunt
/**
* 7/28/2021
* Final assignment
* Demonstrates use of various java applcations
* For some reason, the modules are not detected in the modulepath and as such
* a generation of a javadoc is not possible.
*/
public class Main extends Application {
// IO streams
/**
* The primary method through which the code is transmitted from the client to the server and vice versa.
*/
DataOutputStream toServer = null;
DataInputStream fromServer = null;
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Panel p to hold the label and text field
/**
* Sets up the UI
*/
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Enter a number between 1 and 30: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
// Text area to display contents
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
tf.setOnAction(e -> {
try {
// Get the number from the text field
int num = Integer.parseInt(tf.getText().trim());
// Send the request to the server
/**
* Sends the num value input by the user to the server for processing
*/
toServer.writeInt(num);
toServer.flush();
// Get result from the server
/**
* Reads the result sent from the server. Originally intended to recieve
* multiple values but scrapped due to issues.
* @param num is the number typed in by the user.
*/
int resultnumber = fromServer.readInt();
//char resultletter = fromServer.readChar();
String resultletter = "error";
switch (num) {
case 0 :
resultletter = "error";
break;
case 1 :
resultletter = "door";
break;
case 2 :
resultletter = "chamber";
break;
case 3 :
resultletter = "raven";
break;
case 4 :
resultletter = "nevermore";
break;
case 5 :
resultletter = "bird";
break;
case 6 :
resultletter = "lenore";
break;
case 7 :
resultletter = "nothing";
break;
case 8 :
resultletter = "still";
break;
case 9 :
resultletter = "soul";
break;
case 10 :
resultletter = "said";
break;
case 11 :
resultletter = "thy";
break;
case 12 :
resultletter = "bust";
break;
case 13 :
resultletter = "upon";
break;
case 14 :
resultletter = "tapping";
break;
case 15 :
resultletter = "name";
break;
case 16 :
resultletter = "word";
break;
case 17 :
resultletter = "tell";
break;
case 18 :
resultletter = "quoth";
break;
case 19 :
resultletter = "one";
break;
case 20 :
resultletter = "floor";
break;
}
// Display to the text area
/**
* Sends the result to the UI. Also checks whether or not the process was successful.
* @return The number of times the word appears in the poem
*/
if (resultletter.equals("error")) {
ta.appendText("Value outside 1-30. "); }
else {
ta.appendText(resultletter + " appears in the poem " + resultnumber + " times. ");
}
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
// Create a socket to connect to the server
@SuppressWarnings("resource")
Socket socket = new Socket("localhost", 8000);
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}