From ce835673ddb324b24c9d1b494d484ef53f81f857 Mon Sep 17 00:00:00 2001 From: JUN Date: Sat, 15 Apr 2023 21:09:53 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B2=80=EC=A6=9D=EB=B6=80=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=ED=99=94=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ClientRepository.java | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/firstSeminarAdvancedAssignment/src/main/java/sopt/org/firstSeminarAdvancedAssignment/repository/ClientRepository.java b/firstSeminarAdvancedAssignment/src/main/java/sopt/org/firstSeminarAdvancedAssignment/repository/ClientRepository.java index ba8f728..4da3798 100644 --- a/firstSeminarAdvancedAssignment/src/main/java/sopt/org/firstSeminarAdvancedAssignment/repository/ClientRepository.java +++ b/firstSeminarAdvancedAssignment/src/main/java/sopt/org/firstSeminarAdvancedAssignment/repository/ClientRepository.java @@ -10,47 +10,27 @@ public class ClientRepository { private static final Map clientDB = new LinkedHashMap<>(); - public void register(Client client) { - boolean isDuplicated = false; + public void register(Client registerClient) { + String accountNumber = registerClient.getAccountNumber(); + validateIsDuplicatedAccountNumber(accountNumber); - for (String accountNumber : clientDB.keySet()) { - if (client.getAccountNumber().equals(accountNumber)) { - isDuplicated = true; - break; - } - } - - if (isDuplicated) { - throw new IllegalArgumentException(ACCOUNT_NUMBER_DUPLICATED_ERROR_MESSAGE); - } - - clientDB.put(client.getAccountNumber(), client); + clientDB.put(accountNumber, registerClient); } public int getAccountBalance(String clientAccountNumber, String password) { - int clientMoneyAmount = -1; + validateIsExistAccountNumber(clientAccountNumber); - for (String accountNumber : clientDB.keySet()) { - if (accountNumber.equals(clientAccountNumber)) { - Client clientInfo = clientDB.get(clientAccountNumber); - - if (clientInfo.checkPassword(password)) { - clientMoneyAmount = clientInfo.getAmount(); - break; - } else { - throw new IllegalArgumentException(WRONG_PASSWORD_ERROR_MESSAGE); - } - } - } - if (clientMoneyAmount == -1) throw new IllegalArgumentException(WRONG_ACCOUNT_NUMBER_ERROR_MESSAGE); + Client client = clientDB.get(clientAccountNumber); + validateClientPassword(password, client); - return clientMoneyAmount; + int amount = client.getAmount(); + return amount; } public int withdrawAccountBalance(String accountNumber, int amount) { Client client = clientDB.get(accountNumber); - int remainAmount = client.withdraw(amount); + int remainAmount = client.withdraw(amount); return remainAmount; } @@ -58,11 +38,12 @@ public int deposit(String accountNumber, int depositAmount) { validateIsExistAccountNumber(accountNumber); Client client = clientDB.get(accountNumber); - int amountAfterDeposit = client.deposit(depositAmount); + int amountAfterDeposit = client.deposit(depositAmount); return amountAfterDeposit; } + private void validateIsExistAccountNumber(String clientAccountNumber) { boolean isAccountExist = false; for (String accountNumber : clientDB.keySet()) { @@ -71,9 +52,22 @@ private void validateIsExistAccountNumber(String clientAccountNumber) { break; } } - - if(!isAccountExist) { + if (!isAccountExist) { throw new IllegalArgumentException(WRONG_ACCOUNT_NUMBER_ERROR_MESSAGE); } } + + private void validateIsDuplicatedAccountNumber(String clientAccountNumber) { + for (String accountNumber : clientDB.keySet()) { + if (accountNumber.equals(clientAccountNumber)) { + throw new IllegalArgumentException(ACCOUNT_NUMBER_DUPLICATED_ERROR_MESSAGE); + } + } + } + + private void validateClientPassword(String password, Client client) { + if (!client.checkPassword(password)) { + throw new IllegalArgumentException(WRONG_PASSWORD_ERROR_MESSAGE); + } + } }