본문 바로가기
개발일지

개발일지 19일차 -은행 구현 프로젝트-

by 태운콩즙 2023. 12. 20.
728x90
반응형

오늘은 은행구현 프로젝트를 하였다

 

평소와 다른점은 DTO를 두개를 사용하였다

하나는 계좌와 고객 관리를 위한 clientDTO 또 하나는 입출금 거래내역을 관리해주는 AccountDTO이다

 

< clientDTO>

package ch11_classes.ex5;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ClientDTO {
    Long id;
    String clientName;
    String accountNumber;
    int clientPass;
    String clientCreatedAt;
    long balance;

    public void setId(Long id) {
        this.id = id;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public void setClientPass(int clientPass) {
        this.clientPass = clientPass;
    }

    public void setClientCreatedAt(String clientCreatedAt) {
        this.clientCreatedAt = clientCreatedAt;
    }

    public void setBalance(long balance) {
        this.balance = balance;
    }

    public Long getId() {
        return id;
    }

    public String getClientName() {
        return clientName;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public int getClientPass() {
        return clientPass;
    }

    public String getClientCreatedAt() {
        return clientCreatedAt;
    }

    public long getBalance() {
        return balance;
    }
    public ClientDTO(){

    }
    Long idValue = 1L;
    public ClientDTO(String clientName , String accountNumber, int clientPass , long balance){
        this.id = idValue++;
        this.clientName = clientName;
        this.accountNumber = accountNumber;
        this.clientPass = clientPass;
        this.clientCreatedAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "ClientDTO{" +
                "id=" + id +
                ", clientName='" + clientName + '\'' +
                ", accountNumber='" + accountNumber + '\'' +
                ", clientPass=" + clientPass +
                ", clientCreatedAt='" + clientCreatedAt + '\'' +
                ", balance=" + balance +
                '}';
    }
}

 

< AccountDTO>

package ch11_classes.ex5;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class AccountDTO {
    private Long id;
    private String accountNumber;
    private long deposit;
    private long withdraw;
    private String bankingAt;

    public void setId(Long id) {
        this.id = id;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public void setDeposit(long deposit) {
        this.deposit = deposit;
    }

    public void setWithdraw(long withdraw) {
        this.withdraw = withdraw;
    }

    public void setBankingAt(String bankingAt) {
        this.bankingAt = bankingAt;
    }

    public Long getId() {
        return id;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public long getDeposit() {
        return deposit;
    }

    public long getWithdraw() {
        return withdraw;
    }

    public String getBankingAt() {
        return bankingAt;
    }
    public AccountDTO(){

    }
    Long  idValue = 1L;
    public AccountDTO(String accountNumber, long deposit , long withdraw){
        this.id = idValue++;
        this.accountNumber = accountNumber;
        this.deposit = deposit;
        this.withdraw = withdraw;
        this.bankingAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}

 

<Main>

package ch11_classes.ex5;

import java.util.Scanner;

public class BankMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BankService bankService = new BankService();
        int selecNo = 0;
        boolean run = true;
        while (run){
            System.out.println("1고객 등록 | 2.잔액 조회 | 3.입금 | 4.출금 | 5.거래 내역 확인 | 6.계좌 이체 | 0.종료");
            selecNo = scanner.nextInt();
            if (selecNo == 1){
                bankService.ClientRegistration();
            } else if (selecNo == 2) {
                bankService.balanceCheck();
            } else if (selecNo == 3) {
                bankService.deposit();
            } else if (selecNo == 4) {
                bankService.withdraw();
            } else if (selecNo == 5) {
                bankService.bankingList();
            } else if (selecNo == 6) {
                bankService.accountTransfer();
            } else if (selecNo == 0) {
                break;
            }
        }
    }
}

 

<Service>

package ch11_classes.ex5;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BankService {
    BankRepository bankRepository = new BankRepository();
    Scanner scanner = new Scanner(System.in);

    public void ClientRegistration() {
        boolean checkResult = false;
        String accountNumber = null;
        do {
            System.out.println("계좌번호: ");
            accountNumber = scanner.next();
            checkResult = bankRepository.accountCheck(accountNumber);
            if (checkResult) {
                System.out.println("사용 가능한 계좌번호 입니다");
            } else {
                System.out.println("이미 사용중인 계좌번호 입니다");
            }
        } while (!checkResult);
        System.out.print("비밀번호: ");
        int clientPass = scanner.nextInt();
        System.out.print("성명: ");
        String clientName = scanner.next();
        long balance = 0;
        ClientDTO clientDTO = new ClientDTO(clientName, accountNumber, clientPass, balance);
        boolean result = bankRepository.ClientRegistration(clientDTO);
        if (result) {
            System.out.println("등록 되었습니다");
        } else {
            System.out.println("등록 실패 하였습니다");
        }
    }

    public void balanceCheck() {
        System.out.println("조회하실 계좌번호를 입력하세요: ");
        String accountNumber = scanner.next();
        ClientDTO clientDTO = bankRepository.balanceCheck(accountNumber);
        if (clientDTO != null) {
            System.out.println("계좌 잔액은" + clientDTO.balance + "입니다");
        } else {
            System.out.println("요청하신 정보를 찾을수 없습니다");
        }


    }

    public void deposit() {
        System.out.println("입금하실 계좌번호를 입력하세요: ");
        String accountNumber = scanner.next();
        ClientDTO clientDTO = bankRepository.balanceCheck(accountNumber);
        if (clientDTO != null) {
            System.out.println("입금하실 금액: ");
            long balance = scanner.nextLong();
            boolean depositResult = bankRepository.deposit(accountNumber, balance);
            if (depositResult) {
                System.out.println("입금되었습니다");
            } else {
                System.out.println("입금 실패하였습니다");
            }
        } else {
            System.out.println("계좌번호를를 다시 확인해 주세요");
        }
    }

    public void withdraw() {
        System.out.println("출금하실 계좌번호를 입력하세요: ");
        String accountNumber = scanner.next();
        ClientDTO clientDTO = bankRepository.balanceCheck(accountNumber);
        if (clientDTO != null) {
            System.out.println("출금하실 금액: ");
            long balance = scanner.nextLong();
            System.out.println("비밀번호");
            int clientPass = scanner.nextInt();
            boolean depositResult = bankRepository.withdraw(accountNumber, clientPass, balance);
            if (depositResult) {
                System.out.println("출금되었습니다");
            } else {
                System.out.println("출금 실패입니다");
            }
        } else {
            System.out.println("계좌번호를를 다시 확인해 주세요");
        }


    }

    public void bankingList() {
        List<AccountDTO> accountDTOList = bankRepository.bankingList();
        boolean run = true;
        int sel = 0;
        while (run) {
            System.out.println("1.모든 내용 | 2.입금 내역 | 3.출금 내역 | 4.종료");
            sel = scanner.nextInt();
            if (sel == 1) {
                listPrint(accountDTOList);

            } else if (sel == 2) {
                depositList(accountDTOList);

            } else if (sel == 3) {
                withdrawList(accountDTOList);
            } else if (sel == 4) {
                break;
            }
        }

    }

    private void listPrint(List<AccountDTO> accountDTOList) {
        System.out.println("계좌번호\t" + "입금\t" + "출금\t " + "시간\t");
        for (AccountDTO accountDTO : accountDTOList) {
            System.out.println(accountDTO.getAccountNumber() + "\t" + accountDTO.getDeposit() + "\t" + accountDTO.getWithdraw() + "\t" + accountDTO.getBankingAt() + "\t");
        }
    }

    private void depositList(List<AccountDTO> accountDTOList) {
        System.out.println("계좌번호\t" + "입금\t" + "시간\t");
        for (AccountDTO accountDTO : accountDTOList) {
            System.out.println(accountDTO.getAccountNumber() + "\t" + accountDTO.getDeposit() + "\t" + accountDTO.getBankingAt() + "\t");
        }
    }

    private void withdrawList(List<AccountDTO> accountDTOList) {
        System.out.println("계좌번호\t" + "출금\t " + "시간\t");
        for (AccountDTO accountDTO : accountDTOList) {
            System.out.println(accountDTO.getAccountNumber() + "\t" + accountDTO.getWithdraw() + "\t" + accountDTO.getBankingAt() + "\t");
        }
    }

    public void accountTransfer() {
        System.out.println("이체하실 계좌번호를 입력하세요: ");
        String accountNumber = scanner.next();
      ClientDTO clientDTO = bankRepository.balanceCheck(accountNumber);
        if (clientDTO != null) {
            System.out.println("이체하실 금액을 입력하세요");
            long balance = scanner.nextLong();
             bankRepository.accountTransfer(accountNumber, balance);
            System.out.println("이체받을 계좌번호를 입력하세요: ");
            String accountNumber1 = scanner.next();
            System.out.println("비밀번호를 입력하세요: ");
            int clientPass = scanner.nextInt();
            bankRepository.accountTransfer1(accountNumber1, balance, clientPass);
                System.out.println("이체 하였습니다");
            }
        }

    }

 

서비스에서는 계좌등록 및 중복확인으로  clientDTO 의 필드값을 채워주었고

거래내역 확인으로 입금내역 출금내역을 확인할수있다

 

<Repository>

package ch11_classes.ex5;

import java.util.ArrayList;
import java.util.List;

public class BankRepository {
    List<ClientDTO> clientDTOList = new ArrayList<>();
    List<AccountDTO> accountDTOList = new ArrayList<>();

    public boolean accountCheck(String accountNumber) {
        boolean result = true;
        for (int i = 0; i < clientDTOList.size(); i++) {
            if (accountNumber.equals(clientDTOList.get(i).getAccountNumber())) {
                result = false;
            }
        }
        return result;
    }

    public boolean ClientRegistration(ClientDTO clientDTO) {
        return clientDTOList.add(clientDTO);
    }

    public ClientDTO balanceCheck(String accountNumber) {
        ClientDTO clientDTO = null;
        for (int i = 0; i < clientDTOList.size(); i++) {
            if (accountNumber.equals(clientDTOList.get(i).getAccountNumber())) {
                clientDTO = clientDTOList.get(i);
            }
        }
        return clientDTO;
    }

    public boolean deposit(String accountNumber, long balance) {
        boolean result = false;
        for (int i = 0; i < clientDTOList.size(); i++) {
            if (accountNumber.equals(clientDTOList.get(i).getAccountNumber())) {
                long a = clientDTOList.get(i).getBalance();
                a = a + balance;
                clientDTOList.get(i).setBalance(a);
                result = true;
                AccountDTO accountDTO = new AccountDTO(accountNumber, balance, 0);
                accountDTOList.add(accountDTO);

            }
        }
        return result;
    }

    public boolean withdraw(String accountNumber, int clientPass, long balance) {
        boolean result = false;
        for (int i = 0; i < clientDTOList.size(); i++) {
            if (clientPass == clientDTOList.get(i).getClientPass())
                if (accountNumber.equals(clientDTOList.get(i).getAccountNumber())) {
                    long a = clientDTOList.get(i).getBalance();
                    if (a >= balance) {
                        a = a - balance;
                        clientDTOList.get(i).setBalance(a);
                        result = true;
                        AccountDTO accountDTO = new AccountDTO(accountNumber, 0, balance);
                        accountDTOList.add(accountDTO);
                    }

                }
        }
        return result;
    }

    public List<AccountDTO> bankingList() {

        return accountDTOList;
    }

    public boolean accountTransfer(String accountNumber, long balance) {
        boolean result = false;
        for (int i = 0; i < clientDTOList.size(); i++) {
            if (accountNumber.equals(clientDTOList.get(i).getAccountNumber())) {
                long a = clientDTOList.get(i).getBalance();
                if (a >= balance) {
                    a = a - balance;
                    clientDTOList.get(i).setBalance(a);
                    result = true;
                    AccountDTO accountDTO = new AccountDTO(accountNumber, 0, balance);
                    accountDTOList.add(accountDTO);
                }
            }
        }
        return result;
    }
    public boolean accountTransfer1(String accountNumber1 , long balance , int clientPass){
        boolean result = false;
        for (int i = 0; i < clientDTOList.size(); i++) {
            for (int j = 0; j < clientDTOList.size(); j++){
            if (clientPass == clientDTOList.get(i).getClientPass())
                if (accountNumber1.equals(clientDTOList.get(j).getAccountNumber())) {
                    long a = clientDTOList.get(j).getBalance();
                        a = a + balance;
                        clientDTOList.get(j).setBalance(a);
                        result = true;
                        AccountDTO accountDTO = new AccountDTO(accountNumber1, 0, balance);
                        accountDTOList.add(accountDTO);

            }
                    }

                }
        return result;
        }
    }

리파지토리에서 입금 출금을 담당하는 메서드에서 accountDTO의 필드값을 채워주고

계좌이체 메서드는 입금과 출금 그리고 계좌1과 계좌2를 확인하는과정이 들어있다

 

계좌이체를 실행해주기 위해서는 1번 계좌에서  계좌확인과 이체할 금액을 입력받고 1번계좌의 잔액에서 출금이 진행되게 만들어주었고

두번째 메서드에서 2번계좌에 입금 비밀번호 입력을 받은후 2번계좌에 잔액에 입금처리를 하고 accountDTO에 정보를 넘겨주었다

 

728x90
반응형