0% found this document useful (0 votes)
5 views8 pages

Strategy Design Pattern in Java Code

The document presents a creative assignment on the Strategy Design Pattern, focusing on payment methods in a shopping cart application. It includes Java code for various payment strategies such as CreditCardStrategy and PaypalStrategy, along with a ShoppingCart class to manage items and handle payments. The assignment was submitted by Mr. Tushar G. Wath under the guidance of Ms. Titiksha Bhagat at S. B. Jain Institute of Technology Management & Research.

Uploaded by

tusharw.cseb19
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)
5 views8 pages

Strategy Design Pattern in Java Code

The document presents a creative assignment on the Strategy Design Pattern, focusing on payment methods in a shopping cart application. It includes Java code for various payment strategies such as CreditCardStrategy and PaypalStrategy, along with a ShoppingCart class to manage items and handle payments. The assignment was submitted by Mr. Tushar G. Wath under the guidance of Ms. Titiksha Bhagat at S. B. Jain Institute of Technology Management & Research.

Uploaded by

tusharw.cseb19
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

Creative Assignment Of Design Pattern

On
“Strategy Design Pattern”

Submitted By

Mr. Tushar G. Wath

Roll no.-CS19B47

Under the Guidance of

Ms. Titiksha Bhagat

Department of Computer Science & Engineering

S. B. Jain Institute of Technology Management


&
Research, Nagpur-441501

2021-2022
Code :

[Link]

package [Link];

public interface PaymentStrategy {

public void pay(int amount);

[Link]

package [Link];

public class CreditCardStrategy implements PaymentStrategy {

private String name;

private String cardNumber;

private String cvv;

private String dateOfExpiry;

public CreditCardStrategy(String nm, String ccNum, String cvv, String


expiryDate){

[Link]=nm;

[Link]=ccNum;

[Link]=cvv;

[Link]=expiryDate;
}

@Override

public void pay(int amount) {

[Link](amount +" paid with credit/debit card");

[Link]

package [Link];

public class PaypalStrategy implements PaymentStrategy {

private String emailId;

private String password;

public PaypalStrategy(String email, String pwd){

[Link]=email;

[Link]=pwd;

@Override

public void pay(int amount) {

[Link](amount + " paid using Paypal.");


}

[Link]

package [Link];

public class Item {

private String upcCode;

private int price;

public Item(String upc, int cost){

[Link]=upc;

[Link]=cost;

public String getUpcCode() {

return upcCode;

public int getPrice() {

return price;

}
[Link]

package [Link];

import [Link];

import [Link];

import [Link];

public class ShoppingCart {

//List of items

List<Item> items;

public ShoppingCart(){

[Link]=new ArrayList<Item>();

public void addItem(Item item){

[Link](item);

public void removeItem(Item item){

[Link](item);

}
public int calculateTotal(){

int sum = 0;

for(Item : items){

sum += [Link]();

return sum;

public void pay(PaymentStrategy paymentMethod){

int amount = calculateTotal();

[Link](amount);

[Link]

package [Link];

public class ShoppingCartTest {


public static void main(String[] args) {

ShoppingCart cart = new ShoppingCart();

Item item1 = new Item("1234",10);

Item item2 = new Item("5678",40);

[Link](item1);

[Link](item2);

//pay by paypal

[Link](new PaypalStrategy("myemail@[Link]",
"mypwd"));

//pay by credit card

[Link](new CreditCardStrategy("Pankaj Kumar",


"1234567890123456", "786", "12/15"));

}
Output:-

50 paid using Paypal.

50 paid with credit/debit card.

Github Link:-

[Link]

You might also like