응용문제 - 은행

2025. 1. 25. 20:12·Java

추상클래스.java

package method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public abstract class ab_box3 {
	Integer total = 0;
	final String menus[] = {"1. 신한", "2. 농협", "3. 국민", "4. 하나"};
	Scanner sc = new Scanner(System.in);
	ArrayList<ArrayList<String>> bank = null;
	ArrayList<String> acc = null;
	
	public String today() {
		Date day = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sf.format(day);
	}
	public abstract String deposit(int money);	//입금 메소드
	public abstract String drawal(int money);	//출금 메소드
}

메인.java

package method;
import java.util.*;

public class ab4_1 {	
	public static void main(String[] args) {		
		ab41_box ab = new ab41_box();
		ab.menu();
	}
}

class ab41_box extends ab_box3{
	int mn = 0;
	int bn = 0;
	ArrayList<String> allmoney = new ArrayList<String>();
	ArrayList<Object> b = null;
	ArrayList<ArrayList<Object>> sh_bb = new ArrayList<ArrayList<Object>>();
	ArrayList<ArrayList<Object>> nh_bb = new ArrayList<ArrayList<Object>>();
	ArrayList<ArrayList<Object>> km_bb = new ArrayList<ArrayList<Object>>();
	ArrayList<ArrayList<Object>> hn_bb = new ArrayList<ArrayList<Object>>();
	static ArrayList<ArrayList<ArrayList<Object>>> all_bb = new ArrayList<ArrayList<ArrayList<Object>>>();
	
	//total : 잔액 

	public ab41_box() {
		this.bank = new ArrayList<ArrayList<String>>();
		all_bb.add(sh_bb);
		all_bb.add(nh_bb);
		all_bb.add(km_bb);
		all_bb.add(hn_bb); 
		
//		System.out.println("최종잔액 데이터: " + all_bb);
	}
	
	public void print_menu() {
		System.out.println("---------------");
		System.out.println("은행을 선택해주세요");
		int i = 0;
		while(i<this.menus.length) {
			System.out.printf(this.menus[i]+", ");
			i++;
		}
		System.out.println("5. 종료");
	}
	
	//menu 은행설정 -> 잔액설정 -> menu2
	public void menu() {
		print_menu();
		try {
			this.mn = sc.nextInt();
		}catch(Exception e) {
			System.out.println("잘못된 입력입니다.");
			new ab41_box().menu();
		}
	
		
		System.out.println("---------------");
		if (this.mn <= 4 && this.mn >= 1) { // 은행번호 설정
			if (this.mn == 2) {
				System.out.println("현재 농협은 시스템 점검으로 서비스가 불가능합니다.");
				this.menu();
			}
			this.bn = mn - 1;
			this.total_set(this.bn);
			this.menu2();
		} else if (this.mn == 5) {
			System.out.println("종료합니다.");
			this.sc.close();
			System.exit(0);
		} else {
			System.out.println("번호를 잘못입력하셨습니다.");
			this.menu();
		}
	}
	
	public void menu2() {
		System.out.println("입/출금 메뉴를 선택하세요");				
		System.out.println("1. 입금, 2. 출금, 3. 잔액확인, 4. 돌아가기, 5. 종료");		
		try {
			this.mn = sc.nextInt();
		}catch(Exception e) {
			System.out.println("잘못된 입력입니다.");
			new ab41_box().menu();
		}
		System.out.println("---------------");
		int money = 0;
		if (this.mn == 1) {		//입금
			while(true) {
				System.out.println("입금 금액을 입력하세요 : ");
				money = sc.nextInt();
				if(money>0) {
					break;
				}else {
					System.out.println("금액을 잘못입력하셨습니다");
				}	
			}
			System.out.println(this.deposit(money));
			this.menu();
		}else if (this.mn == 2) {	//출금
			while(true) {
				System.out.println("출금 금액을 입력하세요 (돌아가기 -1) : ");
				money = sc.nextInt();
				if(money == -1) {	//잔액없으면 무한루프돌아버림;;
					this.menu2();
				}else if(money>=0 && money<=this.total) {
					break;
				}else {
					System.out.println("금액을 잘못입력하셨습니다");
				}
			}
			System.out.println(this.drawal(money));
			this.menu();
		} else if (this.mn ==3) {	//잔액확인
			check_bal();
			this.menu();		
		}else if (this.mn ==4) {	//돌아가기
			this.menu();		
		} else if (this.mn ==5) {
			System.out.println("종료합니다.");
			this.sc.close();
			System.exit(0);		
		}else {
			System.out.println("번호를 잘못입력하셨습니다.");
			this.menu2();
		}
		
	}
	
