Hi All,
Today I have released new video on "OTP Generation for Payment System Using Supplier Functional Interface | Java 8 Real-Time Project"
This is frequently asked question in Java Interview.
Please watch full video, share, like and Subscribe Youtube channel and press bell icon. So you will get latest video notification.
|
|
|
public class SupplierPaymentDemo { public static void main(String[] args) { Customer customer = new Customer("Rahul", "9884715428", 50000); PaymentService.processPayment(customer, 15000); } } package org.practice.basicprgms; import java.util.Scanner; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Supplier; public class PaymentService { public static Supplier<String> otpSupplier = () -> String.valueOf(ThreadLocalRandom.current().nextInt(100000,999999));
public static Supplier<String> transactionSupplier = () -> "TXN - "+ UUID.randomUUID().toString().substring(0, 8).toUpperCase();
public static Supplier<String> invoiceSupplier = () -> "INV - "+ UUID.randomUUID().toString().substring(0, 6).toUpperCase();
public static void processPayment(Customer customer, double amount) { if(amount <= 100) { System.out.println("Minimum Payment must be 100"); return; }
if(customer.getAccountBalance() < amount) { System.out.println("Insufficient Balance"); return; }
String generatedOTP = otpSupplier.get(); System.out.println("Sending OPT on this Number : "+ customer.getMobileNumber());
System.out.println("Generated OTP : "+ generatedOTP); // Here we have to integrate sms gateway to send the OTP On customer mobile number;
System.out.println("Pleaes Enter your OTP here :"); Scanner scanner = new Scanner(System.in); String enteredOTP = scanner.nextLine().trim();
if(!generatedOTP.equals(enteredOTP)) { System.out.println("Invalid OTP"); return; }
customer.setAccountBalance(customer.getAccountBalance() - amount);
String transactionNumber = transactionSupplier.get();
String invoiceNumber = invoiceSupplier.get();
System.out.println("\n========== PAYMENT SUCCESS =========="); System.out.println("Customer : " + customer.getName()); System.out.println("Amount Paid : " + amount); System.out.println("TransactionId : " + transactionNumber); System.out.println("Invoice No : " + invoiceNumber); System.out.println("Balance Left : " + customer.getAccountBalance()); } } package org.practice.basicprgms; public class Customer { private String name; private String mobileNumber; private double accountBalance;
public Customer() { super(); } public Customer(String name, String mobileNumber, double accountBalance) { super(); this.name = name; this.mobileNumber = mobileNumber; this.accountBalance = accountBalance; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMobileNumber() { return mobileNumber; } public void setMobileNumber(String mobileNumber) { this.mobileNumber = mobileNumber; } public double getAccountBalance() { return accountBalance; } public void setAccountBalance(double accountBalance) { this.accountBalance = accountBalance; } } output: Sending OPT on this Number : 9884715428 Generated OTP : 970750 Pleaes Enter your OTP here : 970750 ========== PAYMENT SUCCESS ========== Customer : Rahul Amount Paid : 15000.0 TransactionId : TXN - 22604242 Invoice No : INV - 0DDC66 Balance Left : 35000.0 |
0 Comments