Write a Java GUI application that will keep track of a company's products. The application must contain the product id, product name and the product price.
On the form, create three text fields t capture the product details and a non editable text area to display the product details. Create a submit button that when clicked will save the product details to a products.txt file. Also include a search button that will allow a user to search for a product based on the product Id.
Create a sequential file (products.txt) that contains data for the following fields:
The product Id, The product name and price
Load the product details from the products.txt file. Populate the text area form load and whenever the new product us saved.
When the search button is clicked, promt the user with an Input box to enter in the product id. Based on the product id, display the name and product price.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.*;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TextField productId = new TextField();
productId.setPromptText("Product ID");
productId.setMaxWidth(200);
TextField productName = new TextField();
productName.setPromptText("Product name");
productName.setMaxWidth(200);
TextField productPrice = new TextField();
productPrice.setPromptText("Product price");
productPrice.setMaxWidth(200);
TextArea details = new TextArea();
details.setEditable(false);
details.setMaxWidth(200);
details.setText(getData());
Button submit = new Button("Submit");
submit.setOnMouseReleased((MouseEvent me) -> {
if (me.getButton() == MouseButton.PRIMARY && productId.getText().length() > 0
&& productName.getText().length() > 0 && productPrice.getText().length() > 0) {
try {
FileWriter out = new FileWriter("products.txt", true);
out.append(productId.getText()).append(" ").append(productName.getText()).append(" ");
out.append(productPrice.getText()).append('\n');
out.flush();
details.appendText(productId.getText() + " " + productName.getText() + " " + productPrice.getText() + '\n');
out.close();
} catch (IOException e) {
}
}
});
Button search = new Button("Search");
search.setOnMouseReleased((MouseEvent me) -> {
if (me.getButton() == MouseButton.PRIMARY && productId.getText().length() > 0) {
try {
String[] data = getData().split("\n");
for (String product : data) {
String[] productDetails = product.split(" ");
if (productDetails[0].equals(productId.getText())) {
productName.setText(productDetails[1]);
productPrice.setText(productDetails[2]);
}
}
} catch (Exception e) {
}
}
});
HBox hBox = new HBox(10, submit, search);
hBox.setAlignment(Pos.CENTER);
VBox vBox = new VBox(10, productId, productName, productPrice, details, hBox);
vBox.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(new StackPane(vBox), 400, 400));
primaryStage.sizeToScene();
primaryStage.show();
}
public String getData() throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("products.txt"));
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
reader.close();
return builder.toString();
}
public static void main(String[] args) {
launch(args);
}
}
Comments
Leave a comment