Define a PaymentGateway interface with pay(amount) and name(), plus a default receipt(amount); implement Razorpay-style, Paytm-style and Bank gateways and process an order through each.
Solution
Main.java
interface PaymentGateway {
String name();
boolean pay(double amount);
default String receipt(double amount) {
return pay(amount) ? String.format("%-8s paid Rs %.2f", name(), amount) : name() + " declined Rs " + amount;
}
}
class CardGateway implements PaymentGateway {
public String name() { return "Card"; }
public boolean pay(double a) { return a <= 50000; }
}
class WalletGateway implements PaymentGateway {
private double balance = 2000;
public String name() { return "Wallet"; }
public boolean pay(double a) { if (a > balance) return false; balance -= a; return true; }
}
class BankGateway implements PaymentGateway {
public String name() { return "NetBank"; }
public boolean pay(double a) { return a >= 100; }
}
public class Main {
public static void main(String[] args) {
PaymentGateway[] gateways = {new CardGateway(), new WalletGateway(), new BankGateway()};
for (double amt : new double[]{1500, 60000}) {
for (PaymentGateway g : gateways) System.out.println(g.receipt(amt));
}
}
}
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!