Print
// Print ---부모 객체.
package ch10_interface;
public interface Print {
int START = 1;
int CANCEL = 2;
// 인터페이스에 default abstract로 설정됌.
// public abstract void printStart();
public void printStart();
public void printCancel();
}
-----------------------------------------------------------------------------
// Scan ---Print 자식 객체.
package ch10_interface;
public class Scan implements Print{
@Print_Override
public void printStart() {
System.out.println("- 스캔 시작 -");
}
@Print_Override
public void printCancel() {
System.out.println("- 스캔 취소 -");
}
}
-----------------------------------------------------------------------------
// Printer ---Print 자식객체.
package ch10_interface;
public class Printer implements Print{
@Override
public void printStart() {
System.out.println("- 프린터 출력 시작 -");
}
@Override
public void printCancel() {
System.out.println("- 프린터 출력 취소 -");
}
}
-----------------------------------------------------------------------------
//PrintMachine -- 행위 구현 메소드.
package ch10_interface;
import java.util.Scanner;
public class PrintMachine {
private Print current; // 현재 사용중인 장치.
private int device; // 1.프린터 2.스캐너
private Printer printer;
private Scan scan;
private Scanner scanner;
public PrintMachine() {
printer = new Printer();
scan = new Scan();
device = 1;
current = printer;
scanner = new Scanner(System.in);
}
// 메뉴
public boolean modeSelect() {
System.out.print("1.장치 선택 2.인쇄/취소 >> ");
int mode =scanner.nextInt();
switch(mode) {
case 1: // 장치선택
selectDevice();
break;
case 2: // 인쇄,취소
printRun();
break;
case 0: // 종료
return false;
}
System.out.println();
return true;
}
// 장치 선택
public void selectDevice() {
System.out.print("1.프린터 2.스캐너 >> ");
device = scanner.nextInt();
if(device == 1)
current = printer;
else if(device == 2)
current =scan;
}
// 인쇄,취소
public void printRun() {
System.out.print("1.시작 2.취소 >> ");
int select = scanner.nextInt();
switch(select) {
case Print.START:
current.printStart();
break;
case Print.CANCEL:
current.printCancel();
break;
}
}
}
-----------------------------------------------------------------------------
// PrintMain -- 메인 함수
package ch10_interface;
public class PrintMain {
public static void main(String[] args) {
PrintMachine pm = new PrintMachine();
boolean run = true;
while(run) {
run = pm.modeSelect();
System.out.println();
}
System.out.println("- Power OFF -");
}
}