0% found this document useful (0 votes)
34 views

Otp 1

This Java code implements a payment system GUI that generates and verifies one-time passwords (OTPs). It contains widgets like text fields, labels, and buttons to collect user information and payment details. When the "Send OTP" button is clicked, a random 6-digit OTP is generated and displayed. Clicking "Verify OTP" verifies that the entered OTP matches the generated one and notifies the user if the payment was successful or failed.

Uploaded by

sumit paudel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Otp 1

This Java code implements a payment system GUI that generates and verifies one-time passwords (OTPs). It contains widgets like text fields, labels, and buttons to collect user information and payment details. When the "Send OTP" button is clicked, a random 6-digit OTP is generated and displayed. Clicking "Verify OTP" verifies that the entered OTP matches the generated one and notifies the user if the payment was successful or failed.

Uploaded by

sumit paudel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import javax.swing.

*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

public class PaymentSystemWithCVV {

private static int generatedOTP;

private static boolean isVerifying = false;

public static void main(String[] args) {

JFrame frame = new JFrame("Payment System with CVV");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 300);

frame.getContentPane().setBackground(new Color(248, 248, 248)); // Light gray background

JPanel mainPanel = new JPanel(new BorderLayout());

mainPanel.setBackground(new Color(51, 153, 255)); // Blue header background

JPanel orangeStrip = new JPanel();

orangeStrip.setBackground(new Color(255, 165, 0));

orangeStrip.setPreferredSize(new Dimension(50, frame.getHeight()));

mainPanel.add(orangeStrip, BorderLayout.WEST);

JPanel panel = new JPanel(new GridBagLayout());

panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new
Color(51, 153, 255), 2), "Payment Details"));

panel.setBackground(new Color(255, 255, 255)); // White panel background

JLabel titleLabel = new JLabel("PAYMENT FOR FOOD");

titleLabel.setFont(new Font("Arial", Font.BOLD, 24));


titleLabel.setHorizontalAlignment(SwingConstants.CENTER);

mainPanel.add(titleLabel, BorderLayout.NORTH);

frame.add(mainPanel, BorderLayout.CENTER);

mainPanel.add(panel, BorderLayout.CENTER);

GridBagConstraints constraints = new GridBagConstraints();

constraints.insets = new Insets(5, 5, 5, 5);

Font customFont = new Font("Arial", Font.BOLD, 16);

Color componentBackgroundColor = new Color(240, 240, 240);

JLabel nameLabel = new JLabel("Full Name:");

nameLabel.setFont(customFont);

constraints.gridx = 0;

constraints.gridy = 0;

panel.add(nameLabel, constraints);

JTextField nameField = new JTextField(20);

nameField.setFont(customFont);

nameField.setBackground(componentBackgroundColor);

constraints.gridx = 1;

constraints.gridy = 0;

panel.add(nameField, constraints);

JLabel mobileLabel = new JLabel("Mobile Number:");

mobileLabel.setFont(customFont);

constraints.gridx = 0;

constraints.gridy = 1;

panel.add(mobileLabel, constraints);
JTextField mobileNumberField = new JTextField(15);

mobileNumberField.setFont(customFont);

mobileNumberField.setBackground(componentBackgroundColor);

constraints.gridx = 1;

constraints.gridy = 1;

panel.add(mobileNumberField, constraints);

JLabel bankLabel = new JLabel("Bank Account Number:");

bankLabel.setFont(customFont);

constraints.gridx = 0;

constraints.gridy = 2;

panel.add(bankLabel, constraints);

JTextField bankNumberField = new JTextField(15);

bankNumberField.setFont(customFont);

bankNumberField.setBackground(componentBackgroundColor);

constraints.gridx = 1;

constraints.gridy = 2;

panel.add(bankNumberField, constraints);

JLabel cvvLabel = new JLabel("CVV Number:");

cvvLabel.setFont(customFont);

constraints.gridx = 0;

constraints.gridy = 3;

panel.add(cvvLabel, constraints);

JTextField cvvField = new JTextField(3);

cvvField.setFont(customFont);

cvvField.setBackground(componentBackgroundColor);

constraints.gridx = 1;
constraints.gridy = 3;

panel.add(cvvField, constraints);

JButton sendOTPButton = new JButton("Send OTP");

sendOTPButton.setFont(customFont);

sendOTPButton.setBackground(new Color(102, 204, 102)); // Green button

sendOTPButton.setForeground(Color.WHITE); // White text

constraints.gridx = 0;

constraints.gridy = 4;

constraints.gridwidth = 2;

panel.add(sendOTPButton, constraints);

JLabel otpLabel = new JLabel("Enter OTP:");

otpLabel.setFont(customFont);

constraints.gridx = 0;

constraints.gridy = 5;

constraints.gridwidth = 1;

panel.add(otpLabel, constraints);

JTextField otpField = new JTextField(6);

otpField.setFont(customFont);

otpField.setBackground(componentBackgroundColor);

constraints.gridx = 1;

constraints.gridy = 5;

panel.add(otpField, constraints);

JButton verifyOTPButton = new JButton("Verify OTP");

verifyOTPButton.setFont(customFont);

verifyOTPButton.setBackground(new Color(255, 102, 102)); // Red button

verifyOTPButton.setForeground(Color.WHITE); // White text

constraints.gridx = 0;
constraints.gridy = 6;

constraints.gridwidth = 2;

panel.add(verifyOTPButton, constraints);

JLabel loadingLabel = new JLabel("Verifying...");

loadingLabel.setFont(customFont);

loadingLabel.setForeground(Color.BLUE);

loadingLabel.setVisible(false);

constraints.gridx = 0;

constraints.gridy = 7;

constraints.gridwidth = 2;

panel.add(loadingLabel, constraints);

sendOTPButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

generatedOTP = new Random().nextInt(900000) + 100000;

JOptionPane.showMessageDialog(frame, "OTP sent to your mobile: " + generatedOTP);

});

verifyOTPButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (isVerifying) {

return;

isVerifying = true;

loadingLabel.setVisible(true);

new Thread(() -> {

try {

Thread.sleep(3000);
isVerifying = false;

SwingUtilities.invokeLater(() -> {

loadingLabel.setVisible(false);

try {

int enteredOTP = Integer.parseInt(otpField.getText());

if (enteredOTP == generatedOTP) {

JOptionPane.showMessageDialog(frame, "OTP is valid. Payment successful!");

} else {

JOptionPane.showMessageDialog(frame, "Invalid OTP. Payment failed.");

} catch (NumberFormatException ex) {

JOptionPane.showMessageDialog(frame, "Please enter a valid OTP.");

});

} catch (InterruptedException ex) {

ex.printStackTrace();

}).start();

});

frame.setVisible(true);

You might also like