Question

In: Computer Science

Create a JavaFX application that lets the user enter the food charge for a meal at...

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%)

Solutions

Expert Solution

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();
    }
}

Related Solutions

Program on Visual Basic, VBA Create an application that lets the user enter a number of...
Program on Visual Basic, VBA Create an application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player...
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player should be 800 pixels in width and 650 pixels in height. The media player should have a TextField that allows the user to type the full path of the .mp3 file to be played. The application should also use 2 Checkbox controls to show/hide the total playing time and the status (methods of MediaPlayer class). Use 2 RadioButton controls to change background colors (red...
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
C# Create an application that asks the user to enter their new password. The password must...
C# Create an application that asks the user to enter their new password. The password must be at least 6 characters long. Develop a custom exception handling case that checks for the password length. (hint: use " .Length " built-in method). If the new password is less than 6 characters long the custom exception should output an error message.
C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT