AlgoMaster Logo
AlgoMasterDesign Payment Methodsmedium

Design Payment Methods

medium

Design a payment processing model for three methods: Card, UPI, and Wallet. Every payment has a reference and charge amount, and every payment can report its processing fee, merchant settlement, and receipt. What changes from one method to another is the fee calculation.

The provided PaymentGateway class supports these operations:

  • PaymentGateway() creates a gateway with no payments recorded.
  • int addCard(String reference, double amount) records a card payment and returns the index it was stored at.
  • int addUpi(String reference, double amount) records a UPI payment and returns the index it was stored at.
  • int addWallet(String reference, double amount) records a wallet payment and returns the index it was stored at.
  • double fee(int index) returns the processing fee for the payment at index:
    • Card is 2% of the amount.
    • UPI is free.
    • Wallet is 1% of the amount, but never more than 10.
  • double settlement(int index) returns the amount the merchant receives, which is the amount less the fee.
  • String receipt(int index) returns "<reference> (<method>): charged $<amount>, fee $<fee>, settled $<settlement>", where the method is Card, UPI, or Wallet and every monetary value displays exactly two decimal places.
  • double totalFees() returns the sum of every payment's full-precision fee.

Indices are assigned in insertion order starting at 0. Adding a fourth payment method later must not require changing receipt, settlement or totalFees.

Your task is to implement the abstract Payment base class and the concrete CardPayment, UpiPayment, and WalletPayment subclasses. Do not modify the provided PaymentGateway class. Keep shared settlement and receipt behavior on the base class, and place each fee rule inside the corresponding subclass.

Example 1:

Input:

Output:

Explanation: The Card payment is stored at index 0 and charges a fee of 1000 × 2% = 20, leaving a settlement of 980. The UPI payment is stored at index 1, charges no fee, and settles the full 500. Together, the two payments produce 20.0 in fees.

Example 2:

Input:

Output:

Explanation: The uncapped Wallet fee would be 2000 × 1% = 20. Because Wallet fees cannot exceed 10, the actual fee is 10 and the merchant receives 1990.

Constraints:
  • 1 <= reference.length <= 40
  • 0 <= amount <= 10^6
  • index always refers to a payment that has been recorded.
  • At most 100 calls will be made across all methods.

How the design is graded

needs 7/10 to pass
  • Inheritance

    Full marks when a base payment type holds the reference and amount and declares the fee calculation, with card, UPI and wallet subtypes supplying their own rule. Lose points heavily when PaymentGateway branches on a stored method flag or an if/switch over payment kind instead of delegating to the payment.

  • Rule containment

    Full marks when each fee rule, including the wallet cap, lives entirely inside its own subtype, and when settlement is derived from amount and fee rather than recomputed per method. Lose points when the cap leaks into the gateway or into a sibling subtype, or when a fee rule is duplicated.

  • Structure and naming

    Full marks when the receipt line is built once on the base type, rates are named rather than repeated as bare numbers, and the gateway stores payment objects rather than parallel arrays. Lose points for printing to stdout or for a running fee total kept in a field that can drift from the stored payments.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new PaymentGateway()null
addCard("ORD-1", 1000)0
addUpi("ORD-2", 500)1
receipt(0)"ORD-1 (Card): charged $1000.00, fee $20.00, settled $980.00"
receipt(1)"ORD-2 (UPI): charged $500.00, fee $0.00, settled $500.00"
totalFees()20

A card charge of 1000 costs 2 percent, so 20. UPI is free. Only the card fee reaches the total.

Run checks these cases. Submit also runs a larger hidden set.