Skip to content

Commit 29e865a

Browse files
committed
not finished
1 parent 708e327 commit 29e865a

File tree

4 files changed

+290
-0
lines changed

4 files changed

+290
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package exercise_31_06Client;
2+
3+
import java.io.*;
4+
import java.net.*;
5+
import javafx.application.Application;
6+
import javafx.geometry.Insets;
7+
import javafx.geometry.Pos;
8+
import javafx.scene.Scene;
9+
import javafx.scene.control.Button;
10+
import javafx.scene.control.Label;
11+
import javafx.scene.control.TextField;
12+
import javafx.scene.layout.BorderPane;
13+
import javafx.scene.layout.GridPane;
14+
import javafx.scene.layout.HBox;
15+
import javafx.stage.Stage;
16+
17+
public class Exercise_31_06Client extends Application implements StudentAddressConstants {
18+
private TextField tfName = new TextField();
19+
private TextField tfStreet = new TextField();
20+
private TextField tfCity = new TextField();
21+
private TextField tfState = new TextField();
22+
private TextField tfZip = new TextField();
23+
24+
private Button btAdd = new Button("Add");
25+
private Button btFirst = new Button("First");
26+
private Button btNext = new Button("Next");
27+
private Button btPrevious = new Button("Previous");
28+
private Button btLast = new Button("Last");
29+
30+
private String host = "localhost";
31+
private Socket socket;
32+
private ObjectOutputStream outputToServer;
33+
private ObjectInputStream inputFromServer;
34+
35+
@Override
36+
public void start(Stage primaryStage) {
37+
BorderPane borderPane = new BorderPane();
38+
borderPane.setPadding(new Insets(5, 15, 5, 15));
39+
40+
GridPane pane = new GridPane();
41+
pane.setVgap(3);
42+
pane.setHgap(3);
43+
pane.setPadding(new Insets(3));
44+
pane.add(new Label("Name"), 0, 0);
45+
pane.add(tfName, 1, 0);
46+
pane.add(new Label("Street"), 0, 1);
47+
pane.add(tfStreet, 1, 1);
48+
pane.add(new Label("City"), 0, 2);
49+
50+
HBox hBox = new HBox(2);
51+
pane.add(hBox, 1, 2);
52+
hBox.getChildren().addAll(tfCity, new Label("State"), tfState, new Label("Zip"), tfZip);
53+
pane.setAlignment(Pos.CENTER);
54+
borderPane.setCenter(pane);
55+
56+
HBox hBoxForButtons = new HBox(5);
57+
hBoxForButtons.getChildren().addAll(btAdd, btFirst, btNext, btPrevious, btLast);
58+
hBoxForButtons.setAlignment(Pos.CENTER);
59+
borderPane.setBottom(hBoxForButtons);
60+
61+
tfName.setPrefColumnCount(15);
62+
tfStreet.setPrefColumnCount(15);
63+
tfCity.setPrefColumnCount(10);
64+
tfState.setPrefColumnCount(2);
65+
tfZip.setPrefColumnCount(3);
66+
67+
try {
68+
socket = new Socket(host, 8000);
69+
outputToServer = new ObjectOutputStream(socket.getOutputStream());
70+
inputFromServer = new ObjectInputStream(socket.getInputStream());
71+
}
72+
catch (IOException ex) {
73+
ex.printStackTrace();
74+
}
75+
76+
btAdd.setOnAction(e -> {
77+
try {
78+
StudentAddress address = getDataFromTextFields();
79+
outputToServer.writeInt(ADD);
80+
outputToServer.writeObject(address);
81+
}
82+
catch (IOException ex) {
83+
ex.printStackTrace();
84+
}
85+
});
86+
87+
setButtonAction(btFirst, FIRST);
88+
setButtonAction(btNext, NEXT);
89+
setButtonAction(btPrevious, PREVIOUS);
90+
setButtonAction(btLast, LAST);
91+
92+
Scene scene = new Scene(borderPane);
93+
primaryStage.setTitle("Exercise_31_06Client");
94+
primaryStage.setScene(scene);
95+
primaryStage.show();
96+
}
97+
98+
private void setButtonAction(Button bt, int request) {
99+
bt.setOnAction(e -> {
100+
try {
101+
outputToServer.writeInt(request);
102+
setTextFields((StudentAddress)(inputFromServer.readObject()));
103+
}
104+
catch (IOException ex) {
105+
ex.printStackTrace();
106+
}
107+
catch (ClassNotFoundException ex) {
108+
ex.printStackTrace();
109+
}
110+
});
111+
}
112+
113+
private void setTextFields(StudentAddress data) {
114+
tfName.setText(data.getName());
115+
tfStreet.setText(data.getStreet());
116+
tfCity.setText(data.getCity());
117+
tfState.setText(data.getState());
118+
tfZip.setText(data.getZip());
119+
}
120+
121+
private StudentAddress getDataFromTextFields() {
122+
StudentAddress data = new StudentAddress(tfName.getText(), tfStreet.getText(),
123+
tfCity.getText(), tfState.getText(), tfZip.getText());
124+
return data;
125+
}
126+
127+
public static void main(String[] args) {
128+
launch(args);
129+
}
130+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package exercise_31_06Client;
2+
3+
import java.io.Serializable;
4+
5+
public class StudentAddress implements Serializable, StudentAddressConstants {
6+
private static final long serialVersionUID = 1L;
7+
private String name;
8+
private String street;
9+
private String city;
10+
private String state;
11+
private String zip;
12+
13+
public StudentAddress(String name, String street, String city, String state, String zip) {
14+
this.name = name;
15+
this.street = street;
16+
this.city = city;
17+
this.state = state;
18+
this.zip = zip;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public String getStreet() {
26+
return street;
27+
}
28+
29+
public String getCity() {
30+
return city;
31+
}
32+
33+
public String getState() {
34+
return state;
35+
}
36+
37+
public String getZip() {
38+
return zip;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "Name: " + getName() + "\n" + "Street: " + getStreet() + "\n" +
44+
"City: " + getCity() + "\n" + "State: " + getState() + "\n" + "getZip: " + getZip();
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package exercise_31_06Client;
2+
3+
public interface StudentAddressConstants {
4+
public static final int NAME = 32;
5+
public static final int STREET = 32;
6+
public static final int CITY = 20;
7+
public static final int STATE = 4;
8+
public static final int ZIP = 5;
9+
public static final int TOTAL = NAME + STREET + CITY + STATE + ZIP;
10+
public static final int ADD = 1;
11+
public static final int FIRST = 2;
12+
public static final int NEXT = 3;
13+
public static final int PREVIOUS = 4;
14+
public static final int LAST = 5;
15+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package exercise_31_06Server;
2+
3+
import exercise_31_06Client.StudentAddress;
4+
import exercise_31_06Client.StudentAddressConstants;
5+
import java.io.*;
6+
import java.net.*;
7+
import java.util.ArrayList;
8+
9+
public class Exercise_31_06Server implements StudentAddressConstants {
10+
private ObjectOutputStream outputToClient;
11+
private ObjectInputStream inputFromClient;
12+
private ArrayList<StudentAddress> listOfStudentAddresses = new ArrayList<>();
13+
private int currentIndex = 0;
14+
private int countOfClients = 0;
15+
16+
public static void main(String[] args) {
17+
new Exercise_31_06Server();
18+
}
19+
20+
public Exercise_31_06Server() {
21+
try {
22+
ServerSocket serverSocket = new ServerSocket(8000);
23+
System.out.println("Server started!");
24+
25+
while(countOfClients < 2) {
26+
Socket socket = serverSocket.accept();
27+
countOfClients++;
28+
System.out.println("New client has been connected.");
29+
new Thread(new HandleAClient(socket)).start();
30+
}
31+
}
32+
catch(IOException ex) {
33+
ex.printStackTrace();
34+
}
35+
}
36+
37+
private class HandleAClient implements Runnable {
38+
private Socket socket;
39+
40+
public HandleAClient(Socket socket) {
41+
this.socket = socket;
42+
}
43+
44+
@Override
45+
public void run() {
46+
try{
47+
inputFromClient = new ObjectInputStream(socket.getInputStream());
48+
outputToClient = new ObjectOutputStream(socket.getOutputStream());
49+
50+
while(true) {
51+
int request = inputFromClient.readInt();
52+
switch(request) {
53+
case ADD :
54+
StudentAddress element = (StudentAddress)(inputFromClient.readObject());
55+
listOfStudentAddresses.add(element);
56+
currentIndex = 0;
57+
break;
58+
case FIRST :
59+
if(!listOfStudentAddresses.isEmpty()) {
60+
outputToClient.writeObject(listOfStudentAddresses.get(0));
61+
currentIndex = 0;
62+
}
63+
break;
64+
case NEXT :
65+
if(currentIndex < listOfStudentAddresses.size() - 1) {
66+
outputToClient.writeObject(listOfStudentAddresses.get(++currentIndex));
67+
}
68+
break;
69+
case PREVIOUS :
70+
if(currentIndex > 0 && !listOfStudentAddresses.isEmpty()) {
71+
outputToClient.writeObject(listOfStudentAddresses.get(--currentIndex));
72+
}
73+
break;
74+
case LAST :
75+
if(!listOfStudentAddresses.isEmpty()) {
76+
currentIndex = listOfStudentAddresses.size() - 1;
77+
outputToClient.writeObject(listOfStudentAddresses.get(currentIndex));
78+
}
79+
break;
80+
default :
81+
break;
82+
}
83+
for(StudentAddress data : listOfStudentAddresses) {
84+
System.out.println(data);
85+
}
86+
}
87+
}
88+
catch(IOException ex) {
89+
ex.printStackTrace();
90+
}
91+
catch (ClassNotFoundException ex) {
92+
ex.printStackTrace();
93+
}
94+
finally {
95+
countOfClients--;
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)