본문 바로가기
JAVA/Java 기본 돌아보기

[Java] Overloading 활용

by 늑인 2023. 9. 27.

 

public class Lotto {
	
	public static void main(String[] args) {
		Lotto l = new Lotto();
		int bundle [][][] = l.buyLottoBundle(17);
		l.printLotto(bundle);
	}
	
	/**
    	입력된 로또 값을 출력하는 메서드
    */
	public void printLotto(int[][][] buyLottoBundle) {
		for(int paper[][] : buyLottoBundle) {
			System.out.println("-----------------------------------------------------");
			for(int line[] : paper) {
				System.out.print("[");
				for(int number : line) {
					System.out.print(number +"\t");
				}
				System.out.print("]");
				System.out.println();
			}
			System.out.println("-----------------------------------------------------");
			System.out.println();
		}
	}
    
	/**
    	6개의 번호로 이루어진 로또 생성
        return int[] 결과값 1차원 배열로 리턴
    */
	public int[] generateLotto() {
		int lotto[] = new int[6];
		for(int i=0; i<6;i++) {
			// 1~45 숫자 입력
			int ran = (int)(Math.random()*45)+1;
			lotto[i] = ran;
            
			// 번호가 중복되는지 체크 
			for(int j=0; j<i; j++) {
				if(lotto[i] == lotto[j]) {
					i--;
					break;
				}
			}
		}
        
		// 로또 번호 정렬
		Arrays.sort(lotto);
		return lotto;
	}
	
    /**
    	기본적으로 5개의 로또를 생성하는 메소드 
        buyLottoPaper(int num) 을 오버로딩 하여 호출함.
    */
	public int[][] buyLottoPaper(){
		
		return buyLottoPaper(5);
	}
	
    /**
    	N개의 로또를 생성하는 메소드         
    */
	public int[][] buyLottoPaper(int num){
		
		int[][] lottoBundle = new int[num][6];
		for(int i=0; i<lottoBundle.length; i++) {
			lottoBundle[i] = generateLotto();
		}
		return lottoBundle;
	}
	
    /**
    	N개의 로또페이지를 생성하는 메소드
        buyLottoPaper을 호출하여서 이용함.
    */
	public int[][][] buyLottoBundle(int num){
		int paper = num/5;
		if(num%5 !=0) paper++;  
		
		int[][][] lottoBundle = new int[paper][5][6];
		for(int i=0; i<paper; i++) {
			lottoBundle[i] = buyLottoPaper();
		}
		if(num%5 !=0) {
			lottoBundle[paper-1] = buyLottoPaper(num%5);
		}
		return lottoBundle;
	}
	
}