Question

REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return this number if it is a single digit. Otherwise, return the sum of the two digits. // This function is used in Step 1. int getDigit(int number); // Return sum of odd-place digits in in card number (Step 3). int sumOfOddPlace(String cardNumber); // Return true if substr is the prefix for card number. bool startsWith(String cardNumber, String subStr); • The GUI must allow the user to enter the credit card number. • There is a Validate button. When clicked, the entered credit card number will be validated. • If the credit card number is a valid Visa credit card number, display the image of the Visa card. • If the credit card number is a valid American Express credit card number, display the image of the American Express card. • If the credit card number is a valid Master credit card number, display the image of the Master card. • If the credit card number is a valid Discover credit card number, display the image of the Discover card. • If the credit card is invalid, display the Invalid Number image. • Do not allow non-digits from being entered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JavaFx file

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.image.ImageView;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class CreditCardValidation extends Application {
  
String imagePath="C:UsersJayDesktopvalidator.png";
@Override
  
public void start(Stage stage) throws FileNotFoundException {
  
  
  
//creating label Credit card number
  
Text text1 = new Text("Credit Card Number: ");   

//Creating Text Filed Credit card number
TextField textField1 = new TextField();   
//Creating Buttons
Button button1 = new Button("Validate");
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Setting size for the pane
gridPane.setMinSize(800, 300);
//Setting the padding
gridPane.setPadding(new Insets(10, 10, 10, 10));   
//Setting the vertical and horizontal gaps between the columns
gridPane.setVgap(5);
gridPane.setHgap(5);   
//Setting the Grid alignment
gridPane.setAlignment(Pos.TOP_CENTER);
//Arranging all the nodes in the grid
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String cardNumber=textField1.getText();
CreditNumberValidator c1=new CreditNumberValidator();
if(c1.isValid(cardNumber))
{
if(cardNumber.charAt(0)=='4')
imagePath="C:UsersJayDesktopvisa.jpg";
else if(cardNumber.charAt(0)=='5')
imagePath="C:UsersJayDesktopmaster.jpg";
else if(cardNumber.charAt(0)=='6')
imagePath="C:UsersJayDesktopdiscover.jpg";
else
imagePath="C:UsersJayDesktopamericanExpress.jpg";
}
else
{
imagePath="C:UsersJayDesktopinvalid.jpg";
}
try {
Image image;
  
image = new Image(new FileInputStream(imagePath)) {};
ImageView imageView1 = new ImageView(image);
imageView1.setFitHeight(100);
imageView1.setFitWidth(500);
imageView1.setPreserveRatio(true);
gridPane.add(imageView1,0,3);
} catch (FileNotFoundException ex) {
Logger.getLogger(CreditCardValidation.class.getName()).log(Level.SEVERE, null, ex);
}   
}
});
  
gridPane.add(text1, 0, 0);
gridPane.add(textField1, 1, 0);
gridPane.add(button1, 1, 2);

//Creating a scene object
Scene scene = new Scene(gridPane);
//Setting title to the Stage
stage.setTitle("Credit Card Validator");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
  
}

CreditNumberValidator,java
public class CreditNumberValidator {
  
public boolean isValid(String cardNumber)
{
long number=Long.parseLong(cardNumber);
return (getSize(number) >= 13 &&
getSize(number) <= 16) &&
(prefixMatched(number, 4) ||
prefixMatched(number, 5) ||
prefixMatched(number, 37) ||
prefixMatched(number, 6)) &&
((sumOfDoubleEvenPlace(number) +
sumOfOddPlace(number)) % 10 == 0);
}
// Get the result from Step 2
public static int sumOfDoubleEvenPlace(long number)
{
int sum = 0;
String num = number + "";
for (int i = getSize(number) - 2; i >= 0; i -= 2)
sum += getDigit(Integer.parseInt(num.charAt(i) + "") * 2);
  
return sum;
}
  
// Return this number if it is a single digit, otherwise,
// return the sum of the two digits
public static int getDigit(int number)
{
if (number < 9)
return number;
return number / 10 + number % 10;
}
  
// Return sum of odd-place digits in number
public static int sumOfOddPlace(long number)
{
int sum = 0;
String num = number + "";
for (int i = getSize(number) - 1; i >= 0; i -= 2)
sum += Integer.parseInt(num.charAt(i) + "");   
return sum;
}
  
// Return true if the digit d is a prefix for number
public static boolean prefixMatched(long number, int d)
{
return getPrefix(number, getSize(d)) == d;
}
  
// Return the number of digits in d
public static int getSize(long d)
{
String num = d + "";
return num.length();
}
  
// Return the first k number of digits from
// number. If the number of digits in number
// is less than k, return number.
public static long getPrefix(long number, int k)
{
if (getSize(number) > k) {
String num = number + "";
return Long.parseLong(num.substring(0, k));
}
return number;
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Write a javafx GUI program that uses a TextField to allow the user to enter a...

    Write a javafx GUI program that uses a TextField to allow the user to enter a string. When the user presses return, have the action handler for the TextField count the number of vowels (a, e, i, o, u, y) in the string. Use a switch statement for this rather that if statements. Write out the number of vowels in a Label. Case does not matter for the vowels.

  • Modify the code, so that it will check the various user inputs for validity (e.g., months...

    Modify the code, so that it will check the various user inputs for validity (e.g., months being between 1 - 12), - if invalid value was entered, ask user to enter a new (& correct) value, - check to see if new value is valid - if valid allow the user to continue. - if still not valid repeat this process, until valid value is entered __________________________________________________________________________________________________ QUESTION: Desc: This program creates a painting order receipt. Ask for the following...

  • In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student...

    In JAVA please! Write program for exercises You will write a Graphical User Interface(GUI) application to manage student information system. The application should allow: 1. Collect student information and store it in a binary data file. Each student is identified by an unique ID. The user should be able to view/edit an existing student. Do not allow student to edit ID. 2. Collect Course information and store it in a separate data file. Each course is identified by an unique...

  • Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...

    Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT