Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Code review

2909064Apr 16 2015 — edited Apr 21 2015

Could someone review this code on a Mac or Windows 7 machine and tell me if you have issues?

This is a JavaFX application written with eclipse and Scene Builder

The value you need to enter for the field SrCr is a 1 and the little brown buttons are ToggleButtons as is the Male/Female ToggleButton

Main Class

package appCC;

import javafx.application.Application;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.layout.AnchorPane;

import javafx.fxml.FXMLLoader;

public class CRCL extends Application {

  @Override

  public void start(Stage primaryStage) {

  try {

  AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("crcl.fxml"));

  Scene scene = new Scene(root);

  primaryStage.setScene(scene);

  primaryStage.setTitle("Creatinine Clearance Calculator");

  primaryStage.show();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  public static void main(String[] args) {

  launch(args);

  }

}

Controller Class

package appCC;

import java.awt.Component;

import java.net.URL;

import java.text.DecimalFormat;

import java.text.NumberFormat;

import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.fxml.Initializable;

import javafx.scene.control.*;

public class CRCLController implements Initializable {

  @FXML

  private Label lblLBW;

  @FXML

  private Label lblABW;

  @FXML

  private Label lblTBW;

  @FXML

  private TextField txfAge;

  @FXML

  private TextField txfCI;

  @FXML

  private TextField txfKL;

  @FXML

  private TextField txfHt;

  @FXML

  private TextField txfWt;

  @FXML

  private TextField txfSC;

  @FXML

  private ToggleButton tbnCI;

  @FXML

  private ToggleButton tbnKL;

  @FXML

  private ToggleButton tbnMF;

  @FXML

  private Button btnClear;

  @FXML

  private Button btnCalc;

  NumberFormat nf = new DecimalFormat("##.00");

  private int vAge;

  private float vHtin;

  private float vHtcm;

  private float vWtlb;

  private float vWtkg;

  private int vSex;

  private float vIBW;

  private float vLBW;// Weight for LBW

  private float vSrCr;

  private float vCrCL;// CrCl LBW

  private float vWt;// Weight for AdjBW

  private float vCrCA;// CrCl ABW

  private float vCrCT;// CrCl TBW

  @FXML

  private void onCalc(ActionEvent ep) throws Exception {

  // Test all TextFields

  String regNum1 = (".*[a-z A-Z].*");

  String regNum2 = (".*[a-z A-Z].*");

  String regNum3 = (".*[a-z A-Z].*");

  String regNum4 = (".*[a-z A-Z].*");

  String W1 = String.valueOf(txfAge.getText().trim());

  String W2 = String.valueOf(txfHt.getText().trim());

  String W3 = String.valueOf(txfWt.getText().trim());

  String W4 = String.valueOf(txfSC.getText().trim());

  // NOTE use of .trim() <--

  if (W1.length() > 3

  || W1 == null

  || W1.isEmpty()

  || W1.matches(regNum1)

  || (W2.length() > 3 || W2 == null || W2.isEmpty() || W2

  .matches(regNum2))

  || (W3.length() > 3 || W3 == null || W3.isEmpty() || W3

  .matches(regNum3))

  || (W4.length() > 4 || W4 == null || W4.isEmpty() || W4

  .matches(regNum4))) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2, "Invalid Value Entered",

  "Information", JOptionPane.WARNING_MESSAGE);

  if (W1.length() > 3 || W1 == null || W1.isEmpty()

  || W1.matches(regNum1)) {

  txfAge.setText("");

  txfAge.requestFocus();

  return;

  }

  if (W2.length() > 3 || W2 == null || W2.isEmpty()

  || W2.matches(regNum2)) {

  txfHt.setText("");

  txfHt.requestFocus();

  return;

  }

  if (W3.length() > 3 || W3 == null || W3.isEmpty()

  || W3.matches(regNum3)) {

  txfWt.setText("");

  txfWt.requestFocus();

  return;

  }

  if (W4.length() > 4 || W4 == null || W4.isEmpty()

  || W4.matches(regNum4)) {

  txfSC.setText("");

  txfSC.requestFocus();

  return;

  }

  }

  // Test Range Values of Data Entered in TextFields

  // ================================================

  int Age = Integer.parseInt(txfAge.getText().trim());

  if (Age <= 11 || Age > 110) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2,

  "Age Range 12 yr to 110 yr", "Information",

