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

Abstract Class - Example 2

Uploaded by

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

Abstract Class - Example 2

Uploaded by

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

PaymentMethod.

java
public abstract class PaymentMethod {
public abstract void processPayment(double amount);
}
=====================================================================================

CreditCard.java
public class CreditCard extends PaymentMethod {
@Override
public void processPayment(double amount) {
System.out.println("Processing payment of " + amount + " with Credit Card");
}
}

=====================================================================================

DebitCard.java

public class DebitCard extends PaymentMethod {


@Override
public void processPayment(double amount) {
System.out.println("Processing payment of " + amount + " with Debit Card");
}
}
=====================================================================================

PayPal.java

public class PayPal extends PaymentMethod {


@Override
public void processPayment(double amount) {
System.out.println("Processing payment of " + amount + " with PayPal");
}
}
AbstractExample2.java -Main

import java.util.Scanner;
public class AbstractExample2 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Please enter the payment method (credit card, debit card, paypal): ");
String paymentMethodChoice = scanner.nextLine();

System.out.print("Enter amount ");


double amount = scanner.nextDouble();

switch (paymentMethodChoice){
case "credit card" -> {
CreditCard creditCard = new CreditCard();
creditCard.processPayment(amount);
}
case "debit card" -> {
DebitCard debitCard = new DebitCard();
debitCard.processPayment(amount);
}

case "paypal" -> {


PayPal payPal = new PayPal();
payPal.processPayment(amount);
}

}
}
Output

You might also like