Guest User

Untitled

a guest
Apr 28th, 2017
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. /*
  2.  * Name: Phat Tu
  3.  * Date: 04/28/17
  4.  * Course Number: CSC-112-D01
  5.  * Course Name: Intermediate Java
  6.  * Problem Number: HW #10
  7.  * Create a server-client that calculates the quadratic formula.
  8.  */
  9.  
  10. import java.io.*;
  11. import java.net.Socket;
  12. import java.util.*;
  13. import javafx.application.Application;
  14. import javafx.geometry.Insets;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.TextField;
  19. import javafx.scene.layout.GridPane;
  20. import javafx.scene.layout.HBox;
  21. import javafx.stage.Stage;
  22.  
  23. public class QuadraticClient extends Application {
  24.  
  25.     class ClientGui extends GridPane {
  26.         private Socket client;
  27.         private Scanner sc;
  28.         private PrintWriter pw;
  29.  
  30.         public ClientGui() {
  31.             components();
  32.         }
  33.  
  34.         private void components() {
  35.  
  36.             HBox hBox1 = new HBox();
  37.             HBox hBox2 = new HBox();
  38.             HBox hBox3 = new HBox();
  39.             HBox hBox4 = new HBox();
  40.  
  41.             hBox1.setPadding(new Insets(20, 20, 20, 50));
  42.             hBox1.setSpacing(10);
  43.  
  44.             hBox2.setPadding(new Insets(0, 20, 20, 100));
  45.             hBox2.setSpacing(10);
  46.  
  47.             hBox3.setPadding(new Insets(0, 20, 20, 150));
  48.             hBox3.setSpacing(10);
  49.  
  50.             hBox4.setPadding(new Insets(0, 20, 20, 220));
  51.             hBox4.setSpacing(20);
  52.  
  53.             Label lblA = new Label("Coefficient A:");
  54.             Label lblB = new Label("Coefficient B:");
  55.             Label lblC = new Label("Coefficient C:");
  56.             Label lblStatus = new Label("Status:");
  57.             Label lblRoot1 = new Label("Root 1:");
  58.             Label lblRoot2 = new Label("Root 2:");
  59.  
  60.             TextField a = new TextField();
  61.             a.setMaxSize(60, 10);
  62.             TextField b = new TextField();
  63.             b.setMaxSize(60, 10);
  64.             TextField c = new TextField();
  65.             c.setMaxSize(60, 10);
  66.             TextField status = new TextField();
  67.             status.setPrefWidth(300);
  68.             status.setEditable(false);
  69.             TextField root1 = new TextField();
  70.             root1.setMaxSize(60, 10);
  71.             root1.setEditable(false);
  72.             TextField root2 = new TextField();
  73.             root2.setMaxSize(60, 10);
  74.             root2.setEditable(false);
  75.  
  76.             Button btEnter = new Button("Enter");
  77.             Button btReset = new Button("Reset");
  78.             buttonEvents(a, b, c, status, root1, root2, btEnter, btReset);
  79.            
  80.             hBox1.getChildren().addAll(lblA, a, lblB, b, lblC, c);
  81.             hBox2.getChildren().addAll(lblStatus, status);
  82.             hBox3.getChildren().addAll(lblRoot1, root1, lblRoot2, root2);
  83.             hBox4.getChildren().addAll(btEnter, btReset);
  84.  
  85.             setConstraints(hBox1, 0, 0);
  86.             setConstraints(hBox2, 0, 1);
  87.             setConstraints(hBox3, 0, 2);
  88.             setConstraints(hBox4, 0, 3);
  89.             getChildren().addAll(hBox1, hBox2, hBox3, hBox4);
  90.  
  91.         }
  92.  
  93.         private void buttonEvents(TextField a, TextField b, TextField c, TextField status, TextField root1,
  94.                 TextField root2, Button enter, Button reset) {
  95.    
  96.             enter.setOnAction(e -> {
  97.                 generateAnswers(a, b, c, status, root1, root2);
  98.                 if(a.getText().isEmpty() || b.getText().isEmpty() || c.getText().isEmpty()) {
  99.                     status.setText("ERROR. Fields are empty.");
  100.                     root1.setText(null);
  101.                     root2.setText(null);
  102.                 }
  103.             });
  104.             reset.setOnAction(e -> resetAll(a, b, c, status, root1, root2));
  105.         }
  106.  
  107.         private void resetAll(TextField a, TextField b, TextField c, TextField status, TextField root1, TextField root2) {
  108.             a.setText("");
  109.             b.setText("");
  110.             c.setText("");
  111.             status.setText(null);
  112.             root1.setText(null);
  113.             root2.setText(null);
  114.         }
  115.  
  116.         private void generateAnswers(TextField a, TextField b, TextField c, TextField status, TextField root1, TextField root2) {
  117.             try {
  118.                 message("Creating socket 5012...");
  119.                 client = new Socket("127.0.0.1", 5012);
  120.                 message("Connection was succesful.");
  121.  
  122.                 sc = new Scanner(client.getInputStream());
  123.                 pw = new PrintWriter(client.getOutputStream());
  124.  
  125.                 pw.println(Double.parseDouble(a.getText()));
  126.                 pw.flush();
  127.                 pw.println(Double.parseDouble(b.getText()));
  128.                 pw.flush();
  129.                 pw.println(Double.parseDouble(c.getText()));
  130.                 pw.flush();
  131.  
  132.                 String strStatus = sc.nextLine();
  133.                
  134.                 if(Integer.parseInt(strStatus) == 0) {
  135.                      status.setText("No real roots");
  136.                      root1.setText(null);
  137.                      root2.setText(null);
  138.                 }
  139.                  else if(Integer.parseInt(strStatus) == 1) {
  140.                      status.setText(strStatus + " real root");
  141.                      String ansRoot1 = sc.nextLine();
  142.                      root1.setText(ansRoot1);
  143.                      root2.setText(null);
  144.                  }
  145.                  else if(Integer.parseInt(strStatus) == 2) {
  146.                      status.setText(strStatus + " real roots");
  147.                      String strRoot1 = sc.nextLine();
  148.                      String strRoot2 = sc.nextLine();
  149.                      root1.setText(strRoot1);
  150.                      root2.setText(strRoot2);
  151.                  }
  152.  
  153.             } catch (Exception exception) {
  154.                 status.setText("ERROR");
  155.                 root1.setText("ERROR");
  156.                 root2.setText("ERROR");
  157.                 exception.printStackTrace();
  158.             } finally {
  159.                 try {
  160.                     pw.close();
  161.                     sc.close();
  162.                     client.close();
  163.                 } catch (Exception ex) {
  164.                 }
  165.             }
  166.         }
  167.     }
  168.  
  169.     @Override
  170.     public void start(Stage primaryStage) {
  171.         try {
  172.             Scene scene = new Scene(new ClientGui(), 550, 190);
  173.             primaryStage.setResizable(false);
  174.             primaryStage.setTitle("Calculate Quadratic Equations v.TU");
  175.             primaryStage.setScene(scene);
  176.             primaryStage.show();
  177.         } catch (Exception e) {
  178.             e.printStackTrace();
  179.         }
  180.     }
  181.  
  182.     private void message(String string) {
  183.         System.out.println(string);
  184.     }
  185.  
  186.     public static void main(String[] args) {
  187.         launch(args);
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment