gvsandeep

Wallet

Jan 31st, 2023
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | Source Code | 0 0
  1. package com.pharmeasy.demo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import org.springframework.transaction.annotation.Transactional;
  7.  
  8. public class DemoApplication {
  9.  
  10.   public static void main(String[] args) {
  11.     Wallet walletAccount = new WalletAccount();
  12.     String input = "1,100,200,200,1000,1";
  13.     walletAccount.load(input);
  14.     System.out.println("Balance :" + walletAccount.getBalance());
  15.     System.out.println("Coins :" + walletAccount.peek());
  16.  
  17.     walletAccount.spend(30L);
  18.     System.out.println("Balance :" + walletAccount.getBalance());
  19.     System.out.println("Coins :" + walletAccount.peek());
  20.   }
  21.  
  22. }
  23.  
  24. interface Wallet {
  25.  
  26.   /**
  27.    * Load money into my wallet
  28.    */
  29.   void load(String coins);
  30.  
  31.   /**
  32.    * Return current balance of my wallet
  33.    */
  34.   Long getBalance();
  35.  
  36.   /**
  37.    * Return the coins in the wallet. (peek does NOT need to be performant as to be used solely for
  38.    * testing)
  39.    */
  40.   Collection<Long> peek();
  41.  
  42.   /**
  43.    * Spend an [amount] of money from Wallet and ensure the wallet is updated (any change should be
  44.    * added back to the wallet as a single amount) Return single change amount (if any change due) or
  45.    * zero.
  46.    */
  47.   Long spend(Long amount);
  48. }
  49.  
  50. class WalletAccount implements Wallet {
  51.  
  52.   Long balance = 0L;
  53.  
  54.   List<Long> coins = new ArrayList<>();
  55.  
  56.   @Transactional
  57.   public void addCoin(String coin) {
  58.     Long coinDenomintaion = Long.valueOf(coin);
  59.     this.coins.add(coinDenomintaion);
  60.     this.addBalance(coinDenomintaion);
  61.   }
  62.  
  63.   private void addBalance(Long coinDenomintaion) {
  64.     this.balance = (this.getBalance() + coinDenomintaion);
  65.   }
  66.  
  67.   @Override
  68.   public void load(String input) {
  69.     List<String> coins = List.of(input.split(","));
  70.     for (String coin : coins) {
  71.       this.addCoin(coin);
  72.     }
  73.   }
  74.  
  75.   @Override
  76.   public Long getBalance() {
  77.     return this.balance;
  78.   }
  79.  
  80.   public List<Long> peek() {
  81.  
  82.     return this.coins;
  83.   }
  84.  
  85.   @Override
  86.   public Long spend(Long amount) {
  87.     Long currentAmount = 0L;
  88.     /*
  89.      * 30
  90.      * [1, 100, 200, 200, 1000, 1]
  91.      * ^
  92.      * 101 --> return 101 --> Give us back 71
  93.      * coins = [200, 200, 1000, 1]
  94.      * add 71;
  95.      * [200, 200, 1000, 1, 71]
  96.      * */
  97.  
  98.     int i = 0;
  99.     for (i = 0; i < this.coins.size(); i++) {
  100.       Long currentCoin = this.coins.get(i);
  101.       currentAmount += currentCoin;
  102.       if (currentAmount >= amount) {
  103.         break;
  104.       }
  105.     }
  106.     this.balance -= currentAmount;
  107.     this.coins = this.coins.subList(i + 1, this.coins.size());
  108.     Long change = (currentAmount - amount);
  109.     if (change > 0) {
  110.       this.coins.add(change);
  111.       addBalance(change);
  112.     }
  113.  
  114.     return change;
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment