printertest

신미경·2021년 4월 29일
0

JAVA 문제

목록 보기
2/2
class Printer{
	private int numOfPapers = 0;
	private boolean duplex;
	
	
	Printer(int numOfPapers, boolean duplex){
		this.numOfPapers = numOfPapers;
		this.duplex = duplex;
	}
	
	public int getNumOfPapers() {
		return numOfPapers;
	}

	public void setNumOfPapers(int numOfPapers) {
		this.numOfPapers = numOfPapers;
	}

	public boolean isDuplex() {//isDuplex 참 거짓값만 넘겨받겠다
		return duplex;
	}

	public void setDuplex(boolean duplex) {
		this.duplex = duplex;
	}

	void print(int amount) {
		//코드추가
		int countPages;
		if(numOfPapers == 0) {
			System.out.println("남아있는 용지가 없습니다.");
			return;
		}
		
		if(duplex) {
			countPages = amount % 2 == 0?  amount / 2: amount/2 + 1;
			
		}else {
			countPages = amount;
		}
		if(countPages <= numOfPapers) {
			System.out.println(countPages +"장을 출력합니다.");
			numOfPapers -= countPages;
			System.out.println("현재 " + numOfPapers + "장 남아있습니다");
		} else {
			System.out.println("모두 출력하려면 용지가 " + (countPages - numOfPapers) + "장 용지가 부족합니다");
			System.out.println(numOfPapers + "장만 출력합니다.");
			numOfPapers = 0;
		}
		
//		if(duplex) {//boolean이라 자체가 true, false가지므로 duplex=true;로 적지 않아도 됨
//			int countPages = amount % 2 == 0?  amount / 2: amount/2 + 1;//페이지수가 홀수이면 양면 인쇄 시 나머지가 남으므로 한 장 더 출력해야함
//			if(countPages <= numOfPapers) {
//				System.out.println(countPages +"장을 출력합니다.");
//				numOfPapers -= countPages;
//				System.out.println("현재 " + numOfPapers + "장 남아있습니다");
//			} else {
//				System.out.println("모두 출력하려면 용지가 " + (countPages - numOfPapers) + "장 용지가 부족합니다");
//				System.out.println(numOfPapers + "장만 출력합니다.");
//				numOfPapers = 0;
//			}
//					
//		}
		
//		else {
//		
//		
//		
//		if(amount <= numOfPapers) {
//		System.out.println(amount +"장을 출력합니다.");
//		numOfPapers -= amount;
//		System.out.println("현재 " + numOfPapers + "장 남아있습니다");
//		}else {
//			System.out.println("모두 출력하려면 용지가 " + (amount - numOfPapers) + "장 용지가 부족합니다");
//			System.out.println(numOfPapers + "장만 출력합니다.");
//			numOfPapers = 0;
//		}
//	}
	}
}




public class PrinterTest {

	public static void main(String[] args) {
		//Printer 클래스를 테스트하는 코드 
		Printer p = new Printer(20, true);
//		p.numOfPapers = 100;
		p.print(25);
		p.setDuplex(false);
		p.print(10);
		

	}

}

0개의 댓글

Powered by GraphCDN, the GraphQL CDN