0% found this document useful (0 votes)
8 views11 pages

Aoop Week 3

The document contains multiple Java applications demonstrating design patterns, including a payment gateway with PayPal and Stripe adapters, a text processing application with spell check and formatting decorators, a video player supporting multiple formats and operating systems, and a stock price viewer using different stock API adapters. Each application showcases the use of interfaces and classes to implement functionality in a modular and extensible way. The main methods in each application illustrate how to utilize the defined classes and interfaces.

Uploaded by

Alphanso Romany
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)
8 views11 pages

Aoop Week 3

The document contains multiple Java applications demonstrating design patterns, including a payment gateway with PayPal and Stripe adapters, a text processing application with spell check and formatting decorators, a video player supporting multiple formats and operating systems, and a stock price viewer using different stock API adapters. Each application showcases the use of interfaces and classes to implement functionality in a modular and extensible way. The main methods in each application illustrate how to utilize the defined classes and interfaces.

Uploaded by

Alphanso Romany
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/ 11

2300032041 Week-3

K.Neeraja

1. public class PaymentGatewayApplication {

// PaymentProcessor Interface
interface PaymentProcessor {
void processPayment(double amount);
}

// PayPal Adapter
static class PayPalAdapter implements PaymentProcessor {
private PayPalAPI payPalAPI;

public PayPalAdapter(PayPalAPI payPalAPI) {


this.payPalAPI = payPalAPI;
}

@Override
public void processPayment(double amount) {
payPalAPI.makePayment(amount);
}
}

// Stripe Adapter
static class StripeAdapter implements PaymentProcessor {
private StripeAPI stripeAPI;

public StripeAdapter(StripeAPI stripeAPI) {


this.stripeAPI = stripeAPI;
}

@Override
public void processPayment(double amount) {
2300032041 Week-3
K.Neeraja

stripeAPI.charge(amount);
}
}

// Payment Client
static class PaymentClient {
private PaymentProcessor paymentProcessor;

public PaymentClient(PaymentProcessor paymentProcessor) {


this.paymentProcessor = paymentProcessor;
}

public void makePayment(double amount) {


paymentProcessor.processPayment(amount);
}
}

// Dummy implementations of PayPalAPI and StripeAPI


static class PayPalAPI {
public void makePayment(double amount) {
System.out.println("Payment of $" + amount + " made using PayPal.");
}
}

static class StripeAPI {


public void charge(double amount) {
System.out.println("Payment of $" + amount + " made using Stripe.");
}
}

// Main Method
public static void main(String[] args) {
2300032041 Week-3
K.Neeraja

// Using PayPal
PayPalAPI payPalAPI = new PayPalAPI();
PaymentProcessor payPalAdapter = new PayPalAdapter(payPalAPI);
PaymentClient payPalClient = new PaymentClient(payPalAdapter);
payPalClient.makePayment(100.0);

// Using Stripe
StripeAPI stripeAPI = new StripeAPI();
PaymentProcessor stripeAdapter = new StripeAdapter(stripeAPI);
PaymentClient stripeClient = new PaymentClient(stripeAdapter);
stripeClient.makePayment(200.0);
}
}

