JAVA 싱글톤 패턴 연습

안희수·2021년 1월 26일
0
package study;

import java.util.Scanner;

public class main {

	

	
	public static void main(String[] args) {
		
		account ac = new account();
		ac.setmoney(0.0);
		Scanner sc = new Scanner(System.in);
		
		System.out.println("계좌명을 입력하세요.");
		ac.accountName = sc.next();
		sc.reset();
		
		System.out.println("예금주명을 입력하세요.");
		ac.userName = sc.next();
		sc.reset();
		
		if(!ac.exisits_account()) {
			System.out.println("계좌명이 존재하지 않습니다. 새로 등록하시겠습니까?");
			String yesno = sc.next();
			if(yesno.equals("Y")) {
				ac.registaccount();
			} else {
				System.out.println("거래를 종료합니다.");
				return;
			}
		}
		
		if(ac.accList.containsKey(ac.accountName)) {
			System.out.println("거래유형을 선택하세요 1: 입금 2: 출금");

			String option = sc.next();
			select(option,ac);
		}

	}
	
	public static void select(String str, account ac) {

		double inputmoney = 0;
		
		Scanner sc = new Scanner(System.in);
		
		int option = Integer.parseInt(str);
		
		if(option == 1) { // 입금
			System.out.println("입금액을 입력하여 주세요.");
			
			inputmoney = sc.nextDouble();
			if(inputmoney == 0) {
				System.out.println("입금액이 0 원 입니다.");
			}
			else {
				inmoney im = new inmoney();
				im.setmoney(ac.money, inputmoney);
				ac.setmoney(im.getmoney());	
				
				System.out.println("계좌명: " + ac.accountName);
				System.out.println("예금주명: " + ac.userName);
				System.out.println("입금액 :" + inputmoney + "원 현재 금액 :" + ac.money + " 원");
				
				System.out.println(ac.accList);
				
				
				
				if(ac.accList.containsKey(ac.accountName)) {
					System.out.println("거래유형을 선택하세요 1: 입금 2: 출금");				
					select(sc.next(),ac);
				}

			}
		}
		else if (option == 2) { // 출금
			if(ac.money <= 0) {
				System.out.println("출금할 금액이 없습니다.");
				if(ac.accList.containsKey(ac.accountName)) {
					System.out.println("거래유형을 선택하세요 1: 입금 2: 출금");	
					select(sc.next(),ac);
				}

			}
			else {
				System.out.println("출금액을 입력하여 주세요.");
				inputmoney = sc.nextDouble();
				
				outmoney om = new outmoney();
				om.setmoney(ac.getmoney(), inputmoney);
				ac.setmoney(om.getmoney());
				System.out.println("계좌명: " + ac.accountName);
				System.out.println("예금주명: " + ac.userName);
				System.out.println("출금액 :" + inputmoney + "원 현재 금액 :" + ac.money + " 원");

				System.out.println(ac.accList);
				
				if(ac.accList.containsKey(ac.accountName)) {
					System.out.println("거래유형을 선택하세요 1: 입금 2: 출금");	
					select(sc.next(),ac);
				}
			}
			
		}
		else {
			System.out.println("잘못된 유형입니다.");
			
			if(ac.accList.containsKey(ac.accountName)) {
				System.out.println("거래유형을 선택하세요 1: 입금 2: 출금");	
				select(sc.next(),ac);
			}
		}
		
	}
	

}
세요

자바를 개발자가 되기 전
교육기관에서 선행학습으로 몇개월 배워본 것이 전부라
자바를 거의 처음 다뤄보다 시피 해봤는데

싱글톤 패턴을 완벽하게 이해하고 짠 것이 아니라
싱글톤 패턴을 잘못 구현했을 수도 있으므로
해당 코드는 차후 다시 수정 보완을 지속적으로 할 예정입니다

싱글톤 패턴에 사용한 클래스는 다음과 같습니다

입금 매서드에 대한 클래스
public class inmoney {

public double tempmoney;
public void setmoney(double nowmoney, double money) {
	this.tempmoney =  nowmoney + money;		
}	
public double getmoney(){		
	return tempmoney;
}

}
출금 매서드에 대한 클래스
public class outmoney {

public double tempmoney;
public void setmoney(double nowmoney, double money) {
	this.tempmoney =  nowmoney - money;
	
}	
public double getmoney(){		
	return tempmoney;
}

}
계좌 관리에 대한 클래스
public class account {

public String accountName; // 계좌명
public String userName; // 예금주	
public Double money; // 금액

public HashMap<String, String> accList = new HashMap<String, String>();

// 계좌명
public void setaccountName(String account) {
	this.accountName = account;
}
public String getaccountName() {
	return accountName;
}

public boolean exisits_account() 
{
	boolean isExists = false;
	if(accList.isEmpty()) {
		isExists = false;
	} else {			
		if(accList.containsKey(accountName)) {
			isExists = true;
		}			
	}		
	return isExists;
}

public void registaccount() 
{
	if(accList.isEmpty()) {
		accList.put(accountName, userName);
	} 
}

// 예금주
public void setuserName(String user) {
	this.userName = user;
}
public String getuserName() {
	return userName;
}

// 계좌금액
public void setmoney(Double money) {
	this.money = money;
}
public Double getmoney() {
	return money;
}	

}

구현하고자 한 내용은 다음과 같습니다

계좌를 개설하고 예금주 이름을 입력하고
입출여부 선택 뒤 금액을 입력하면 account라는 클래스에
계좌로 돈이 입금되거나 출금되고 계속 진행할 지 다시 묻는 방식이며

일전에 싱글톤 패턴 코딩 테스트를 보게 되었을때
미처 구현하지 못해 그 이후 혼자서 구현해본 코드 입니다

profile
9년차 소프트웨어 개발자 (2024년 재 개편 예정입니다)

0개의 댓글

관련 채용 정보