Advertisement
Guest User

Untitled

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