Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Phat Tu
- * Date: 04/28/17
- * Course Number: CSC-112-D01
- * Course Name: Intermediate Java
- * Problem Number: HW #10
- * Email: pgtu0001@student.stcc.edu
- * Create a server-client that calculates the quadratic formula.
- */
- import java.io.*;
- import java.net.Socket;
- import java.util.*;
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.stage.Stage;
- public class QuadraticClient extends Application {
- class ClientGui extends GridPane {
- private Socket client;
- private Scanner sc;
- private PrintWriter pw;
- public ClientGui() {
- components();
- }
- private void components() {
- HBox hBox1 = new HBox();
- HBox hBox2 = new HBox();
- HBox hBox3 = new HBox();
- HBox hBox4 = new HBox();
- hBox1.setPadding(new Insets(20, 20, 20, 50));
- hBox1.setSpacing(10);
- hBox2.setPadding(new Insets(0, 20, 20, 100));
- hBox2.setSpacing(10);
- hBox3.setPadding(new Insets(0, 20, 20, 150));
- hBox3.setSpacing(10);
- hBox4.setPadding(new Insets(0, 20, 20, 220));
- hBox4.setSpacing(20);
- Label lblA = new Label("Coefficient A:");
- Label lblB = new Label("Coefficient B:");
- Label lblC = new Label("Coefficient C:");
- Label lblStatus = new Label("Status:");
- Label lblRoot1 = new Label("Root 1:");
- Label lblRoot2 = new Label("Root 2:");
- TextField a = new TextField();
- a.setMaxSize(60, 10);
- TextField b = new TextField();
- b.setMaxSize(60, 10);
- TextField c = new TextField();
- c.setMaxSize(60, 10);
- TextField status = new TextField();
- status.setPrefWidth(300);
- status.setEditable(false);
- TextField root1 = new TextField();
- root1.setMaxSize(60, 10);
- root1.setEditable(false);
- TextField root2 = new TextField();
- root2.setMaxSize(60, 10);
- root2.setEditable(false);
- Button btEnter = new Button("Enter");
- Button btReset = new Button("Reset");
- buttonEvents(a, b, c, status, root1, root2, btEnter, btReset);
- hBox1.getChildren().addAll(lblA, a, lblB, b, lblC, c);
- hBox2.getChildren().addAll(lblStatus, status);
- hBox3.getChildren().addAll(lblRoot1, root1, lblRoot2, root2);
- hBox4.getChildren().addAll(btEnter, btReset);
- setConstraints(hBox1, 0, 0);
- setConstraints(hBox2, 0, 1);
- setConstraints(hBox3, 0, 2);
- setConstraints(hBox4, 0, 3);
- getChildren().addAll(hBox1, hBox2, hBox3, hBox4);
- }
- private void buttonEvents(TextField a, TextField b, TextField c, TextField status, TextField root1,
- TextField root2, Button enter, Button reset) {
- enter.setOnAction(e -> {
- generateAnswers(a, b, c, status, root1, root2);
- if(a.getText().isEmpty() || b.getText().isEmpty() || c.getText().isEmpty()) {
- status.setText("ERROR. Fields are empty.");
- root1.setText(null);
- root2.setText(null);
- }
- });
- reset.setOnAction(e -> resetAll(a, b, c, status, root1, root2));
- }
- private void resetAll(TextField a, TextField b, TextField c, TextField status, TextField root1, TextField root2) {
- a.setText("");
- b.setText("");
- c.setText("");
- status.setText(null);
- root1.setText(null);
- root2.setText(null);
- }
- private void generateAnswers(TextField a, TextField b, TextField c, TextField status, TextField root1, TextField root2) {
- try {
- message("Creating socket 5012...");
- client = new Socket("127.0.0.1", 5012);
- message("Connection was succesful.");
- sc = new Scanner(client.getInputStream());
- pw = new PrintWriter(client.getOutputStream());
- pw.println(Double.parseDouble(a.getText()));
- pw.flush();
- pw.println(Double.parseDouble(b.getText()));
- pw.flush();
- pw.println(Double.parseDouble(c.getText()));
- pw.flush();
- String strStatus = sc.nextLine();
- if(Integer.parseInt(strStatus) == 0) {
- status.setText("No real roots");
- root1.setText(null);
- root2.setText(null);
- }
- else if(Integer.parseInt(strStatus) == 1) {
- status.setText(strStatus + " real root");
- String ansRoot1 = sc.nextLine();
- root1.setText(ansRoot1);
- root2.setText(null);
- }
- else if(Integer.parseInt(strStatus) == 2) {
- status.setText(strStatus + " real roots");
- String strRoot1 = sc.nextLine();
- String strRoot2 = sc.nextLine();
- root1.setText(strRoot1);
- root2.setText(strRoot2);
- }
- } catch (Exception exception) {
- status.setText("ERROR");
- root1.setText("ERROR");
- root2.setText("ERROR");
- exception.printStackTrace();
- } finally {
- try {
- pw.close();
- sc.close();
- client.close();
- } catch (Exception ex) {
- }
- }
- }
- }
- @Override
- public void start(Stage primaryStage) {
- try {
- Scene scene = new Scene(new ClientGui(), 550, 190);
- primaryStage.setResizable(false);
- primaryStage.setTitle("Calculate Quadratic Equations v.TU");
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void message(String string) {
- System.out.println(string);
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement