개발자 미니민의 개발스터디

[미니 프로젝트] 은행 입출금 시스템 구현

by mini_min

♪ DB , 오라클 배우기 전 작성했던 미니 프로젝트 입니다.

- TransactionVO / AccountVO

// getter setter 별도로 생성해줌!

	String accountNo;
	String name;
	String launch_date;
	String pwd;
	String birth;
	int balance;
    
    
    String accountNo;
	String transaction_date;
	String kind;
	int amount;
	int balance=0;

 

 

- AccountImpl

public class AccountImpl implements Account {
	private List<AccountVO> list = new ArrayList<>();
	private List<TransactionVO> tlist = new ArrayList<>();
	
	
///////////////////////////
	
	
	String m =  "020-08-0000001";
	String a;
	int n;

	//통장 개설하기
	@Override
	public void addAccount(AccountVO vo) {
		
		
		//이게 날짜 생성날짜 (계좌생성일 s)
		String s;
		Calendar cal = Calendar.getInstance();
		s = String.format("%tF(%tA)", cal, cal);
		vo.setLaunch_date(s);

	
		String s1 = m.substring(0, m.lastIndexOf("-"));
		String s2 = m.substring(m.lastIndexOf("-")+1); //0000001

		n = Integer.parseInt(s2)+1;
		m = s1+"-"+String.format("%07d", n);
		
		vo.setAccountNo(m);
		vo.setBalance(0);
		
		
		list.add(vo);
		
	}

	
	//입금
	@Override
	public void deposit(String accountNo, long amount) {
		
		TransactionVO to = new TransactionVO();
		for(AccountVO vo : list) {	//통장정보 list 확인
			
			if(! vo.getAccountNo().equals(accountNo)) {
				System.out.println("입금하실 통장이 없습니다.");
				return;
			}
			
				System.out.println();
			
			if(amount <0) {
					System.out.println("금액이 0보다 적습니다.");
					return;
			}
				
			to.setAccountNo(accountNo);
			to.setKind("입금");
			to.setAmount((int)amount);
				
	
			String s;
			Calendar cal = Calendar.getInstance();
			s = String.format("%tF(%tA)", cal, cal);
			to.setTransaction_date(s);
			
			
			TransactionVO last = lastbalance(accountNo);
			int balance = (int)(last.getBalance() + amount);
			to.setBalance(balance);
		
				
			tlist.add(to);
				
			}
	}
	
	
	//출금
	@Override
	public void withdraw(String accountNo, String pwd, long amount) {
		
		TransactionVO to = new TransactionVO();
		for(AccountVO vo : list) {	//통장정보 list 확인
			
			if(!vo.getAccountNo().equals(accountNo) || !vo.getPwd().equals(pwd)) {
				System.out.println("출금하실 통장이 없거나 패스워드 오류입니다.");
				return;
				}
			
			if(amount <0) {
				System.out.println("금액이 0보다 적습니다.");
				return;
			}
			
			TransactionVO last = lastbalance(accountNo);
			if(last.getBalance()<amount) {
				System.out.println("출금 가능액을 초과했습니다.");
				return;
			}
			int balance = (int)(last.getBalance() - amount);
			to.setBalance(balance);
			
				to.setAccountNo(accountNo);
				to.setKind("출금");
				to.setAmount((int)amount);

				String s;
				Calendar cal = Calendar.getInstance();
				s = String.format("%tF(%tA)", cal, cal);
				to.setTransaction_date(s);
				
	
				tlist.add(to);
				
			}
	}
	
	//통장조회(이름과 생년월일로 조회)
	
	public List<AccountVO> listAccount(String name, String birth) {
		List<AccountVO> finds = new ArrayList<>();
		for(AccountVO vo : list) {
			if(vo.getName().indexOf(name) != -1 && vo.getBirth().equals(birth)) {
				finds.add(vo);
			}
		}
		return finds;
	}

	
	
	//거래내역조회
	@Override
	public List<TransactionVO> transactionHistory(String accountNo) {
		List<TransactionVO> finds = new ArrayList<>();
		for(TransactionVO to : tlist) {
			if(to.getAccountNo().equals(accountNo)) {
				finds.add(to);
			}
		}
		return finds;
	}


	
	//통장 삭제하기
	@Override
	public boolean deleteAccount(String accountNo, String pwd) {
		AccountVO vo = readAccount(accountNo, pwd);

		if(vo==null) {
			return false;
		}
		list.remove(vo);
		return true;
	}


	
	//통장, 비번 확인해서 출력하기
	@Override
	public AccountVO readAccount(String accountNo, String pwd) {
		for(AccountVO vo : list) {
			if(vo.getAccountNo().equals(accountNo) && vo.getPwd().equals(pwd)) {
				return vo;
			}
		}
		return null;
	}

	
	//마지막 거래내역 출력하기
	@Override
	public TransactionVO lastbalance(String accountNo) {
		List<TransactionVO> finds = transactionHistory(accountNo);
		
		if(finds!=null) {
			if(finds.size()!=0) {
				return finds.get(finds.size()-1);
			}
		}
		TransactionVO temp = new TransactionVO();
		return temp;
	}

}

 

 

- AccountUI

public class AccountUI {
	private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	private Account account = new AccountImpl();
	private DateUtil util = new DateUtil();
	
	public void menu() {
		int ch;
		
		while(true) {
			try {
				System.out.println("\n\t       >> SS은행 ATM <<");
				System.out.println("----------------------------------------------");
				System.out.println("[1] 계좌개설 [2] 입금 [3] 출금 [4] 계좌조회 [5] 거래내역");
				System.out.println("[6] 계좌삭제 [7] 패스워드 변경 [8] 종료");
				System.out.println("----------------------------------------------");
				System.out.print("선택 => ");
				ch = Integer.parseInt(br.readLine());
				
				if(ch==8) {
					System.out.println("ATM 이용을 종료합니다.");
					return;
				}
				
				switch(ch) {
				case 1: insert(); break;
				case 2: plus(); break;
				case 3: minus(); break;
				case 4: list(); break;
				case 5: actionlist(); break;
				case 6: delete(); break;
				case 7: pwdchange(); break;
				}
				
			} catch (Exception e) {
				
			}}}

	
	
	private void insert() {
		System.out.println("\n[신규계좌 등록]");
		
		AccountVO vo = new AccountVO();
		
		try {
			System.out.print("고객 성함: ");
			vo.setName(br.readLine());
			
			System.out.print("생년월일[0000-00-00 형태]: ");
			vo.setBirth(br.readLine());
			if(! util.isValidDate(vo.getBirth())) {
				System.out.println("등록 실패. 날짜 형식 입력 오류입니다.");
				return;
			}
			
			System.out.print("패스워드[4자리]: ");
			vo.setPwd(br.readLine());
			
			account.addAccount(vo);
			System.out.println("등록 완료되었습니다.");
			
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("등록 실패. 은행에 문의하세요.");
		}
		System.out.println();
	}	
	
	
	
	private void list() {
		System.out.println("\n[계좌 조회]");
		
		String name;
		String birth;
		
		try {
			System.out.print("계좌 조회할 고객의 성함은? :  ");
			name = br.readLine();
			
			System.out.print("고객님의 생년월일은? :  ");
			birth = br.readLine();
			
			
			List<AccountVO> list = account.listAccount(name, birth);
			System.out.println("계좌번호\t\t 고객성함\t \t계좌생성일\t");
			System.out.println("----------------------------------------------");
			for(AccountVO vo : list) {
				System.out.println(vo.getAccountNo()+ "    " + vo.getName() + "  	" + vo.getLaunch_date());
			}
			System.out.println("----------------------------------------------");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	private void pwdchange() {
		System.out.println("\n[계좌 패스워드 수정]");
		
		String accountNo, pwd;
		
		try {
			System.out.print("패스워드를 수정할 계좌번호는? :  ");
			accountNo = br.readLine();
			
			System.out.print("기존 패스워드를 입력하세요 :  ");
			pwd = br.readLine();
			
			
			AccountVO vo = account.readAccount(accountNo, pwd);
				if(vo==null) {
					System.out.println("계좌 또는 비밀번호가 틀립니다.");
					return;
				}
				
			System.out.println("새로운 패스워드를 입력하세요.");
			pwd = br.readLine();
			vo.setPwd(pwd);
		
			System.out.println("수정이 완료 되었습니다!!");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
	}
	
	
	
	

	private void delete() {
		System.out.println("\n[계좌 삭제]");
		
		String accountNo, pwd;
		
		try {
			System.out.print("삭제할 계좌의 계좌 번호 :  ");
			accountNo = br.readLine();
			
			System.out.print("패스워드를 입력하세요 :  ");
			pwd = br.readLine();
			
			boolean b = account.deleteAccount(accountNo, pwd);
			if(!b) {
				System.out.println("등록된 계좌 정보가 없습니다.\n");
				return;
			}
			
			System.out.println("계좌가 삭제되었습니다!");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println();
		
	}
	
	
	
	private void minus() {
		System.out.println("\n[계좌 출금 서비스]");
		
		String accountNo, pwd;
		long amount;
		
		try {
			System.out.print("출금하실 계좌 번호 입력: ");
			accountNo = br.readLine();
			
			System.out.print("패스워드를 입력하세요 :  ");
			pwd = br.readLine();
			
			AccountVO vo = account.readAccount(accountNo, pwd);
			if(vo==null) {
				System.out.println("계좌 또는 비밀번호가 틀립니다.");
				return;
			}
			
			System.out.print("출금액: ");
			amount = Integer.parseInt(br.readLine());
			
			account.withdraw(accountNo, pwd, amount);
			
			System.out.println("성공적으로 출금되었습니다.");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private void plus() {
		System.out.println("\n[계좌 입금 서비스]");
		
		String accountNo;
		long amount;
		
		//TransactionVO to = new TransactionVO();
		
		try {
			System.out.print("입금하실 계좌 번호 입력: ");
			accountNo = br.readLine();
			
			System.out.print("입금액: ");
			amount = Integer.parseInt(br.readLine());
			
			account.deposit(accountNo, amount);
			
			System.out.println("성공적으로 입금되었습니다.");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private void actionlist() {
		System.out.println("\n[계좌내역 조회 서비스]");
		
		String accountNo;
		
		try {
			System.out.print("조회하실 계좌 번호 입력: ");
			accountNo = br.readLine();
			
			List<TransactionVO> list2 = account.transactionHistory(accountNo);
			System.out.println("계좌번호\t\t 입출금\t\t금액\t잔액\t 거래일자\t");
			System.out.println("---------------------------------------------------------------");
			for(TransactionVO to : list2) {
				System.out.println(to.getAccountNo()+ "    " +to.getKind() + " \t\t" + to.getAmount() +"\t"+ to.getBalance()+"\t"+ to.getTransaction_date());
			}
			System.out.println("---------------------------------------------------------------");
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}
}

 

 

 

 

 

 

블로그의 정보

개발자 미니민의 개발로그

mini_min

활동하기