JAVA

210817_스캐너+다향성+switch

요옫 2021. 8. 17. 17:11

interface Command{

 

public void write();

}

 

class Insert implements Command{

 

@Override

public void write() {

System.out.println("게시물을 추가합니다");

}

}

 

class Erase implements Command{

 

@Override

public void write() {

System.out.println("게시물을 삭제합니다");

}

}

 

class Modify implements Command{

 

@Override

public void write() {

System.out.println("게시물을 수정합니다");

}

}

 

class Select implements Command{

 

@Override

public void write() {

System.out.println("게시물을 출력합니다");

}

}

 

public class ScannerInheriEx09 {

 

public static void main(String[] args) {

 

int num=0;

Command command=null;

Scanner sc=new Scanner(System.in);

 

while(true)

{

System.out.println("[메뉴]");

System.out.println("1.추가\n2.삭제\n3.수정\n4.출력\n5.종료");

System.out.print("번호선택==>");

num=sc.nextInt();

 

switch (num) {

case 1:

command=new Insert();

break;

case 2:

command=new Erase();

break;

case 3:

command=new Modify();

break;

case 4:

command=new Select();

break;

default:

System.out.println("종료");

System.exit(5);

}

command.write();

}

 

}

}

 

//결과

[메뉴]

1.추가

2.삭제

3.수정

4.출력

5.종료

번호선택==>1

게시물을 추가합니다

[메뉴]

1.추가

2.삭제

3.수정

4.출력

5.종료

번호선택==>4

게시물을 출력합니다

[메뉴]

1.추가

2.삭제

3.수정

4.출력

5.종료

번호선택==>5

종료

'JAVA' 카테고리의 다른 글

210818_익명내부클래스  (0) 2021.08.18
210818_내부클래스  (0) 2021.08.18
210817_Abstract+배열  (0) 2021.08.17
210817_인터페이스+다형성  (0) 2021.08.17
210817_추상클래스+다형성  (0) 2021.08.17