2. public class TextProcessingApplication {

// Base Component
static class TextProcessor {
public String process(String text) {
return text; // Basic processing (no changes)
}
2300032041 Week-3
K.Neeraja

// SpellCheckDecorator
static class SpellCheckDecorator extends TextProcessor {
private TextProcessor textProcessor;

public SpellCheckDecorator(TextProcessor textProcessor) {


this.textProcessor = textProcessor;
}

@Override
public String process(String text) {
text = textProcessor.process(text);
return checkSpelling(text);
}

private String checkSpelling(String text) {


// Dummy implementation of spell checking
return text.replace("teh", "the").replace("recieve", "receive");
}
}

// TextFormatDecorator
static class TextFormatDecorator extends TextProcessor {
private TextProcessor textProcessor;

public TextFormatDecorator(TextProcessor textProcessor) {


this.textProcessor = textProcessor;
}

@Override
public String process(String text) {
2300032041 Week-3
K.Neeraja

text = textProcessor.process(text);
return formatText(text);
}

private String formatText(String text) {


// Dummy implementation of text formatting
return text.toUpperCase();
}
}

// Main Method
public static void main(String[] args) {
// Base TextProcessor
TextProcessor baseProcessor = new TextProcessor();

// Add Spell Checking


TextProcessor spellCheckedProcessor = new SpellCheckDecorator(baseProcessor);

// Add Text Formatting


TextProcessor formattedProcessor = new
TextFormatDecorator(spellCheckedProcessor);

// Test the decorated processor


String inputText = "this is kl university.";
String processedText = formattedProcessor.process(inputText);

System.out.println("Original Text: " + inputText);


System.out.println("Processed Text: " + processedText);
}
}
2300032041 Week-3
K.Neeraja

3. public class client {

// Interface for Video Formats


interface VideoFormat {
void decode(String fileName);
}

// Concrete Classes for Video Formats


static class MP4Player implements VideoFormat {
public void decode(String fileName) {
System.out.println("Decoding MP4: " + fileName);
}
}

static class AVIPlayer implements VideoFormat {


public void decode(String fileName) {
System.out.println("Decoding AVI: " + fileName);
}
}

static class MKVPlayer implements VideoFormat {


public void decode(String fileName) {
System.out.println("Decoding MKV: " + fileName);
}
}

// Interface for Operating Systems


interface OperatingSystem {
void play(String fileName, VideoFormat format);
}
2300032041 Week-3
K.Neeraja

// Concrete Classes for Operating Systems


static class WindowsOS implements OperatingSystem {
public void play(String fileName, VideoFormat format) {
System.out.println("Playing on Windows:");
format.decode(fileName);
}
}

static class LinuxOS implements OperatingSystem {


public void play(String fileName, VideoFormat format) {
System.out.println("Playing on Linux:");
format.decode(fileName);
}
}

static class MacOS implements OperatingSystem {


public void play(String fileName, VideoFormat format) {
System.out.println("Playing on macOS:");
format.decode(fileName);
}
}

// Bridge Class
static class VideoPlayer {
private OperatingSystem os;
private VideoFormat format;

public VideoPlayer(OperatingSystem os, VideoFormat format) {


this.os = os;
this.format = format;
}
2300032041 Week-3
K.Neeraja

public void play(String fileName) {


os.play(fileName, format);
}
}

// Main Method
public static void main(String[] args) {
VideoPlayer windowsPlayer = new VideoPlayer(new WindowsOS(), new MP4Player());
windowsPlayer.play("movie.mp4");

VideoPlayer linuxPlayer = new VideoPlayer(new LinuxOS(), new AVIPlayer());


linuxPlayer.play("clip.avi");

VideoPlayer macPlayer = new VideoPlayer(new MacOS(), new MKVPlayer());


macPlayer.play("series.mkv");
}
}

4. public class StockPriceApplication {


2300032041 Week-3
K.Neeraja

// StockPriceProvider Interface
interface StockPriceProvider {
double getStockPrice(String stockSymbol);
}

// StockAPI_A Adapter
static class StockAPIAAdapter implements StockPriceProvider {
private StockAPIA stockAPIA;

public StockAPIAAdapter(StockAPIA stockAPIA) {


this.stockAPIA = stockAPIA;
}

@Override
public double getStockPrice(String stockSymbol) {
return stockAPIA.getPrice(stockSymbol);
}
}

// StockAPI_B Adapter
static class StockAPIBAdapter implements StockPriceProvider {
private StockAPIB stockAPIB;

public StockAPIBAdapter(StockAPIB stockAPIB) {


this.stockAPIB = stockAPIB;
}

@Override
public double getStockPrice(String stockSymbol) {
// Convert the String returned by StockAPIB to a double
return Double.parseDouble(stockAPIB.fetchStockPrice(stockSymbol));
}
2300032041 Week-3
K.Neeraja

// StockPriceViewer Client Class


static class StockPriceViewer {
private StockPriceProvider stockPriceProvider;

public StockPriceViewer(StockPriceProvider stockPriceProvider) {


this.stockPriceProvider = stockPriceProvider;
}

public void displayStockPrice(String stockSymbol) {


double price = stockPriceProvider.getStockPrice(stockSymbol);
System.out.println("The price of " + stockSymbol + " is: " + price);
}
}

// Dummy implementations of StockAPIA and StockAPIB


static class StockAPIA {
public double getPrice(String stockSymbol) {
// Example implementation: return a dummy price
return 150.50;
}
}

static class StockAPIB {


public String fetchStockPrice(String stockSymbol) {
// Example implementation: return a dummy price as String
return "2800.75";
}
}

// Main Method
2300032041 Week-3
K.Neeraja

public static void main(String[] args) {


// Using StockAPI_A
StockAPIA stockAPIA = new StockAPIA();
StockPriceProvider adapterA = new StockAPIAAdapter(stockAPIA);
StockPriceViewer viewerA = new StockPriceViewer(adapterA);
viewerA.displayStockPrice("AAPL");

// Using StockAPI_B
StockAPIB stockAPIB = new StockAPIB();
StockPriceProvider adapterB = new StockAPIBAdapter(stockAPIB);
StockPriceViewer viewerB = new StockPriceViewer(adapterB);
viewerB.displayStockPrice("GOOGL");
}
}

You might also like