Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 13

import java.io.

FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

class Patient {

String fname, lname, adress, email, insu, phys;

public Patient(String fname, String lname, String adress, String email, String
insu, String phys) {
this.fname = fname;
this.lname = lname;
this.adress = adress;
this.email = email;
this.insu = insu;
this.phys = phys;
}

public String getFname() {


return fname;
}

public String getLname() {


return lname;
}

public String getAdress() {


return adress;
}

public String getEmail() {


return email;
}

public String getInsu() {


return insu;
}

public String getPhys() {


return phys;
}

}
public class Project10 extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Patient Registration Form");

Label firstNameLabel = new Label("First Name:");


TextField firstNameField = new TextField();
Label lastNameLabel = new Label("Last Name:");
TextField lastNameField = new TextField();
Label dobLabel = new Label("Date of Birth:");
DatePicker dobPicker = new DatePicker();
Label genderLabel = new Label("Gender:");
ToggleGroup t = new ToggleGroup();
RadioButton R1 = new RadioButton("male");
RadioButton R2 = new RadioButton("female");
R1.setToggleGroup(t);
R2.setToggleGroup(t);
Label addressLabel = new Label("Address:");
TextField addressArea = new TextField();
Label phoneLabel = new Label("Phone Number:");
TextField phoneField = new TextField();
Label emailLabel = new Label("Email Address:");
TextField emailField = new TextField();
Label insuranceLabel = new Label("Insurance Provider:");
TextField insuranceField = new TextField();
Label physicianLabel = new Label("Primary Care Physician:");
TextField physicianField = new TextField();
Button submitButton = new Button("Submit");
Button clearButton = new Button("Clear");
Button viewButton = new Button("Display");
Label Welcome = new Label("Patient Registration System");

clearButton.setDefaultButton(true);
viewButton.setDefaultButton(true);
submitButton.setDefaultButton(true);
HBox hb = new HBox();
hb.getChildren().addAll(R1, R2);
HBox hbo = new HBox();
hbo.getChildren().addAll(Welcome);
hbo.setAlignment(Pos.TOP_CENTER);
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(20, 20, 20, 20));
gridPane.setVgap(5);
gridPane.setHgap(5);
gridPane.add(firstNameLabel, 0, 1);
gridPane.add(firstNameField, 1, 1);
gridPane.add(lastNameLabel, 0, 2);
gridPane.add(lastNameField, 1, 2);

gridPane.add(addressLabel, 0, 5);
gridPane.add(addressArea, 1, 5);
gridPane.add(phoneLabel, 0, 6);
gridPane.add(phoneField, 1, 6);
gridPane.add(emailLabel, 0, 7);
gridPane.add(emailField, 1, 7);
gridPane.add(insuranceLabel, 0, 8);
gridPane.add(insuranceField, 1, 8);
gridPane.add(physicianLabel, 0, 9);
gridPane.add(physicianField, 1, 9);
gridPane.add(dobLabel, 0, 10);
gridPane.add(dobPicker, 1, 10);
gridPane.add(hb, 1, 11);
gridPane.add(genderLabel, 0, 11);
gridPane.add(submitButton, 0, 12);
gridPane.add(clearButton, 0, 13);
gridPane.add(viewButton, 0, 14);

gridPane.setAlignment(Pos.CENTER);

BorderPane root = new BorderPane();

Scene scene = new Scene(root, 600, 650);

scene.getStylesheets().add("/css/css.css");
root.setId("a1");
hbo.setId("a2");
root.setCenter(gridPane);
root.setTop(hbo);
primaryStage.setTitle("Hello World!");

primaryStage.setScene(scene);
primaryStage.show();

clearButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
firstNameField.clear();
lastNameField.clear();
addressArea.clear();
phoneField.clear();
emailField.clear();
insuranceField.clear();
physicianField.clear();
}
});
submitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String fname = firstNameField.getText();
String lname = lastNameField.getText();
String adress = addressArea.getText();
String email = emailField.getText();
String insu = insuranceField.getText();
String phys = physicianField.getText();

