42. Design a Payment Wallet like PayPal

Design a Payment Wallet like PayPal
Build an in-memory payment wallet system supporting user registration, wallet balance management, money transfers, and a single active Fixed Deposit (FD) per user.

Methods

  • void registerUser(String userId)
    - Registers a new unique user with account balance = 0.
    - userId will always be non blank and unique
  • String addMoneyToWallet(String userId, int amount)
    - amount will always be a positive integer ≤ 1000
    - if userId is not already registered then return "user does not exist"
    - else if amount is successfully added then return "success"
  • String spendMoney(String userId, int amount)
    - if userId is not registered then return "user does not exist"
    - if user doesn't have enough balance then return "insufficient balance"
    - if amount is successfully deducted then return "success"
  • String transferMoney(String fromUser, String toUser, int amount)
    - deduct amount from "fromUser" and add to "toUser" account.
    - if "fromUser" is not registered then return "sender does not exist"
    - if "toUser" is not registered then return "receiver does not exist"
    - if "fromUser" doesn't has suffiecient balance then return "insufficient balance"
    - else if amount is successfully transferred then return "success"
  • String createFixedDeposit(String userId, int amount)

    - when this method is called successfully and then after next 5 successful debit transactions
    if user's account balance remains ≥amount then 5% FD's interest is added to their account.
    - if user's account balance becomes less than "amount" by next 5 debit transactions then fixed deposit fails or is broken.
    - "amount" is not deducted from the user’s balance at FD creation; it only acts as a threshold for the balance check after 5 debits.

    - e.g. if amount=82 then after 5 transactions, FD interest= 5% of 82 i.e. 5/100*82 = 4.10
    - 4.10 is rounded to nearest integer Math.floor(4.10+0.5) = Math.floor(4.60) = 4
    - Hence 4 is added to user's account.

    - debit transactions are:
    - spendMoney(userId, amount) for that user, and
    - transferMoney(fromUser, toUser, amount) where this user is fromUser.
    - Only one fixed deposit can be created at any given time.
    - Another fixed deposit can be created only after previous one has successfully completed or has failed.

    - if userId is not registered return "user does not exist"
    - if user doesn't have enough balance i.e. amount > user's current account balance, then return "insufficient balance"
    - if a fixed deposit is active and createFixedDeposit() is called for same user then return "an active fixed deposit already exists"
    - else if fixed deposit is successfully created then return "success"
  • int getAccountBalance(String userId)
    - return user's account balance
    - if "userId" doesn't exist then return -1

Example – Basic Usage

registerUser("alice")
addMoneyToWallet("alice", 500) → "success"
spendMoney("alice", 200)       → "success"
transferMoney("alice", "bob", 100) → "receiver does not exist"
registerUser("bob")
transferMoney("alice", "bob", 100) → "success"
  

Example – Fixed Deposit

registerUser("u1")
addMoneyToWallet("u1", 200)   → "success"

createFixedDeposit("u1", 82)  → "success"
/* Next 5 successful debits counted */

spendMoney("u1", 10)          → success (1)
spendMoney("u1", 10)          → success (2)
registerUser("u2")
transferMoney("u1","u2",20)   → success (3)
spendMoney("u1", 30)          → success (4)
spendMoney("u1", 30)          → success (5)

/* After 5 debits:
   Balance = 100 ≥ 82 → Add interest round(5% * 82) = 4
*/

getAccountBalance("u1") → 104
  




Please use Laptop/Desktop or any other large screen to add/edit code.