In: Computer Science
Create a JavaFX application that lets the user enter the food charge for a meal at a restaurant. For example, if $20 is entered as a food charge for a meal then $3.6 should be displayed for the tip, $1.4 should be displayed for sales tax, and $25 should be displayed as a total of all three amounts. Modification 1: create a text box (not a pop up) for the user to enter a percent tip (don't just hardcode it to 18%) Modification 2: create a text box (not a pop up) for the user to enter a percent sales tax (don't just hardcode it to 7%)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Bill extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(5);
grid.setHgap(5);
TextField foodCharge = new TextField(" Food Charge");
TextField tip = new TextField(" Enter Tip ");
TextField salesTax = new TextField(" Sales Tax ");
foodCharge.setEditable(false);
tip.setEditable(false);
salesTax.setEditable(false);
TextField foodChargeValue = new TextField();
TextField tipValue = new TextField();
TextField salesTaxValue = new TextField();
foodChargeValue.setPromptText(" Enter Food Charge ");
tipValue.setPromptText(" Enter Tip Percentage ");
salesTaxValue.setPromptText(" Enter Sales Tax Percentage ");
grid.add(foodCharge, 0, 0);
grid.add(tip, 0, 1);
grid.add(salesTax, 0, 2);
grid.add(foodChargeValue, 1, 0);
grid.add(tipValue, 1, 1);
grid.add(salesTaxValue, 1, 2);
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
TextField tipShow = new TextField();
TextField salesTaxShow = new TextField();
TextField totalShow = new TextField();
if(!foodChargeValue.getText().isEmpty() && !tipValue.getText().isEmpty() && !salesTaxValue.getText().isEmpty()) {
double tipPercentage = Double.parseDouble(tipValue.getText().trim());
double salesTaxPercentage = Double.parseDouble(salesTaxValue.getText().trim());
double foodPrice = Double.parseDouble(foodChargeValue.getText().trim());
double tipAmount = (foodPrice * tipPercentage)/100;
double salesTaxAmount = (foodPrice * salesTaxPercentage)/100;
double totalAmount = foodPrice + tipAmount + salesTaxAmount;
TextField tipLabel = new TextField(" Tip ");
TextField salesTaxLabel = new TextField(" Sales Tax ");
TextField totalLabel = new TextField(" Total ");
tipLabel.setEditable(false);
salesTaxLabel.setEditable(false);
totalLabel.setEditable(false);
grid.add(tipLabel, 0, 4);
grid.add(salesTaxLabel, 0, 5);
grid.add(totalLabel, 0, 6);
tipShow.setEditable(false);
salesTaxShow.setEditable(false);
totalShow.setEditable(false);
tipShow.setText(String.valueOf(tipAmount));
salesTaxShow.setText(String.valueOf(salesTaxAmount));
totalShow.setText(String.valueOf(totalAmount));
grid.add(tipShow, 1, 4);
grid.add(salesTaxShow, 1, 5);
grid.add(totalShow, 1, 6);
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setContentText("Please Enter correct value");
alert.show();
}
}
};
Button btn = new Button("Calculate");
grid.add(btn, 0, 3);
btn.setOnAction(event);
Scene sc = new Scene(grid, 500, 500);
primaryStage.setScene(sc);
primaryStage.show();
}
}