Commit 50ce090e authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Initial commit: Task 6 completed - Sealed class PaymentException with pattern matching

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="ms-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/untitled.iml" filepath="$PROJECT_DIR$/untitled.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
public final class InvalidOfferAppliedException extends PaymentException {
public InvalidOfferAppliedException(String message) {
super(message);
}
}
public final class InvalidPaymentMethodException extends PaymentException {
public InvalidPaymentMethodException(String message) {
super(message);
}
}
public class Main {
public static void processPaymentMethod(String method, String offer) {
PaymentService paymentService = new PaymentService();
try {
paymentService.paymentMethod(method, offer);
} catch (PaymentException e) {
if (e instanceof InvalidPaymentMethodException ipm) {
System.out.println("Payment failed: " + ipm.getMessage());
} else if (e instanceof InvalidOfferAppliedException ioa) {
System.out.println("Offer error: " + ioa.getMessage());
} else {
System.out.println("Unknown payment error: " + e.getMessage());
}
}
}
public static void main(String[] args) {
processPaymentMethod("cash", "NONE"); // invalid payment method
processPaymentMethod("card", "UPI10"); // invalid offer for card
processPaymentMethod("upi", "UPI10"); // valid
}
}
public sealed class PaymentException extends Exception
permits InvalidPaymentMethodException, InvalidOfferAppliedException {
public PaymentException(String message) {
super(message);
}
}
public class PaymentService {
public void paymentMethod(String method, String offer) throws PaymentException {
if (!method.equalsIgnoreCase("card") && !method.equalsIgnoreCase("upi")) {
throw new InvalidPaymentMethodException("Unsupported payment method: " + method);
}
if (method.equalsIgnoreCase("card") && offer.equalsIgnoreCase("UPI10")) {
throw new InvalidOfferAppliedException("Card payments do not support UPI10 offer.");
}
System.out.println("Payment successful using " + method + " with offer " + offer);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment