diff --git a/Assignment1/src/main/java/Controller/BankController.java b/Assignment1/src/main/java/Controller/BankController.java index e24d09f..f7d514d 100644 --- a/Assignment1/src/main/java/Controller/BankController.java +++ b/Assignment1/src/main/java/Controller/BankController.java @@ -1,2 +1,89 @@ -package Controller;public class BankController { +package Controller; + +import Service.BankService; +import Service.BankServiceImpl; + +import java.util.Scanner; + +import static java.lang.System.exit; + +public class BankController { + public void entranceBank(){ + BankService bankService = new BankServiceImpl(); + Scanner sc = new Scanner(System.in); + while(true){ + System.out.println(); + System.out.println("원하시는 업무를 입력해주세요."); + System.out.println("[은행가입] [예적금] [잔액조회] [전체계좌조회] [나가기]"); + String userInput = sc.nextLine().trim(); + MainAction action = MainAction.findByAction(userInput); + if (action == null) { + System.out.println("!!!올바른 업무를 입력해주세요!!!"); + continue; + } + switch (action) { + case JOIN_BANK ->{ + System.out.print("이름을 입력해주세요 : "); + String name = sc.nextLine();; + System.out.print("주민번호 앞자리를 입력해주세요 : "); + String birth = sc.nextLine(); + bankService.join(name, birth); + } + case SAVINGS -> { + System.out.println("원하시는 업무를 입력해주세요."); + System.out.println("[계좌이체] [입금] [출금]"); + userInput = sc.nextLine(); + SavingAction savingAction = SavingAction.findByAction(userInput); + if (savingAction == null) { + System.out.println("!!!올바른 업무를 입력해주세요!!!"); + continue; + } + switch (savingAction){ + case TRANSFER->{ + //todo: 계좌이체 서비스 + System.out.print("고객님의 이름을 입력해주세요: "); + String name = sc.nextLine(); + System.out.print("고객님의 생년월일을 입력해주세요: "); + String birth = sc.nextLine(); + if(bankService.checkMember(name, birth)){ + System.out.print("송금할 계좌번호를 입력하세요: "); + int account = Integer.parseInt(sc.nextLine()); + System.out.print("송금할 금액을 입력하세요: "); + int money = Integer.parseInt(sc.nextLine()); + bankService.transfer(name, birth, money, account); + } + } + case DEPOSIT ->{ + System.out.print("입금할 계좌번호를 입력하세요: "); + int account = Integer.parseInt(sc.nextLine()); + System.out.print("입금할 금액을 입력하세요: "); + int money = Integer.parseInt(sc.nextLine()); + bankService.deposit(money, account); + } + case WITHDRAW ->{ + System.out.print("출금할 계좌번호를 입력하세요: "); + int account = Integer.parseInt(sc.nextLine()); + System.out.print("출금할 금액을 입력하세요: "); + int money = Integer.parseInt(sc.nextLine()); + bankService.withdraw(money, account); + } + } + } + case SHOW_ALL_ACCOUNTS -> { + bankService.showAll(); + } + case BALANCE -> { + System.out.print("고객님의 이름을 입력해주세요: "); + String name = sc.nextLine(); + System.out.print("고객님의 생년월일을 입력해주세요: "); + String birth = sc.nextLine(); + bankService.showBalance(name, birth); + } + case EXIT ->{ + System.out.println("조심히 가십쇼."); + exit(0); + } + } + } + } } diff --git a/Assignment1/src/main/java/Controller/MainAction.java b/Assignment1/src/main/java/Controller/MainAction.java index 7df1eda..f9a9042 100644 --- a/Assignment1/src/main/java/Controller/MainAction.java +++ b/Assignment1/src/main/java/Controller/MainAction.java @@ -4,6 +4,7 @@ public enum MainAction { JOIN_BANK("은행가입"), SAVINGS("예적금"), SHOW_ALL_ACCOUNTS("전체계좌조회"), + BALANCE("잔액조회"), EXIT("나가기"); private final String action; @@ -11,11 +12,9 @@ public enum MainAction { MainAction(String action) { this.action = action; } - public String getAction() { return action; } - public static MainAction findByAction(String action) { for (MainAction value : values()) { if (value.getAction().equalsIgnoreCase(action)) { diff --git a/Assignment1/src/main/java/Controller/SavingAction.java b/Assignment1/src/main/java/Controller/SavingAction.java index 486d5d9..010af45 100644 --- a/Assignment1/src/main/java/Controller/SavingAction.java +++ b/Assignment1/src/main/java/Controller/SavingAction.java @@ -1,2 +1,23 @@ -package Controller;public enum SavingAction { +package Controller; + +public enum SavingAction { + TRANSFER("계좌이체"), + DEPOSIT("입금"), + WITHDRAW("출금"); + private final String action; + + SavingAction(String action) { + this.action = action; + } + public String getAction() { + return action; + } + public static SavingAction findByAction(String action) { + for (SavingAction value : values()) { + if (value.getAction().equalsIgnoreCase(action)) { + return value; + } + } + return null; + } } diff --git a/Assignment1/src/main/java/Data/Member.java b/Assignment1/src/main/java/Data/Member.java index 7f98532..69cb77f 100644 --- a/Assignment1/src/main/java/Data/Member.java +++ b/Assignment1/src/main/java/Data/Member.java @@ -1,2 +1,75 @@ -package Data;public class Member { +package Data; + +import java.util.Date; + +public class Member { + private Long id; + private String name; + private Date enterDate; + private String birth; + private int account; + private int balance; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBirth() { + return birth; + } + + public void setBirth(String birth) { + this.birth = birth; + } + + public Date getEnterDate() { + return enterDate; + } + + public void setEnterDate(Date enterDate) { + this.enterDate = enterDate; + } + + public int getAccount() { + return account; + } + + public void setAccount(int account) { + this.account = account; + } + + public int getBalance() { + return balance; + } + + public void setBalance(int balance) { + this.balance = balance; + } + + @Override + public String toString() { + return "[고객ID] " + id + + " [이름] " + name + + " [생년월일] " + birth + + " [가입날짜] " + enterDate + + " [계좌번호] " + account + + " [잔액] " + balance +"원"; + } + + public Member(String name, String birth){ + this.name = name; + this.birth = birth; + } } diff --git a/Assignment1/src/main/java/Main.java b/Assignment1/src/main/java/Main.java index fb7039d..9b37497 100644 --- a/Assignment1/src/main/java/Main.java +++ b/Assignment1/src/main/java/Main.java @@ -1,2 +1,8 @@ -package PACKAGE_NAME;public class Main { +import Controller.BankController; + +public class Main { + public static void main(String[] args) { + BankController bankController = new BankController(); + bankController.entranceBank(); + } } diff --git a/Assignment1/src/main/java/Repository/BankRepository.java b/Assignment1/src/main/java/Repository/BankRepository.java index 928ea63..e99b905 100644 --- a/Assignment1/src/main/java/Repository/BankRepository.java +++ b/Assignment1/src/main/java/Repository/BankRepository.java @@ -1,2 +1,17 @@ -package Repository;public interface BankRepository { +package Repository; + +import Data.Member; + +import java.util.List; +import java.util.Optional; + +public interface BankRepository { + Member join(Member member); + List findAll(); + + Optional findByAccount(int account); + Optional findMember(String name, String birth); + int deposit(Member member, int money); + + void withdraw(Member member, int money); } diff --git a/Assignment1/src/main/java/Repository/MemoryBankRepository.java b/Assignment1/src/main/java/Repository/MemoryBankRepository.java index df0e096..fc2c77a 100644 --- a/Assignment1/src/main/java/Repository/MemoryBankRepository.java +++ b/Assignment1/src/main/java/Repository/MemoryBankRepository.java @@ -1,17 +1,48 @@ +package Repository; + import Data.Member; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.*; -public class MemoryBankRepository { - private static HashMap bank = new HashMap<>(); +public class MemoryBankRepository implements BankRepository{ + private static HashMap bank = new HashMap<>(); private static Long id = 0L; - public void join(Member member) { + + @Override + public Member join(Member member) { member.setId(id++); - bank.put(member.getId(), member); + member.setEnterDate(new Date()); + member.setAccount((int)(Math.random()*10000000)); + bank.put(member.getAccount(), member); + return member; } + public List findAll(){ return new ArrayList<>(bank.values()); } + + @Override + public Optional findByAccount(int account) { + return Optional.ofNullable(bank.get(account)); + } + + + @Override + public Optional findMember(String name, String birth) { + Optional member = bank.values().stream() + .filter(m -> m.getName().equals(name) && m.getBirth().equals(birth)) + .findAny(); + return member; + } + + @Override + public int deposit(Member member, int money) { + member.setBalance(member.getBalance()+money); + return member.getBalance(); + } + + @Override + public void withdraw(Member member, int money) { + member.setBalance(member.getBalance()-money); + } } diff --git a/Assignment1/src/main/java/Service/BankService.java b/Assignment1/src/main/java/Service/BankService.java index e1aca5e..2d9f058 100644 --- a/Assignment1/src/main/java/Service/BankService.java +++ b/Assignment1/src/main/java/Service/BankService.java @@ -1,2 +1,17 @@ -package Service;public interface BankService { +package Service; + +import Data.Member; + +import java.util.List; + +public interface BankService { + void join(String name, String birth); + void deposit(int money, int account); + void withdraw(int money, int account); + void showAll(); + void showBalance(String name, String birth); + + boolean checkMember(String name, String birth); + + void transfer(String name, String birth, int money, int account); } diff --git a/Assignment1/src/main/java/Service/BankServiceImpl.java b/Assignment1/src/main/java/Service/BankServiceImpl.java index cad0725..27e9d95 100644 --- a/Assignment1/src/main/java/Service/BankServiceImpl.java +++ b/Assignment1/src/main/java/Service/BankServiceImpl.java @@ -1,2 +1,100 @@ -package Service;public class BankServiceImpl { +package Service; + +import Data.Member; +import Repository.MemoryBankRepository; + +import java.util.List; +import java.util.Optional; +import java.util.TreeMap; + +public class BankServiceImpl implements BankService{ + MemoryBankRepository memoryBankRepository = new MemoryBankRepository(); + @Override + public void join(String name, String birth) { + Member member = new Member(name, birth.trim()); + Optional already = memoryBankRepository.findMember(name, birth); + if (already.isEmpty()) { + memoryBankRepository.join(member); + System.out.println("성공적으로 가입되었습니다!"); + System.out.println("가입정보: "+ member); + } + else{ + System.out.println("이미 가입되어 있는 고객입니다."); + } + } + + @Override + public void deposit(int money, int account){ + Optional member = memoryBankRepository.findByAccount(account); + if (memoryBankRepository.findByAccount(account).isEmpty()) { + System.out.println("존재하지 않는 계좌번호입니다."); + }else { + int afterBalance = memoryBankRepository.deposit(member.get(), money); + System.out.println(afterBalance+"원이 성공적으로 입금되었습니다. 잔액: "+member.get().getBalance()+"원"); + } + } + + @Override + public void withdraw(int money, int account) { + Optional member = memoryBankRepository.findByAccount(account); + if (memoryBankRepository.findByAccount(account).isEmpty()) { + System.out.println("존재하지 않는 계좌번호입니다."); + }else { + if (money > member.get().getBalance()){ + System.out.println("잔액 이상의 금액은 출금할 수 없습니다. 잔액: "+member.get().getBalance()+"원"); + }else { + memoryBankRepository.withdraw(member.get(), money); + System.out.println(money+"원이 성공적으로 출금되었습니다. 잔액: "+member.get().getBalance()+"원"); + } + } + } + + @Override + public void showAll() { + List members = memoryBankRepository.findAll(); + if ((long) members.size() == 0) { + System.out.println("아직 조회할 계좌 정보가 없습니다."); + } + for (Member member : members) { + System.out.println(member); + } + } + + @Override + public void showBalance(String name, String birth) { + Optional member = memoryBankRepository.findMember(name, birth.trim()); + if (member.isEmpty()) { + System.out.println("계좌 정보가 존재하지 않습니다."); + }else{ + System.out.println(member.get().getName()+"님의 잔액은 "+member.get().getBalance()+"원 입니다."); + } + } + + @Override + public boolean checkMember(String name, String birth) { + Optional member = memoryBankRepository.findMember(name, birth); + if (member.isEmpty()) { + System.out.println("고객 정보가 일치하지 않습니다."); + return false; + }else{ + return true; + } + } + + @Override + public void transfer(String name, String birth, int money, int account) { + Optional toMember = memoryBankRepository.findByAccount(account); + if (toMember.isEmpty()) { + System.out.println("존재하지 않는 계좌번호입니다."); + }else { + Optional fromMember = memoryBankRepository.findMember(name, birth); + if (money > fromMember.get().getBalance()){ + System.out.println("잔액이 부족합니다. 잔액: "+fromMember.get().getBalance()+"원"); + }else{ + toMember.get().setBalance(toMember.get().getBalance()+money); + fromMember.get().setBalance(fromMember.get().getBalance()-money); + System.out.println("성공적으로 이체되었습니다."); + } + } + } } diff --git a/Assignment1/src/test/java/MemberTest.java b/Assignment1/src/test/java/MemberTest.java index 5ed4f2d..2984803 100644 --- a/Assignment1/src/test/java/MemberTest.java +++ b/Assignment1/src/test/java/MemberTest.java @@ -1,2 +1,17 @@ -package PACKAGE_NAME;public class MemberTest { +import Data.Member; +import org.junit.jupiter.api.Test; + +import java.util.Date; + +class MemberTest { +// @Test +// void 멤버빌더생성(){ +// Member member1 = new Member.MemberBuilder() +// .id(1L) +// .name("member1") +// .birth("2000-10-20") +// .enterDate(new Date()) +// .build(); +// System.out.println("member1 = " + member1); +// } } diff --git a/Assignment1/src/test/java/MemoryBankRepositoryTest.java b/Assignment1/src/test/java/MemoryBankRepositoryTest.java index aeb199a..aa80429 100644 --- a/Assignment1/src/test/java/MemoryBankRepositoryTest.java +++ b/Assignment1/src/test/java/MemoryBankRepositoryTest.java @@ -1,2 +1,12 @@ -package PACKAGE_NAME;public class BankTest { +import org.junit.jupiter.api.Test; + +class MemoryBankRepositoryTest { + @Test + void 고객조회(){ + + } + @Test + void 고객전체조회(){ + + } }