Exemple #1
0
 @Test(expected = NullPointerException.class)
 public void testValidateCardNullCard() throws NoCardInsertedException {
   System.out.println("validateNullCard");
   ATM atmTest = new ATM(100);
   int pinCode = 7777;
   atmTest.validateCard(null, pinCode);
 }
Exemple #2
0
 @Test(expected = UnsupportedOperationException.class)
 public void testInsertMoreThanOneCard() {
   System.out.println("insertMoreThanOneCard");
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   atmTest.insertCard(mockCard);
   atmTest.insertCard(mockCard);
 }
Exemple #3
0
 @Test
 public void testInsertOnlyOneCard() {
   System.out.println("insertOnlyOneCard");
   ATM atmTest = new ATM(0.0);
   Card mockCard = mock(Card.class);
   boolean result = atmTest.insertCard(mockCard);
   assertTrue(result);
 }
Exemple #4
0
 @Test(expected = NoCardInsertedException.class)
 public void testGetCardFromATMIfCardIsNotInserted()
     throws NotEnoughMoneyInATMException, NotEnoughMoneyInAccountException,
         NoCardInsertedException {
   System.out.println("getCardFromATMIfCardIsNotInserted");
   ATM atmTest = new ATM(1000.0);
   atmTest.getCardFromATM();
 }
