JavaFX find person with SearchById
enter code here
package application;
public class Person {
String Id;
String fullName;
String street;
String city;
String gender;
String zip;
public Person(String Id) {
this.Id = Id;
}
public Person(String Id,String fullName, String street, String city, String gender, String zip) {
this(Id);
this.fullName=fullName;
this.street = street;
this.city = city;
this.gender = gender;
this.zip = zip;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getSUID() {
return Id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
@Override
public String toString() {
return "Id: " + Id +"Full Name: " + fullName + " | Street: " + street +
" | City: " + city + " | Gender: " + gender + " | Zip: " + zip;
}
}
enter code herepackage application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
import java.nio.file.DirectoryStream.Filter;
import java.util.ArrayList;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
AddressBook addrPane = new AddressBook("C:\\Users\\fener\\eclipse-
workspace\\Projectfx\\src");
primaryStage.setScene(new Scene(addrPane));
primaryStage.setTitle("Address Book");
primaryStage.show();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
}
}
class BasicAddressPane extends GridPane {
// Labels
Label lblId;
Label lblSUID;
Label lblName;
Label lblStreet;
Label lblCity;
Label lblGender;
Label lblZip;
// TextFields
TextField tfId;
TextField tfSUID;
TextField tfName;
TextField tfStreet;
TextField tfCity;
TextField tfGender;
TextField tfZip;
BasicAddressPane() {
// Grid pane settings
this.setPadding(new Insets(10));
this.setHgap(140);
this.setVgap(10);
// Create Labels with Textfield
lblId = createComponent(" ID:", 0, tfId = new TextField(), 0);
lblSUID = createComponent("Search/UpdateID:", 0, tfSUID = new TextField(), 0);
lblName = createComponent("Name:", 0, tfName = new TextField(), 0);
lblStreet = createComponent("Street:", 0, tfStreet = new TextField(), 0);
lblCity = createComponent(" City:", 140, tfCity = new TextField(), 100);
lblGender = createComponent("Gender:", 90, tfGender = new TextField(), 40);
lblZip = createComponent("Zip:", 138, tfZip = new TextField(), 110);
// add id on first row
this.add(lblId, 0, 0, 4, 1);
tfId.setPrefColumnCount(4);
// add id on first row
this.add(lblSUID, 1, 0, 4, 1);
tfSUID.setPrefColumnCount(9);
// add name on second row
this.add(lblName, 0, 1, 4, 1);
tfName.setPrefColumnCount(27);
// add street on third row
this.add(lblStreet, 0, 2, 4, 1);
tfStreet.setPrefColumnCount(27);
// add city, state, zip on fourth row
HBox hBox = new HBox();
hBox.getChildren().addAll(lblCity, lblGender, lblZip);
hBox.setMaxWidth(410);
hBox.setSpacing(1);
this.add(hBox, 0, 3, 4, 1);
}
/**
* @param name - Name of Label
* @param lblWidth - Label max width: 0 = default
* @param textField - TextField to be attached with label
* @param tfWidth - TextField max width: 0 = default
* @return Label
*/
private Label createComponent(String name, double lblWidth, TextField textField, double tfWidth)
{
Label label = new Label(name, textField);
label.setContentDisplay(ContentDisplay.RIGHT);
// set textfield and label custom min width if needed
if (lblWidth > 0) {
label.setMaxWidth(lblWidth);
}
if (tfWidth > 0) {
textField.setMaxWidth(tfWidth);
}
return label;
}
protected void setTextFields(String Id,String SUID,String name, String street, String city,
String gender, String zip) {
tfId.setText(Id);
tfSUID.setText(SUID);
tfName.setText(name);
tfStreet.setText(street);
tfCity.setText(city);
tfGender.setText(gender);
tfZip.setText(zip);
}
}
class AddressBook extends BasicAddressPane {
ArrayList<Person> persons;
int index = 0;
// Buttons
Button btnAdd;
Alert alert = new Alert(AlertType.INFORMATION);
Button btnFirst;
Button btnNext;
Button btnPrev;
Button btnLast;
Button btnSearch;
Button btnUpdate;
Button btnClear;
boolean isNewFile;
String fileName = "AddressBook.dat";
File file;
AddressBook() {
this(System.getProperty("user.dir")); // default path is current working directory
}
AddressBook(String path) {
btnAdd = new Button("Add");
btnFirst = new Button("First");
btnNext = new Button("Next");
btnPrev = new Button("Prev");
btnLast = new Button("Last");
btnSearch = new Button("SearchById");
btnUpdate = new Button("UpdateById");
btnClear = new Button ("CleanTextFields");
// Init buttons and create bottom button panel
HBox hBox = new HBox(10, btnAdd, btnFirst, btnPrev, btnNext, btnLast, btnSearch, btnUpdate,
btnClear);
//hBox.setAlignment(Pos.CENTER);
this.add(hBox, 0, 4, 5, 1);
// create listeners
btnAdd.setOnAction(e -> addPerson());
btnUpdate.setOnAction(e -> updatePerson());
btnFirst.setOnAction(e -> {
index = 0;
refresh();
});
btnLast.setOnAction(e -> {
index = persons.size() - 1;
refresh();
});
btnNext.setOnAction(e -> next());
btnPrev.setOnAction(e -> previous());
btnClear.setOnAction(e -> clear());
// Get address book file
file = new File(path + fileName);
// if file doesn't exist, create it
try {
isNewFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (!isNewFile) {
syncPersons();
} else {
persons = new ArrayList<>();
}
refresh();
}
private void previous() {
if (index > 0) {
index--;
refresh();
tfId.setDisable(true);
}
}
private void next() {
if (index != persons.size() - 1) {
index++;
refresh();
tfId.setDisable(true);
}
}
private void refresh() {
if (persons.isEmpty()) return;
Person c = persons.get(index);
setTextFields(
c.getId(),
c.getSUID(),
c.getFullName(),
c.getStreet(),
c.getCity(),
c.getGender(),
c.getZip()
);
}
private void clear() {
cleanTextFields();
tfId.setDisable(false);
}
private void cleanTextFields() {
tfId.clear();
tfSUID.clear();
tfName.clear();
tfStreet.clear();
tfCity.clear();
tfGender.clear();
tfZip.clear();
}
public void searchID(){
}
public void addPerson() {
persons.add(getCurrentPerson());
index = persons.size() - 1;
refresh();
savePersons();
alert.setContentText("Record is added successfully");
alert.showAndWait();
cleanTextFields();
}
public void updatePerson() {
persons.remove(index);
persons.add(index, getCurrentPerson());
savePersons();
tfId.setDisable(true);
}
public Person getCurrentPerson() {
return new Person(
tfId.getText(),
tfName.getText(),
tfStreet.getText(),
tfCity.getText(),
tfGender.getText(),
tfZip.getText()
);
}
/**
* Reads address book and saves Persons to ArrayList
*/
private void syncPersons() {
// Init new buffer array
persons = new ArrayList<>();
// Byte array for each field
byte[] Id = new byte[4];
byte[] name = new byte[32];
byte[] street = new byte[32];
byte[] city = new byte[20];
byte[] gender = new byte[2];
byte[] zip = new byte[5];
try (FileInputStream input = new FileInputStream(file)) {
while (input.available() >= 95 && -1 != input.read(Id)) {
input.read(name);
input.read(street);
input.read(city);
input.read(gender);
input.read(zip);
persons.add(new Person(
new String(Id, "UTF-8"),
new String(name, "UTF-8"),
new String(street, "UTF-8"),
new String(city, "UTF-8"),
new String(gender, "UTF-8"),
new String(zip, "UTF-8")
));
}
} catch (IOException e) {
e.printStackTrace();
}
}
//FileOperations test= new FileOperations();
public byte[] getFixedSizeByteArray(String string, int fixedSize) {
byte[] src = string.getBytes();
byte[] fixedArray = new byte[fixedSize];
System.arraycopy(src, 0, fixedArray, 0, Math.min(src.length, fixedSize));
return fixedArray;
}
private void savePersons() {
try (FileOutputStream out = new FileOutputStream(file)) {
for (Person c : persons) {
out.write(getFixedSizeByteArray(c.getId(), 4));
out.write(getFixedSizeByteArray(c.getFullName(), 32));
out.write(getFixedSizeByteArray(c.getStreet(), 32));
out.write(getFixedSizeByteArray(c.getCity(), 20));
out.write(getFixedSizeByteArray(c.getGender(), 2));
out.write(getFixedSizeByteArray(c.getZip(), 5));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
When I type the Id number in the Search / UpdateId textfield and press the SearchById button, the person with that ID should come. I need a search function that does them. And SearchByID textfields must be empty, but The Search / Update ID textfield shows the id's number, it should only get input from the user.
from Recent Questions - Stack Overflow https://ift.tt/3akdTmG
https://ift.tt/eA8V8J
Comments
Post a Comment