Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions design-patterns/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,26 @@
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope> <!-- Ensures it's only used at runtime -->
</dependency>

<!-- Logging (Optional but Recommended) -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prateek.PaymentServiceDb.Basics;

public class Card {
String cardNo;
String userName;

public Card(String cardNo, String userName) {
this.cardNo = cardNo;
this.userName = userName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prateek.PaymentServiceDb.Basics;

public class CreditCard extends Card implements PaymentMethod {
public CreditCard(String cardNo, String name) {
super(cardNo, name);
}

public void pay() {
System.out.println("Making payment via Credit Card");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.prateek.PaymentServiceDb.Basics;

public class DebitCard extends Card implements PaymentMethod {
public DebitCard(String cardNo, String userName) {
super(cardNo, userName);
}

@Override
public void pay() {
System.out.println("Making payment via Debit Card");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.prateek.PaymentServiceDb.Basics;

public interface PaymentMethod {
void pay();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.prateek.PaymentServiceDb.Basics;

import java.sql.*;

public class PaymentService {
private static PaymentService instance;
private Connection connection;

// Singleton Constructor
private PaymentService() {
try {
this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdatabase", "root", "AdminSandeep");
createTableIfNotExists();
} catch (Exception e) {
throw new RuntimeException("Database connection failed!", e);
}
}

public static synchronized PaymentService getInstance() {
if (instance == null) {
instance = new PaymentService();
}
return instance;
}

// Ensures Table Exists
private void createTableIfNotExists() {
try (Statement stmt = connection.createStatement()) {
String sql = "CREATE TABLE IF NOT EXISTS payment_methods (" +
"id INT AUTO_INCREMENT PRIMARY KEY, " +
"name VARCHAR(255) UNIQUE NOT NULL, " +
"type ENUM('CREDIT_CARD', 'DEBIT_CARD', 'UPI') NOT NULL, " +
"details TEXT NOT NULL)";
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}

// Add Payment Method with ENUM
public void addPaymentMethod(String name, PaymentMethod pm) {
try {
PaymentType type;
String details;

if (pm instanceof CreditCard) {
type = PaymentType.CREDIT_CARD;
details = ((CreditCard) pm).cardNo;
} else if (pm instanceof DebitCard) {
type = PaymentType.DEBIT_CARD;
details = ((DebitCard) pm).cardNo;
} else if (pm instanceof UPI) {
type = PaymentType.UPI;
details = ((UPI) pm).upiId;
} else {
throw new IllegalArgumentException("Unknown Payment Method");
}

PreparedStatement stmt = connection.prepareStatement(
"INSERT INTO payment_methods (name, type, details) VALUES (?, ?, ?) " +
"ON DUPLICATE KEY UPDATE type=VALUES(type), details=VALUES(details)");

stmt.setString(1, name);
stmt.setString(2, type.name()); // Store ENUM as String
stmt.setString(3, details);
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}

// Retrieve and Make Payment
public void makePayment(String name) {
try {
PreparedStatement stmt = connection.prepareStatement(
"SELECT type, details FROM payment_methods WHERE name = ?");
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();

if (rs.next()) {
PaymentType type = PaymentType.valueOf(rs.getString("type")); // Convert DB String to Enum
String details = rs.getString("details");

PaymentMethod pm;
switch (type) {
case CREDIT_CARD:
pm = new CreditCard(details, name);
break;
case DEBIT_CARD:
pm = new DebitCard(details, name);
break;
case UPI:
pm = new UPI(details, name);
break;
default:
throw new IllegalArgumentException("Unknown Payment Type");
}

pm.pay();
} else {
throw new IllegalArgumentException("Payment method not found: " + name);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.prateek.PaymentServiceDb.Basics;

public enum PaymentType {
CREDIT_CARD, DEBIT_CARD, UPI, WALLET;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.prateek.PaymentServiceDb.Basics;

public class UPI implements PaymentMethod {
String upiId;
String userName;

public UPI(String upiId, String userName) {
this.upiId = upiId;
this.userName = userName;
}

public void pay() {
System.out.println("Making payment via UPI" + upiId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.prateek.PaymentServiceDb;

import org.prateek.PaymentServiceDb.Basics.CreditCard;
import org.prateek.PaymentServiceDb.Basics.DebitCard;
import org.prateek.PaymentServiceDb.Basics.PaymentService;
import org.prateek.PaymentServiceDb.Basics.UPI;

public class Client {
public static void main(String[] args) {
System.out.println("Hello, World!");
PaymentService ps = PaymentService.getInstance();

ps.addPaymentMethod("sandeepCreditCardId", new CreditCard("1234", "Sandeep"));
ps.addPaymentMethod("JohnCredit", new CreditCard("1234-5678-9876", "John"));
ps.addPaymentMethod("JohnUPI", new UPI("john@upi", "John"));
ps.makePayment("JohnCredit");

ps.makePayment("sandeepCreditCardId");
ps.addPaymentMethod("sgDebitCardId", new DebitCard("5678", "Sandeep"));
ps.makePayment("sgDebitCardId");
ps.addPaymentMethod("SgUPIID", new UPI("sg@okhdfcbank", "Sandeep"));
ps.makePayment("SgUPIID");

}
}