Alert a;
if (fname.isEmpty()) {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please Fill all fields");
a.show();
} else if (lname.isEmpty()) {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please Fill all fields");
a.show();
} else if (adress.isEmpty()) {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please Fill all fields");
a.show();
} else if (email.isEmpty()) {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please Fill all fields");
a.show();
} else if (insu.isEmpty()) {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please Fill all fields");
a.show();
} else if (phys.isEmpty()) {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please Fill all fields");
a.show();
} else {
try {
PrintWriter op = new PrintWriter(new
FileOutputStream("myfile.txt", true));
op.println(fname + " " + lname + " " + adress + " " + email
+ " " + insu + " " + phys);
op.flush();
op.close();
a = new Alert(Alert.AlertType.INFORMATION);
a.setContentText("Successfull");
a.show();
} catch (Exception e) {

}
}
}
});
viewButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
HBox h = new HBox();
TableView<Patient> table = new TableView();
TableColumn<Patient, String> col1 = new TableColumn<>("First
Name");
TableColumn<Patient, String> col2 = new TableColumn<>("Last Name");
TableColumn<Patient, String> col3 = new TableColumn<>("Addres");
TableColumn<Patient, String> col4 = new TableColumn<>("Email");
TableColumn<Patient, String> col5 = new TableColumn<>("Insurance");
TableColumn<Patient, String> col6 = new TableColumn<>("Physician");
table.getColumns().add(col1);
table.getColumns().add(col2);
table.getColumns().add(col3);
table.getColumns().add(col4);
table.getColumns().add(col5);
table.getColumns().add(col6);
col1.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getFname()));
col2.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getLname()));
col3.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getAdress()));
col4.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getEmail()));
col5.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getInsu()));
col6.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getPhys()));

Patient s;

try {
Scanner sc = new Scanner(new FileInputStream("myfile.txt"));

while (sc.hasNext()) {
String fname, lname, adress, email, insu, phys;
fname = sc.next();
lname = sc.next();
adress = sc.next();
email = sc.next();
insu = sc.next();
phys = sc.next();
s = new Patient(fname, lname, adress, email, insu, phys);
table.getItems().add(s);

}
} catch (FileNotFoundException ex) {
}
h.getChildren().add(table);
Scene scene2 = new Scene(h,600,500);
primaryStage.setScene(scene2);

}
});

public static void main(String[] args) {


launch(args);
}
}

///////////////////////////////////////////////////////////////////////////////////
//////////

package sampleproject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

class Student {

String id, fname, lname, year, sex, department, adress, email, phon, phys;

public Student(String id, String fname, String lname, String year, String sex,
String department, String email, String adress, String phon, String phys) {
this.id = id;
this.fname = fname;
this.lname = lname;
this.year = year;
this.sex = sex;
this.department = department;
this.email = email;
this.adress = adress;
this.phon = phon;
this.phys = phys;
}

public String getId() {


return id;
}

public String getFname() {


return fname;
}

public String getLname() {


return lname;
}

public String getYear() {


return year;
}

public String getSex() {


return sex;
}

public String getDepartment() {


return department;
}

public String getAdress() {


return adress;
}

public String getEmail() {


return email;
}

public String getPhon() {


return phon;
}

public String getPhys() {


return phys;
}
}

public class StudentInfo extends Application {

@Override
public void start(Stage primaryStage) {
// Create the TabPane
TabPane tabPane = new TabPane();

// Create tabs
Tab tab1 = new Tab("Register");
Tab tab2 = new Tab("View Info");
Tab tab3 = new Tab("Search Info");
Tab tab4 = new Tab("Delete Info");

// create content
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(10, 10, 10, 10));
gridPane.setHgap(10);
gridPane.setVgap(10);

// Year ComboBox
ComboBox<String> yearComboBox = new ComboBox<>();
yearComboBox.getItems().addAll("I", "II", "III", "IV", "V");
Label lfname = new Label("First Name:");
Label llname = new Label("Last Name:");
Label lid = new Label("ID:");
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TextField tf3 = new TextField();

// Sex RadioButtons
ToggleGroup sexToggleGroup = new ToggleGroup();
RadioButton maleRadioButton = new RadioButton("Male");
maleRadioButton.setToggleGroup(sexToggleGroup);
RadioButton femaleRadioButton = new RadioButton("Female");
femaleRadioButton.setToggleGroup(sexToggleGroup);
// gridPane.add(new Label("Sex:"), 0, 1);
maleRadioButton.setSelected(true);
HBox h = new HBox();
h.getChildren().addAll(femaleRadioButton, maleRadioButton);
// gridPane.add(h, 1, 1);

//gridPane.add(femaleRadioButton, 2, 1);
// Department ComboBox
ComboBox<String> departmentComboBox = new ComboBox<>();
maleRadioButton.getStyleClass().add("custom-radio-button");
femaleRadioButton.getStyleClass().add("custom-radio-button");
departmentComboBox.getStyleClass().add("custom-combo-box");
departmentComboBox.getItems().addAll("CS", "IT", "IS");

TableView<Student> table = new TableView<>();


// Save and Clear Buttons
Button clearButton = new Button("Clear");
Button saveButton = new Button("Save");
HBox h2 = new HBox();
h2.setSpacing(10);
h2.getChildren().addAll(saveButton, clearButton);
Label addressLabel = new Label("Address:");
TextField addressArea = new TextField();
Label phoneLabel = new Label("Phone Number:");
TextField phoneField = new TextField();
Label emailLabel = new Label("Email Address:");
TextField emailField = new TextField();
Label physicianLabel = new Label("Doctor Name:");
TextField physicianField = new TextField();

gridPane.addRow(0, lfname, tf1);


gridPane.addRow(1, llname, tf2);
gridPane.addRow(2, lid, tf3);
gridPane.addRow(3, new Label("Year:"), yearComboBox);
gridPane.addRow(4, new Label("Sex:"), h);
gridPane.addRow(5, new Label("Department:"), departmentComboBox);
gridPane.add(addressLabel, 0, 6);
gridPane.add(addressArea, 1, 6);
gridPane.add(phoneLabel, 0, 7);
gridPane.add(phoneField, 1, 7);
gridPane.add(emailLabel, 0, 8);
gridPane.add(emailField, 1, 8);
gridPane.add(physicianLabel, 0, 9);
gridPane.add(physicianField, 1, 9);
gridPane.addColumn(1, h2);
gridPane.setAlignment(Pos.CENTER);
// gridPane.add(saveButton, 0, 3);

// Event handling for buttons


saveButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// Handle save button action
String fname = tf1.getText();
String lname = tf2.getText();
String id = tf3.getText();
String year = yearComboBox.getValue();
String sex = ((RadioButton)
sexToggleGroup.getSelectedToggle()).getText();
String department = departmentComboBox.getValue();
String adress = addressArea.getText();
String email = emailField.getText();
String phon = phoneField.getText();
String phys = physicianField.getText();

try {
Alert a;
//check if empty field exist
if (fname.isEmpty() || lname.isEmpty() || id.isEmpty() ||
sex.isEmpty() || adress.isEmpty() || email.isEmpty() || phon.isEmpty() ||
phon.isEmpty()) {

a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Please fill all field");
a.show();
} else {
//file reading to check id redundancy
PrintWriter out = new PrintWriter(new
FileOutputStream("student.txt", true));
Scanner c = new Scanner(new
FileInputStream("student.txt"));
int check = 0;
while (c.hasNext()) {
if (c.next().equals(id)) {
//id is exist
check = 1;
}
}
if (check == 0) {
//create print writer to write data
out.println(id + " " + fname + " " + lname + " " + year
+ " " + sex + " " + department + " " + adress + " " + email + " " + phon + " " +
phys);
out.close();
a = new Alert(Alert.AlertType.INFORMATION);
a.setContentText("Successfully Saved");
a.show();
Scanner c2 = new Scanner(new
FileInputStream("student.txt"));
Student st;
while (c2.hasNext()) {
String id1 = c2.next();
String fname1 = c2.next();
String lname1 = c2.next();
String year1 = c2.next();
String dep1 = c2.next();
String sex1 = c2.next();
String adr1 = c2.next();
String ema1 = c2.next();
String pho1 = c2.next();
String phy1 = c2.next();
st = new Student(id1, fname1, lname1, year1, dep1,
sex1, adr1, ema1, pho1, phy1);
table.getItems().clear();
table.getItems().add(st);
}

} else {
a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Sorry Id already exist");
a.show();
}
}
} catch (FileNotFoundException e) {
}
}
});
clearButton.setOnAction(event -> {
// Handle clear button action

});
HBox h3 = new HBox();
TableColumn<Student, String> col1 = new TableColumn<>("ID");
TableColumn<Student, String> col2 = new TableColumn<>("First Name");
TableColumn<Student, String> col3 = new TableColumn<>("Last Name");
TableColumn<Student, String> col4 = new TableColumn<>("Year");
TableColumn<Student, String> col5 = new TableColumn<>("Department");
TableColumn<Student, String> col6 = new TableColumn<>("Sex");
TableColumn<Student, String> col7 = new TableColumn<>("Addres");
TableColumn<Student, String> col8 = new TableColumn<>("Email");
TableColumn<Student, String> col9 = new TableColumn<>("Phone");
TableColumn<Student, String> col10 = new TableColumn<>("Physician");

//tabPane.getSelectionModel().select(tab2); // Selects Tab 2 as active


table.getColumns().add(col1);
table.getColumns().add(col2);
table.getColumns().add(col3);
table.getColumns().add(col4);
table.getColumns().add(col5);
table.getColumns().add(col6);
table.getColumns().add(col7);
table.getColumns().add(col8);
table.getColumns().add(col9);
table.getColumns().add(col10);
// HBox.setHgrow(table, javafx.scene.layout.Priority.ALWAYS);
// col1.prefWidthProperty().bind(table.widthProperty().multiply(0.1)); //
20% of the TableView width
// col2.prefWidthProperty().bind(table.widthProperty().multiply(0.2)); //
20% of the TableView width
// col3.prefWidthProperty().bind(table.widthProperty().multiply(0.2)); //
20% of the TableView width
// col4.prefWidthProperty().bind(table.widthProperty().multiply(0.1)); //
10% of the TableView width
// col5.prefWidthProperty().bind(table.widthProperty().multiply(0.3)); //
20% of the TableView width
// col6.prefWidthProperty().bind(table.widthProperty().multiply(0.1)); //
10% of the TableView width

