Advertisement
LeatherDeer

Untitled

May 6th, 2023
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. class ATM {
  2.     int[] banknotesCount;
  3.     private final int[] coin;
  4.  
  5.     public ATM() {
  6.         banknotesCount = new int[5];
  7.         coin = new int[] {20, 50, 100, 200, 500};
  8.     }
  9.    
  10.     public void deposit(int[] banknotes) {
  11.         for (int i = 0; i < 5; i++) {
  12.             this.banknotesCount[i] += banknotes[i];
  13.         }
  14.     }
  15.    
  16.     public int[] withdraw(int amount) {
  17.         int[] withdrawCount = new int[5];
  18.         for (int i = banknotesCount.length - 1; i >= 0; i--) {
  19.                 int count = (int) Math.min(banknotesCount[i], amount / coin[i]);
  20.                 amount -= coin[i] * count;
  21.                 withdrawCount[i] = count;
  22.            
  23.         }
  24.  
  25.         if (amount != 0) {
  26.             return new int[] {-1};    
  27.         } else {
  28.             for (int i = 0; i < 5; i++) {
  29.                 banknotesCount[i] -= withdrawCount[i];
  30.             }
  31.  
  32.             return withdrawCount;
  33.         }
  34.     }
  35.  
  36.     // 700 = 500 * 1
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement