public abstract class Shape {
private Shape next;
public Shape() {next = null;}
public void setNext(Shape obj) {next = obj;}
public Shape getNext() {return next;}
public abstract void draw();
}
import java.util.Scanner;
class Line extends Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Line");
}
}
class Rect extends Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Rect");
}
}
class Circle extends Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Circle");
}
}
public class GraphicEditor {
static Shape start=null,last=null,obj=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("그래픽 에디터 beauty을 실행합니다.");
while(true) {
System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>");
int choice = sc.nextInt();
switch(choice) {
case 1 : //삽입
System.out.print("Line(1), Rect(2), Circle(3)>>");
int num = sc.nextInt();
if(num<1||num>3) break;
insert(num);
break;
case 2: //삭제
System.out.print("삭제할 도형의 위치>>");
int index = sc.nextInt();
if(!delete(index)) {
System.out.println("삭제할 수 없습니다.");
}
break;
case 3: //모두 보기
Shape p = start;
while(p!=null) {
p.draw();
p = p.getNext();
}
break;
case 4:
System.out.println("beauty를 종료합니다.");
return;
default:
System.out.println("잘못 입력했습니다");
System.out.println("다시압력하새요");
return;
}
}
}
private static boolean delete(int index) {
// TODO Auto-generated method stub
Shape current = start, previous = start;
if (start == null) // 리스트가 빈 경우
return false;
for (int i=0; i<index; i++) {
previous = current;
current = current.getNext(); // 다음 원소로 이동
if (current == null) // 인덱스가 리스트 원소 갯수보다 큰 경우
return false;
}
if (start == last) { // 리스트에 원소가 한개밖에 없는 경우
start = last = null;
return true;
}
if (current == start) {// 첫번째 원소를 삭제하는 경우
start = start.getNext(); // 다음 원소가 첫번째 원소가 됨
}
else if (current == last) {// 마지막 원소를 삭제하는 경우
last = previous; // 이전 원소가 마지막 원소가 됨
last.setNext(null);
} else {
previous.setNext(current.getNext()); // 현재 원소를 리스트에서 삭제
}
return true;
}
private static void insert(int choice) {
Shape obj = null;
switch(choice) {
case 1: //Line
obj= new Line();
break;
case 2: //Rect
obj = new Rect();
break;
case 3: //Circle
obj = new Circle();
}
//리스트가 비었을 경우
if(start == null) {
start = last = obj;
}
else {
last.setNext(obj); //마지막 원소 뒤에 삽입
last = obj;
}
}
}
결과
그래픽 에디터 beauty을 실행합니다.
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>1
Line(1), Rect(2), Circle(3)>>1
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>1
Line(1), Rect(2), Circle(3)>>2
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>1
Line(1), Rect(2), Circle(3)>>3
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>3
Line
Rect
Circle
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>2
삭제할 도형의 위치>>1
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>3
Line
Circle
삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>4
beauty를 종료합니다.
더보기
intsert, delete 구현을 못해서 찾아서 했다
언제쯤 잘 구현 할까
'명품JAVA프로그래밍 > 5장 상속' 카테고리의 다른 글
[명품JAVA프로그래밍] 5장 실습문제 14번 (0) | 2022.01.17 |
---|---|
[명품JAVA프로그래밍] 5장 실습문제 13번 (0) | 2022.01.17 |
[명품JAVA프로그래밍] 5장 실습문제 11번 (0) | 2022.01.17 |
[명품JAVA프로그래밍] 5장 실습문제 10번 (0) | 2022.01.13 |
[명품JAVA프로그래밍] 5장 실습문제 9번 (0) | 2022.01.13 |