col1.setCellValueFactory(celldata -> new


SimpleStringProperty(celldata.getValue().getId()));
col2.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getFname()));
col3.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getLname()));
col4.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getYear()));
col5.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getDepartment()));
col6.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getSex()));
col7.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getAdress()));
col8.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getEmail()));
col9.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getPhon()));
col10.setCellValueFactory(celldata -> new
SimpleStringProperty(celldata.getValue().getPhys()));
try {
Scanner c = new Scanner(new FileInputStream("student.txt"));
Student st;
while (c.hasNext()) {
String id = c.next();
String fname = c.next();
String lname = c.next();
String year = c.next();
String dep = c.next();
String sex = c.next();
String adr = c.next();
String ema = c.next();
String pho = c.next();
String phy = c.next();
st = new Student(id, fname, lname, year, dep, sex, adr, ema, pho,
phy);
table.getItems().add(st);
}

} catch (FileNotFoundException e) {
}

h3.getChildren().addAll(table);
tab2.setContent(h3);
// Set content for tabs
tab1.setContent(gridPane);
// tab2.setContent(new StackPane(saveButton));
//create serach UI for tab3
TextField idtf = new TextField();
Button search = new Button("Search");
HBox h4 = new HBox();
HBox h5 = new HBox();
h4.setSpacing(10);
h4.getChildren().addAll(new Label("ID"), idtf, search);

clearButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
tf1.clear();
tf2.clear();
addressArea.clear();
phoneField.clear();
emailField.clear();
phoneField.clear();
physicianField.clear();
tf3.clear();
}
});
search.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
Scanner c = new Scanner(new FileInputStream("student.txt"));
Student st;
String searchId = idtf.getText();
int check = 0;
while (c.hasNext()) {
String id = c.next();
String fname = c.next();
String lname = c.next();
String year = c.next();
String dep = c.next();
String sex = c.next();
String adr = c.next();
String ema = c.next();
String pho = c.next();
String phy = c.next();

if (id.equals(searchId)) {
check++;
st = new Student(id, fname, lname, year, dep, sex, adr,
ema, pho, phy);
table.getItems().clear();
table.getItems().add(st);
}
}

if (check == 0) {
Alert a = new Alert(Alert.AlertType.ERROR);
a.setContentText("Data not found");
a.show();
} else {
h5.getChildren().add(table);
}

} catch (FileNotFoundException e) {
}

}
});
// tab4.setContent(gridPane);
// tab2.setContent(new StackPane(saveButton));
//create serach UI for tab3
TextField idt = new TextField();
Button delete = new Button("Delete");

HBox h6 = new HBox();


HBox h7 = new HBox();

h6.setSpacing(10);
h6.getChildren().addAll(new Label("ID"), idt, delete);
delete.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
Scanner c = new Scanner(new FileInputStream("student.txt"));
Student st;
String deleteId = idt.getText();
int check = 0;
while (c.hasNext()) {
String id = c.next();
String fname = c.next();
String lname = c.next();
String year = c.next();
String dep = c.next();
String sex = c.next();
String adr = c.next();
String ema = c.next();
String pho = c.next();
String phy = c.next();

if (id.equals(deleteId)) {
check++;
st = new Student(id, fname, lname, year, dep, sex, adr,
ema, pho, phy);
table.getItems().clear();
table.getItems().remove(st);
}
}

if (check == 0) {

} else {
Alert a = new Alert(Alert.AlertType.INFORMATION);
a.setContentText("Data Deleted Successfuly");
a.show();
h7.getChildren().add(table);
}

} catch (FileNotFoundException e) {
}

}
});
VBox v = new VBox();
v.getChildren().addAll(h4, h5);
tab3.setContent(v);
VBox z = new VBox();
z.getChildren().addAll(h6, h7);
tab4.setContent(z);
// Add tabs to the TabPane
tabPane.getTabs().addAll(tab1, tab2, tab3, tab4);
gridPane.setId("a1");
// Create the Scene
Scene scene = new Scene(tabPane, 800, 500);
// scene.getStylesheets().add("/css/style.css");
// Set the title and scene for the Stage
primaryStage.setTitle("Student Information");
primaryStage.setScene(scene);

// Show the Stage


primaryStage.show();
}

public static void main(String[] args) {


launch(args);
}
}

You might also like