0% found this document useful (0 votes)
2 views4 pages

Abstract Class

The document defines an interface PaymentMethod and an abstract class AbstractPaymentGateway for handling payment processing. It includes two concrete classes, CreditCardPayment and PayPalPayment, which implement the PaymentMethod interface and extend the AbstractPaymentGateway class, providing specific payment processing and validation logic. A main class, PaymentGatewayDemo, demonstrates the creation and usage of these payment methods.

Uploaded by

todkarjanhavi5
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)
2 views4 pages

Abstract Class

The document defines an interface PaymentMethod and an abstract class AbstractPaymentGateway for handling payment processing. It includes two concrete classes, CreditCardPayment and PayPalPayment, which implement the PaymentMethod interface and extend the AbstractPaymentGateway class, providing specific payment processing and validation logic. A main class, PaymentGatewayDemo, demonstrates the creation and usage of these payment methods.

Uploaded by

todkarjanhavi5
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

// Interface PaymentMethod

interface PaymentMethod {

void processPayment(double amount);

boolean validatePayment();

// Abstract class AbstractPaymentGateway

abstract class AbstractPaymentGateway {

String gatewayName;

double transactionFee;

// Constructor

AbstractPaymentGateway(String gatewayName, double transactionFee) {

[Link] = gatewayName;

[Link] = transactionFee;

// Common method

void logTransaction(String details) {

[Link]("Logging transaction: " + details + " via " + gatewayName);

// Abstract method to be implemented by subclasses

abstract void initiatePayment(double amount);

// Concrete class CreditCardPayment extending AbstractPaymentGateway and implementing


PaymentMethod
class CreditCardPayment extends AbstractPaymentGateway implements PaymentMethod {

String cardNumber;

CreditCardPayment(String gatewayName, double transactionFee, String cardNumber) {

super(gatewayName, transactionFee);

[Link] = cardNumber;

@Override

public void processPayment(double amount) {

[Link]("Processing credit card payment of $" + amount + " with card " +
cardNumber);

logTransaction("Credit Card Payment");

@Override

public boolean validatePayment() {

// Simple validation logic

return [Link]() == 16; // Assuming 16-digit card

@Override

void initiatePayment(double amount) {

if (validatePayment()) {

processPayment(amount + transactionFee);

} else {

[Link]("Invalid credit card details.");

}
}

// Concrete class PayPalPayment extending AbstractPaymentGateway and implementing


PaymentMethod

class PayPalPayment extends AbstractPaymentGateway implements PaymentMethod {

String email;

PayPalPayment(String gatewayName, double transactionFee, String email) {

super(gatewayName, transactionFee);

[Link] = email;

@Override

public void processPayment(double amount) {

[Link]("Processing PayPal payment of $" + amount + " for email " + email);

logTransaction("PayPal Payment");

@Override

public boolean validatePayment() {

// Simple validation logic

return [Link]("@"); // Basic email check

@Override

void initiatePayment(double amount) {

if (validatePayment()) {
processPayment(amount + transactionFee);

} else {

[Link]("Invalid PayPal email.");

// Main class to demonstrate

public class PaymentGatewayDemo {

public static void main(String[] args) {

// Create CreditCardPayment instance

PaymentMethod creditCard = new CreditCardPayment("Stripe", 2.5, "1234567890123456");

((AbstractPaymentGateway) creditCard).initiatePayment(100.0);

[Link]();

// Create PayPalPayment instance

PaymentMethod paypal = new PayPalPayment("PayPal", 1.0, "user@[Link]");

((AbstractPaymentGateway) paypal).initiatePayment(50.0);

You might also like