728x90
반응형
Shape.class
package Ex03;
public abstract class Shape {
private Shape next;
public Shape() {
next=null;
}
public Shape getNext() {
return next;
}
public Shape(Shape next) {
this.next = next;
}
public abstract void draw();
}
Rect.class
package Ex03;
public class Rect extends Shape{
@Override
public void draw() {
System.out.println("Rect");
}
}
Line.class
package Ex03;
public class Line extends Shape{
@Override
public void draw() {
System.out.println("Line");
}
}
Circle.class
package Ex03;
public class Circle extends Shape{
@Override
public void draw() {
System.out.println("Circle");
}
}
GraphicEdit.class
package Ex03;
import java.util.Scanner;
import java.util.Vector;
public class GraphicEdit {
private Scanner sc = new Scanner(System.in);
/*
벡터를 3개 만들어야 하지만 다형성을 이용해서 1개만 만듬
*/
private Vector<Shape> v = new Vector<>();
private void run() {
System.out.println("그래픽 에디터를 실행합니다.");
int choice = 0;
while(choice != 4) {
System.out.println("삽입(1), 삭제(2), 모두보기(3), 종료(4)");
int type, index;
choice = sc.nextInt();
switch(choice) {
case 1: //삽입
System.out.println("Line(1), Rect(2), Circle(3)");
type = sc.nextInt();
if(type < 1 || type > 3) {
System.out.println("잘못 선택했습니다.");
break;
}
insert(type);
break;
case 2: //삭제
System.out.println("삭제할 도형의 위치 >> ");
index = sc.nextInt();
if(!delete(index)) {
System.out.println("삭제 할 수 없습니다.");
}
break;
case 3: //모두보기
view();
break;
case 4: //종료
break;
default :
System.out.println("잘못된 번호를 입력했습니다. 다시 입력하세요.");
}
}
}
private void insert(int choice) {
Shape shape = null;
switch (choice) {
case 1:
shape = new Line();
break;
case 2:
shape = new Rect();
break;
case 3:
shape = new Circle();
break;
default:
System.out.println("잘못된 번호를 입력했습니다. 다시 입력하세요.");
}
v.add(shape);
}
private boolean delete(int index) {
if(v.size() == 0 || index >= v.size()) {
return false;
}
v.remove(index);
return true;
}
private void view() {
for(int i=0; i<v.size(); i++) {
v.get(i).draw();
}
}
public static void main(String[] args) {
GraphicEdit ge = new GraphicEdit();
ge.run();
}
}
728x90
반응형
'JAVA > 예제' 카테고리의 다른 글
[JAVA] addActionListener 카운터증가 (0) | 2022.10.28 |
---|---|
[JAVA] Label 클릭시 위치 랜덤 이동 (0) | 2022.10.28 |
[JAVA] HashMap를 활용한 장학생선발 (0) | 2022.10.27 |
[JAVA] HashMap를 활용한 포인트관리 (0) | 2022.10.27 |
[JAVA] HashMap를 활용한 학생관리 (0) | 2022.10.27 |
댓글