0% found this document useful (0 votes)
3 views2 pages

Assignment 11

The document presents a Java implementation of a payment processing system using the Strategy design pattern. It defines various payment methods such as Credit Card, PayPal, and Bitcoin, each implementing a common interface. The ShoppingCart class allows users to add amounts and checkout using their selected payment method.

Uploaded by

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

Assignment 11

The document presents a Java implementation of a payment processing system using the Strategy design pattern. It defines various payment methods such as Credit Card, PayPal, and Bitcoin, each implementing a common interface. The ShoppingCart class allows users to add amounts and checkout using their selected payment method.

Uploaded by

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

import java.util.

Scanner;

interface PaymentStrategy {
void pay(int amount);
}

class CreditCardPayment implements PaymentStrategy {


private String name;
private String cardNumber;
private String cvv;
private String expiryDate;

public CreditCardPayment(String name, String cardNumber, String cvv, String


expiryDate) {
this.name = name;
this.cardNumber = cardNumber;
this.cvv = cvv;
this.expiryDate = expiryDate;
}

public void pay(int amount) {


System.out.println("Paid " + amount + " using Credit Card.");
}
}

class PayPalPayment implements PaymentStrategy {


private String email;
private String password;

public PayPalPayment(String email, String password) {


this.email = email;
this.password = password;
}

public void pay(int amount) {


System.out.println("Paid " + amount + " using PayPal.");
}
}

class BitcoinPayment implements PaymentStrategy {


private String bitcoinAddress;

public BitcoinPayment(String bitcoinAddress) {


this.bitcoinAddress = bitcoinAddress;
}

public void pay(int amount) {


System.out.println("Paid " + amount + " using Bitcoin.");
}
}

class ShoppingCart {
private int totalAmount;

public void addAmount(int amount) {


totalAmount += amount;
}

public void checkout(PaymentStrategy paymentMethod) {


paymentMethod.pay(totalAmount);
}
}

public class assign11 {


public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
Scanner sc = new Scanner(System.in);

cart.addAmount(500);

System.out.println("Select payment method: \n1. Credit Card \n2. PayPal \


n3. Bitcoin");
int choice = sc.nextInt();

PaymentStrategy paymentMethod;
switch (choice) {
case 1:
paymentMethod = new CreditCardPayment("John Doe", "123456789",
"123", "12/25");
break;
case 2:
paymentMethod = new PayPalPayment("[email protected]",
"password123");
break;
case 3:
paymentMethod = new BitcoinPayment("1A2b3C4D5E6F7G8H9I0J");
break;
default:
System.out.println("Invalid choice");
return;
}

cart.checkout(paymentMethod);
}
}

You might also like