  JOptionPane.WARNING_MESSAGE);

  txfAge.setText("");

  txfAge.requestFocus();

  return;

  }

  if (!tbnCI.isSelected()) {// !NOT selected Ht in cm evaluated

  float Ht = Float.parseFloat(txfHt.getText().trim());

  if (Ht < 130 || Ht > 213) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2,

  "Height Range 130 cm to 213 cm", "Information",

  JOptionPane.WARNING_MESSAGE);

  txfHt.setText("");

  txfHt.requestFocus();

  return;

  }

  }

  if (tbnCI.isSelected()) {

  float Ht = Float.parseFloat(txfHt.getText().trim());

  if (Ht < 51 || Ht > 83) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2,

  "Height Range 51 in to 83 in", "Information",

  JOptionPane.WARNING_MESSAGE);

  txfHt.setText("");

  txfHt.requestFocus();

  return;

  }

  }

  if (!tbnKL.isSelected()) {// ! NOT selected Wt in kg evaluated

  float Wt = Float.parseFloat(txfWt.getText().trim());

  if (Wt < 30 || Wt > 136) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2,

  "Weight Range 30 kg to 136 kg", "Information",

  JOptionPane.WARNING_MESSAGE);

  txfWt.setText("");

  txfWt.requestFocus();

  return;

  }

  }

  if (tbnKL.isSelected()) {

  float Wt = Float.parseFloat(txfWt.getText().trim());

  if (Wt < 66 || Wt > 300) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2,

  "Weight Range 66 lb to 300 lb", "Information",

  JOptionPane.WARNING_MESSAGE);

  txfWt.setText("");

  txfWt.requestFocus();

  return;

  }

  }

  float SC = Float.parseFloat(txfSC.getText().trim());

  if (SC < 0.4 || SC > 24) {

  Component Reflection2 = null;

  JOptionPane.showMessageDialog(Reflection2,

  "SrCr Range 0.4 mg/dl to 24 mg/dl", "Information",

  JOptionPane.WARNING_MESSAGE);

  txfSC.setText("");

  txfSC.requestFocus();

  return;

  }

  if (tbnCI.isSelected()) {// Selected is inches & cm

  vHtcm = (float) (Float.parseFloat(txfHt.getText().trim()) * 2.54);

  vHtcm = Float.parseFloat(nf.format(vHtcm));

  // inches calculated it is needed for other calculations

  vHtin = (float) (Float.parseFloat(txfHt.getText().trim()));

  vHtin = Float.parseFloat(nf.format(vHtin));

  } else {

  vHtin = (float) (Float.parseFloat(txfHt.getText().trim()) * 0.3938);

  vHtin = Float.parseFloat(nf.format(vHtin));

  }

  if (tbnKL.isSelected()) {// Selected is kg

  vWtkg = (float) (Float.parseFloat(txfWt.getText().trim()) * 0.4546);

  vWtkg = Float.parseFloat(nf.format(vWtkg));

  } else {

  vWtlb = (float) (Float.parseFloat(txfWt.getText().trim()) * 2.2);

  vWtlb = Float.parseFloat(nf.format(vWtlb));

  // kg calculated it is needed for other calculations

  vWtkg = (float) (Float.parseFloat(txfWt.getText().trim()));

  vWtkg = Float.parseFloat(nf.format(vWtkg));

  }

  if (tbnMF.isSelected()) {

  vSex = 1;// Female

  } else {

  vSex = 0;// Male

  }

  // calculation for IBW

  if (tbnMF.isSelected()) {// Selected is FEMALE

  vIBW = (float) (45.5 + (2.3 * (vHtin - 60)));

  vIBW = Float.parseFloat(nf.format(vIBW));

  } else {

  vIBW = (float) (50 + (2.3 * (vHtin - 60)));

  vIBW = Float.parseFloat(nf.format(vIBW));

  }

  // LBW from IBW

  if (vWtkg < vIBW) {

  vLBW = vWtkg;

  } else {

  vLBW = vIBW;

  }

  // convert Age & SrCr to math variables

  vSrCr = (float) (Float.parseFloat(txfSC.getText().trim()));

  vAge = (int) (Integer.parseInt(txfAge.getText().trim()));

  // CrCl LBW

  vCrCL = ((140 - vAge) * vLBW) / (vSrCr * 72);

  vCrCL = Float.parseFloat(nf.format(vCrCL));

  lblLBW.setVisible(true);

  lblLBW.setText(String.valueOf(vCrCL) + " mL/min");

  if (vSex == 1) {// if female

  vCrCL = (float) (vCrCL * 0.85);

  vCrCL = Float.parseFloat(nf.format(vCrCL));

  lblLBW.setVisible(true);

  lblLBW.setText(String.valueOf(vCrCL) + " mL/min");

  }

  // Calculate adjusted body weight for CrCl ABW

  if (vWtkg > vLBW) {

  vWt = (float) (vLBW + (0.4 * (vWtkg - vLBW)));

  } else {

  vWt = vWtkg;

  }

  // CrCl ABW

  vCrCA = ((140 - vAge) * vWt) / (vSrCr * 72);

  vCrCA = Float.parseFloat(nf.format(vCrCA));

  lblABW.setVisible(true);

  lblABW.setText(String.valueOf(vCrCA) + " mL/min");

  if (vSex == 1) {// if female

  vCrCA = (float) (vCrCA * 0.85);

  vCrCA = Float.parseFloat(nf.format(vCrCA));

  lblABW.setVisible(true);

  lblABW.setText(String.valueOf(vCrCA) + " mL/min");

  }

  // CrCl TBW

  vCrCT = ((140 - vAge) * vWtkg) / (vSrCr * 72);

  vCrCT = Float.parseFloat(nf.format(vCrCT));

  lblTBW.setVisible(true);

  lblTBW.setText(String.valueOf(vCrCT) + " mL/min");

  if (vSex == 1) {// if female

  vCrCT = (float) (vCrCT * 0.85);

  vCrCT = Float.parseFloat(nf.format(vCrCT));

  lblTBW.setVisible(true);

  lblTBW.setText(String.valueOf(vCrCT) + " mL/min");

  }

  }

  @FXML

  private void onClear(ActionEvent ep) throws Exception {

  lblLBW.setText(null);

  lblABW.setText(null);

  lblTBW.setText(null);

  txfAge.setText(null);

  txfHt.setText(null);

  txfWt.setText(null);

  txfSC.setText(null);

  tbnCI.setSelected(false);

  tbnCI.setText("cm");

  tbnCI.setStyle("-fx-background-color: tan;-fx-font-weight:bold");

  tbnKL.setSelected(false);

  tbnKL.setText("kg");

  tbnKL.setStyle("-fx-background-color: tan;-fx-font-weight:bold");

  tbnMF.setSelected(false);

  tbnMF.setText("Male");

  tbnMF.setStyle("-fx-background-color: lightblue;-fx-font-weight:bold");

  txfAge.requestFocus();

  }

  // Toggle Button setting code

  @FXML

  private void onPCI(ActionEvent ep) throws Exception {

  if (tbnCI.isSelected()) {

  tbnCI.setText("in");

  tbnCI.setStyle("-fx-font-weight:bold");

  } else {

  tbnCI.setText("cm");

  tbnCI.setStyle("-fx-background-color: tan;-fx-font-weight:bold");

  }

  }

  @FXML

  private void onPKL(ActionEvent ep) throws Exception {

  if (tbnKL.isSelected()) {

  tbnKL.setText("lb");

  tbnKL.setStyle("-fx-font-weight: bold");

  } else {

  tbnKL.setText("kg");

  tbnKL.setStyle("-fx-background-color: tan;-fx-font-weight:bold");

  }

  }

  @FXML

  private void onPMF(ActionEvent ep) throws Exception {

  if (tbnMF.isSelected()) {

  tbnMF.setText("Female");

  tbnMF.setStyle("-fx-background-color: pink;-fx-font-weight:bold");

  // NOTE css inline code construction use ; after first construct

  } else {

  tbnMF.setText("Male");

  tbnMF.setStyle("-fx-background-color: lightblue;-fx-font-weight:bold");

  }

  }

  @Override

  public void initialize(URL arg0, ResourceBundle arg1) {

  }

}