Exemple #5
0
  public static void main(String[] args) {

    try (ATM atm = new ATM()) {
      atm.getMoney(1500);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemple #6
0
 @Test
 public void testGetMoneyInATM() {
   System.out.println("getMoneyInATM");
   ATM atmTest = new ATM(100.0);
   double expResult = 100.0;
   double result = atmTest.getMoneyInATM();
   assertEquals(expResult, result, 0.0);
 }
Exemple #7
0
 @Test(expected = NoCardInsertedException.class)
 public void testGetCardFromATMMoreThanOneTime() throws NoCardInsertedException {
   System.out.println("getCardFromATMMoreThanOneTime");
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   atmTest.insertCard(mockCard);
   atmTest.getCardFromATM();
   atmTest.getCardFromATM();
 }
Exemple #8
0
 @Test
 public void testValidateCardWithInvalidPin() throws NoCardInsertedException {
   System.out.println("validateCardWithInvalidPin");
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   int pinCode = 7777;
   when(mockCard.isBlocked()).thenReturn(false);
   when(mockCard.checkPin(pinCode)).thenReturn(false);
   assertFalse(atmTest.validateCard(mockCard, pinCode));
 }
Exemple #9
0
 @Test
 public void testCheckBalanceVerify() throws NoCardInsertedException {
   System.out.println("checkBalanceVerify");
   ATM atmTest = new ATM(1000);
   Card mockCard = mock(Card.class);
   Account mockAccount = mock(Account.class);
   when(mockCard.getAccount()).thenReturn(mockAccount);
   atmTest.insertCard(mockCard);
   atmTest.checkBalance();
   verify(mockCard, times(1)).isBlocked();
 }
Exemple #10
0
 @Test(expected = IllegalArgumentException.class)
 public void testGetCashLessThanZero()
     throws NotEnoughMoneyInATMException, NotEnoughMoneyInAccountException,
         NoCardInsertedException {
   System.out.println("getCashLessThanZero");
   double amount = -100.0;
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   atmTest.insertCard(mockCard);
   Account mockAccount = mock(Account.class);
   when(mockCard.getAccount()).thenReturn(mockAccount);
   atmTest.getCash(amount);
 }
Exemple #11
0
  public static void main(String[] args) {
    //        ATM atm = new ATM();
    //        try {
    //            atm.getMoney(1500);
    //        } catch (InsufficientMoney e) {
    //            e.printStackTrace();
    //        }

    try (ATM atm = new ATM()) {
      atm.getMoney(1500);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemple #12
0
 @Test
 public void testValidateValidCard() throws NoCardInsertedException {
   System.out.println("validateValidCard");
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   int pinCode = 7777;
   when(mockCard.isBlocked()).thenReturn(false);
   when(mockCard.checkPin(pinCode)).thenReturn(true);
   boolean expResult = true;
   boolean result = atmTest.validateCard(mockCard, pinCode);
   assertEquals(expResult, result);
   InOrder inOrder = inOrder(mockCard);
   inOrder.verify(mockCard).isBlocked();
   inOrder.verify(mockCard).checkPin(pinCode);
 }
Exemple #13
0
 @Test
 public void testCheckBalance() throws NoCardInsertedException {
   System.out.println("checkBalance");
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   Account mockAccount = mock(Account.class);
   atmTest.insertCard(mockCard);
   when(mockCard.getAccount()).thenReturn(mockAccount);
   when(mockAccount.getBalance()).thenReturn(0.0);
   double expResult = 0.0;
   double result = atmTest.checkBalance();
   assertEquals(expResult, result, 0.0);
   InOrder inOrder = inOrder(mockCard, mockAccount);
   inOrder.verify(mockCard).getAccount();
   inOrder.verify(mockAccount).getBalance();
 }
Exemple #14
0
 @Test(expected = NotEnoughMoneyInATMException.class)
 public void testGetCashNotEnoughMoneyInATM()
     throws NoCardInsertedException, NotEnoughMoneyInAccountException,
         NotEnoughMoneyInATMException {
   System.out.println("getCashNotEnoughMoneyInATM");
   ATM atmTest = new ATM(1000.0);
   Card mockCard = mock(Card.class);
   Account mockAccount = mock(Account.class);
   double amount = 1100.0;
   int pinCode = 7777;
   atmTest.insertCard(mockCard); // ver.2
   when(mockCard.getAccount()).thenReturn(mockAccount);
   when(mockCard.checkPin(pinCode)).thenReturn(true);
   atmTest.validateCard(mockCard, pinCode);
   atmTest.getCash(amount);
   InOrder inOrder = inOrder(mockCard, mockAccount);
   inOrder.verify(mockCard).getAccount();
   inOrder.verify(mockAccount).getBalance();
 }
Exemple #15
0
 @Test
 public void testGetCash()
     throws NotEnoughMoneyInAccountException, NotEnoughMoneyInATMException,
         NoCardInsertedException {
   System.out.println("getCashNotZeroBalance");
   double atmMoney = 1000.0;
   ATM atmTest = new ATM(atmMoney);
   Card mockCard = mock(Card.class);
   Account mockAccount = mock(Account.class);
   atmTest.insertCard(mockCard); // ver.2
   double balance = 600.0;
   double amount = 100.0;
   int pinCode = 7777;
   when(mockCard.getAccount()).thenReturn(mockAccount);
   when(mockCard.checkPin(pinCode)).thenReturn(true);
   when(mockCard.isBlocked()).thenReturn(false);
   when(mockAccount.getBalance()).thenReturn(balance);
   when(mockAccount.withdrow(amount)).thenReturn(amount);
   atmTest.validateCard(mockCard, pinCode);
   atmTest.getCash(amount);
   when(mockAccount.getBalance()).thenReturn(balance - amount);
   assertEquals(atmTest.getMoneyInATM(), atmMoney - amount, 0.0);
   assertEquals(atmTest.checkBalance(), balance - amount, 0.0);
   InOrder inOrder = inOrder(mockCard, mockAccount);
   inOrder.verify(mockCard).isBlocked();
   inOrder.verify(mockCard).checkPin(pinCode);
   inOrder.verify(mockCard, atLeastOnce()).getAccount();
   verify(mockAccount).withdrow(amount);
   inOrder.verify(mockAccount).getBalance();
 }
  /** The logic here is specific to our protocol. We parse the string according to that protocol. */
  private Float executeCommand(String commandLine) throws ATMException {
    // Break out the command line into String[]
    StringTokenizer tokenizer = new StringTokenizer(commandLine);
    String commandAndParam[] = new String[tokenizer.countTokens()];
    int index = 0;
    while (tokenizer.hasMoreTokens()) {
      commandAndParam[index++] = tokenizer.nextToken();
    }

    // store the command
    String command = commandAndParam[0];

    // Dispatch BALANCE request without further ado.
    if (command.equalsIgnoreCase(Commands.BALANCE.toString())) {
      return atmImplementation.getBalance();
    }

    // Must have 2nd arg for amount when processing DEPOSIT/WITHDRAW commands
    if (commandAndParam.length < 2) {
      throw new ATMException("Missing amount for command \"" + command + "\"");
    }

    // execute deposit or withdraw command
    try {
      float amount = Float.parseFloat(commandAndParam[1]);
      if (command.equalsIgnoreCase(Commands.DEPOSIT.toString())) {
        atmImplementation.deposit(amount);
      } else if (command.equalsIgnoreCase(Commands.WITHDRAW.toString())) {
        atmImplementation.withdraw(amount);
      } else {
        throw new ATMException("Unrecognized command: " + command);
      }
    } catch (NumberFormatException nfe) {
      throw new ATMException("Unable to make float from input: " + commandAndParam[1]);
    }
    // BALANCE command returned result above.  Other commands return null;
    return null;
  }
Exemple #17
0
  public static void main(String[] args) {
    Account account = new Account(1000000);

    ATM a1 = new ATM("강남", account);
    ATM a2 = new ATM("양재", account);
    ATM a3 = new ATM("종로", account);
    ATM a4 = new ATM("부산", account);
    ATM a5 = new ATM("광주", account);
    ATM a6 = new ATM("강릉", account);

    a1.start();
    a2.start();
    a3.start();
    a4.start();
    a5.start();
    a6.start();
  }