	/*
	 배열에 은행별 통장 
	 {
	0신한{
		0{0금액, 1잔액, 2날짜},
		1{“0”, ”5000”, ”날짜”}

	}
	1농협{
	}
	
}
	 */
	public void check_bal() {	// 통장 확인
		all_bb.add(sh_bb);
		all_bb.add(nh_bb);
		all_bb.add(km_bb);
		all_bb.add(hn_bb);
		
		if(this.all_bb.get(this.bn).size() == 0) {	//내역 없을때 
			System.out.println("거래 내역이 없습니다.");
			this.menu();
		}
		
		int i = 0;
		while (i < this.all_bb.get(this.bn).size()) {
//			System.out.println(this.all_bb.get(this.bn).size());
//			System.out.println(i);
			System.out.print("[" + this.all_bb.get(this.bn).get(i).get(2) + "]");
			if((int)this.all_bb.get(this.bn).get(i).get(0) < 0) {
				System.out.print(" [출금 " + this.all_bb.get(this.bn).get(i).get(0) + "원]");
			}else {
				System.out.print(" [입금 " + this.all_bb.get(this.bn).get(i).get(0) + "원]");
			}
			
			System.out.println(" [잔액 " + this.all_bb.get(this.bn).get(i).get(1) + "]");
			i++;
		}
	}
		
	//은행별 잔액설정
	public void total_set(int bn) {
		int bbls = (int)this.all_bb.get(bn).size();	//해당 은행의 기록개수
//		System.out.println("bbls : "+bbls);
		if(bbls==0) {	//통장 기록이 없으면 잔액 0
			this.total=0;
			if(bn == 2) {	//국민은행 잔액 10마넌
				this.total = 100000;
//				System.out.println("bbls : "+bbls+"total : "+total);
			}
		}else {	//통장 기록있을때 잔액 설정 
			this.total = (int)this.all_bb.get(bn).get(bbls-1).get(1);	//해당 은행 통장의 잔액 가져와서 total에 넣기
		}
	}
	
	
	
	//입출금 1차배열 기록->은행별 2차 배열에 저장
	public void bankbook(int money) {	//통장
		this.b = new ArrayList<Object>();
		this.b.add(money);
		this.b.add(this.total);
		this.b.add(this.today());
		
		if(this.bn == 0) {
			this.sh_bb.add(this.b);
		}
//		else if(this.bn == 1) {
//			this.nh_bb.add(this.b);
//		}
		else if(this.bn == 2) {
			this.km_bb.add(this.b);
		}else if(this.bn == 3) {
			this.hn_bb.add(this.b);
		}else {
			System.out.println("bankbook() 잘못된 은행번호");
		}
		
	}
	
	@Override
	public String deposit(int money) {	//입금
		this.total += money;
		String msg = money+"원 입금되었습니다.\n잔액 : "+this.total;
		this.bankbook(money);
		
		return msg;
	}
	
	@Override
	public String drawal(int money) {	//출금
//		System.out.println("drawal()");
		String msg = null;
		
		this.total -= money;
		msg = money+"원 출금되었습니다.\n잔액 : "+this.total;
		money = money - (money*2);
		this.bankbook(money);
	
		return msg;
	}
	
}

⭐️어려웠던점

은행의 기록을 어떻게 저장할까? -> 3차원배열

아 잔액은 숫자로 처리하고 날짜기록은 스트링이네 -> 오브젝트배열

만들어둔 스트링 2차배열을 3차 오브젝트 배열로 이관? 안되네 -> 변환할 방법없음

추상클래스의 전역변수로 설정된 클래스배열을 어떻게 클래스 전체에서 쓰게 만들지? -> 즉시실행함수에서 선언 (void써서 안됐음ㅎㅎ;)

try~catch너무어렵다 어디서부터 어디까지 묶지 -> 그냥 스캔부분만 묶고 캐치에서 new 클래스.메소드(); 때리기

어? 그랬더니 내 통장 기록이 new 때문에 다 사라졌어 ㅠㅠ -> 통장 배열 static변수로 만들기

 

너무 어려웠는데 하나하나 해결하면서 뿌듯했다

옛날엔 자바가 이해가 하나도 안됐는데 살짝 알듯말듯한 느낌

코드 짜면서 구조를 자꾸 바꾸느라 지저분하고 이상한 코드가 많고 더 손대고싶은 부분도 많지만

시간이 너무 모자랐다

 

다음번에 다시 짜면 잘할 수 있을것같다!!! 영차차

 

 

 

저작자표시 비영리 변경금지 (새창열림)
'Java' 카테고리의 다른 글
  • Java - interface
  • 복습11 - Java 응용
  • 복습10 - Java 응용
  • 복습9 - Java 응용
9na0
9na0
응애
  • 9na0
    구나딩
    9na0
  • 전체
    오늘
    어제
    • 분류 전체보기 (211)
      • Web (118)
      • Java (28)
      • 데이터베이스 (14)
      • 세팅 (12)
      • 과제 (3)
      • 쪽지시험 (2)
      • 정보처리기사 (4)
      • 서버 (25)
  • 블로그 메뉴

    • 링크

      • 포폴
      • 구깃
    • 공지사항

    • 인기 글

    • 태그

      file24
      net1
      notice_writer
      datalist
      re_java10
      spring-boot
      Oracle
      macbook pro m4
      file25_t
      net3
      net2
      net4
      io_dto
      java_io1~10
      file25
      re2
      exam1_1~10
      noticewriteok
      net5~10
      ab1
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    9na0
    응용문제 - 은행
    상단으로

    티스토리툴바