FXML file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.shape.*?>

<?import javafx.scene.text.*?>

<?import javafx.geometry.*?>

<?import javafx.scene.control.*?>

<?import java.lang.*?>

<?import javafx.scene.layout.*?>

<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="420.0" prefWidth="1060.0"

  style="-fx-background-color: lightgrey;" xmlns="http://javafx.com/javafx/8"

  xmlns:fx="http://javafx.com/fxml/1" fx:controller="appCC.CRCLController">

  <children>

  <Pane prefHeight="420.0" prefWidth="1060.0"

  AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"

  AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">

  <children>

  <Rectangle arcHeight="5.0" arcWidth="5.0" fill="transparent"

  height="250.0" layoutX="60.0" layoutY="70.0" opacity="0.99" stroke="blue"

  strokeType="INSIDE" strokeWidth="2.0" width="280.0" />

  <Button fx:id="btnCalc" focusTraversable="false" layoutX="230.0"

  layoutY="340.0" mnemonicParsing="false" onAction="#onCalc"

  prefHeight="28.0" prefWidth="100.0" style="-fx-font-weight: bold;"

  text="Calculate" />

  <Button fx:id="btnClear" focusTraversable="false" layoutX="80.0"

  layoutY="340.0" mnemonicParsing="false" onAction="#onClear"

  prefHeight="28.0" prefWidth="100.0" style="-fx-font-weight: bold;"

  text="Clear Form" />

  <Rectangle arcHeight="5.0" arcWidth="5.0" fill="transparent"

  height="260.0" layoutX="400.0" layoutY="65.0" stroke="blue"

  strokeType="INSIDE" strokeWidth="2.0" width="600.0" />

  <Rectangle arcHeight="5.0" arcWidth="5.0" fill="transparent"

  height="250.0" layoutX="405.0" layoutY="70.0" stroke="blue"

  strokeType="INSIDE" strokeWidth="2.0" width="590.0" />

  <Label layoutX="450.0" layoutY="100.0" text="Calculation Formula">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="450.0" layoutY="140.0" text="Cockcroft and Gault LBW">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="450.0" layoutY="180.0"

  text="Cockcroft and Gault Adjusted BW">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="450.0" layoutY="220.0" text="Cockcroft and Gault True BW">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="780.0" layoutY="100.0" text="Creatinine Clearance">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label fx:id="lblLBW" layoutX="800.0" layoutY="140.0"

  prefHeight="23.0" prefWidth="130.0" visible="false">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label fx:id="lblABW" layoutX="800.0" layoutY="180.0"

  prefHeight="23.0" prefWidth="130.0" visible="false">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label fx:id="lblTBW" layoutX="800.0" layoutY="220.0"

  prefHeight="23.0" prefWidth="130.0" visible="false">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="100.0" layoutY="28.0" text="Patient Demographics">

  <font>

  <Font size="20.0" />

  </font>

  </Label>

  </children>

  </Pane>

  <Rectangle arcHeight="5.0" arcWidth="5.0" fill="transparent"

  height="260.0" layoutX="55.0" layoutY="65.0" opacity="0.99" stroke="blue"

  strokeType="INSIDE" strokeWidth="2.0" width="290.0" />

  <Label layoutX="105.0" layoutY="100.0" text="Age">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="90.0" layoutY="140.0" text="Height">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="90.0" layoutY="180.0" text="Weight">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="90.0" layoutY="220.0" text="Gender">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="105.0" layoutY="260.0" text="SrCr">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="240.0" layoutY="100.0" text="years">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <Label layoutX="240.0" layoutY="260.0" text="mg/dl">

  <font>

  <Font size="18.0" />

  </font>

  </Label>

  <TextField fx:id="txfAge" layoutX="155.0" layoutY="100.0"

  prefHeight="28.0" prefWidth="75.0" />

  <TextField fx:id="txfHt" layoutX="155.0" layoutY="135.0"

  prefHeight="28.0" prefWidth="75.0" />

  <ToggleButton fx:id="tbnCI" layoutX="240.0" layoutY="135.0"

  mnemonicParsing="false" onAction="#onPCI" prefHeight="28.0"

  prefWidth="45.0" style="-fx-background-color: tan; -fx-font-weight: bold;"

  text="cm" />

  <TextField fx:id="txfWt" layoutX="155.0" layoutY="175.0"

  prefHeight="28.0" prefWidth="75.0" />

  <ToggleButton fx:id="tbnKL" layoutX="240.0" layoutY="175.0"

  mnemonicParsing="false" onAction="#onPKL" prefHeight="28.0"

  prefWidth="45.0" style="-fx-background-color: tan; -fx-font-weight: bold;"

  text="kg" />

  <ToggleButton fx:id="tbnMF" layoutX="155.0" layoutY="220.0"

  mnemonicParsing="false" onAction="#onPMF" prefHeight="28.0"

  prefWidth="75.0" style="-fx-background-color: lightblue; -fx-font-weight: bold;"

  text="Male" />

  <TextField fx:id="txfSC" layoutX="155.0" layoutY="258.0"

  prefHeight="28.0" prefWidth="75.0" />

  </children>

</AnchorPane>

This post has been answered by jsmith on Apr 19 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2015
Added on Apr 16 2015
6 comments
1,318 views