2016-10-30 2 views
0

그래서 사용자 입력으로 모양을 만드는 콘솔 기반 Shape Creator을 작성하고 있습니다. 사용자가 5 광장 생성하고자하는 경우, 프로그램은 ArrayList을 만들고ArrayList에서 열 가져 오기

import java.util.ArrayList; 
import javax.swing.JOptionPane; 

public class ShapeMaker { 


    public ShapeMaker(){ 


     create(); 

    } 

    public void create(){ 
     //creates list of arrays within shapes. 
     ArrayList<Circle>circles=new ArrayList<Circle>(); 
     ArrayList<Square>squares=new ArrayList<Square>(); 
     ArrayList<Rectangle>rectangles=new ArrayList<Rectangle>(); 

     //this loop is dangerously infinity but I had to open the program for choices. 
     while(true){ 
      String[] choices={"Create Random Shapes","Circles","Squares","Rectangles", 
        "Print All Shapes","Print Rectangles","Print Squares", 
        "Print Circles","Exit"}; 
      String optionMenu = (String)JOptionPane.showInputDialog(
        null, 
        "Choose what you desire", 
        "Customized Dialog", 
        JOptionPane.PLAIN_MESSAGE, 
        null, 
        choices, 
        choices[0]); 


      if (optionMenu==choices[0]){ 
       //    String input4=(String)JOptionPane.showInputDialog(null,"How Many Shape ?","Question", 
       //      JOptionPane.QUESTION_MESSAGE); 
       //    int inputLength4=Integer.parseInt(input4); 
       //    for (int i=0;i<inputLength4;i++){ 
       // 
       //     circles2[i]=new Circle(); 
       //     circles.add(circles2[i]); 
       //     squares2[i]=new Square(); 
       //     squares.add(squares2[i]); 
       //     rectangles2[i]=new Rectangle(); 
       //     rectangles.add(rectangles2[i]); 
       // 
       //    } 
       //    JOptionPane.showMessageDialog(null,inputLength4+" shapes have been created successfully."); 
      } 
      else if (optionMenu==choices[1]) { 
       String input=(String)JOptionPane.showInputDialog(null,"How Many Circle ?","Question", 
         JOptionPane.QUESTION_MESSAGE); 
       int inputLength=Integer.parseInt(input); 
       Circle[]circles2=new Circle[inputLength]; 
       if (circles.isEmpty()==false){ 
        int delete=JOptionPane.showConfirmDialog(null, "There are circles in the list. Do you want to delete them?", 
          "Existing Shapes",JOptionPane.YES_NO_OPTION); 
        if (delete==JOptionPane.YES_OPTION){ 
         circles.remove(circles2); 
        } 
        else{ 
         for (int i=0;i<inputLength;i++){ 
          circles2[i]=new Circle(); 
          circles.add(circles2[i]); 
         } 
        } 

       } 
       else{ 


        for (int i=0;i<circles2.length;i++){ 
         circles2[i]=new Circle(); 
         circles.add(circles2[i]); 
        } 
        JOptionPane.showMessageDialog(null,inputLength+" circle have been created successfully."); 
       } 

      } 
      else if (optionMenu==choices[2]) { 
       String input3=(String)JOptionPane.showInputDialog(null,"How Many Squares ?","Question", 
         JOptionPane.QUESTION_MESSAGE); 
       int inputLength3=Integer.parseInt(input3); 
       Square[]squares2=new Square[inputLength3]; 
       if (squares.isEmpty()==false){ 
        int delete=JOptionPane.showConfirmDialog(null, "There are squares in the list. Do you want to delete them?", 
          "Existing Shapes",JOptionPane.YES_NO_OPTION); 
        if (delete==JOptionPane.YES_OPTION){ 
         squares.remove(squares2); 
        } 
        else{ 
         for (int i=0;i<inputLength3;i++){ 
          squares2[i]=new Square(); 
          squares.add(squares2[i]); 
         } 
        } 

       } 
       else{ 

        for (int i=0;i<inputLength3;i++){ 
         squares2[i]=new Square(); 
         squares.add(squares2[i]); 
        } 
       } 
       JOptionPane.showMessageDialog(null,inputLength3+" square have been created successfully."); 


      } 
      else if (optionMenu==choices[3]) { 
       String input2=(String)JOptionPane.showInputDialog(null,"How Many Rectangles ?","Question", 
         JOptionPane.QUESTION_MESSAGE); 
       int inputLength2=Integer.parseInt(input2); 
       Rectangle[]rectangles2=new Rectangle[inputLength2]; 
       if (rectangles.isEmpty()==false){ 
        int delete=JOptionPane.showConfirmDialog(null, "There are rectangles in the list. Do you want to delete them?", 
          "Existing Shapes",JOptionPane.YES_NO_OPTION); 
        if (delete==JOptionPane.YES_OPTION){ 
         rectangles.remove(rectangles2); 
        } 
        else{ 
         for (int i=0;i<inputLength2;i++){ 
          rectangles2[i]=new Rectangle(); 
          rectangles.add(rectangles2[i]); 
         } 
        } 

       } 
       else{ 
        for (int i=0;i<inputLength2;i++){ 
         rectangles2[i]=new Rectangle(); 
         rectangles.add(rectangles2[i]); 

        } 
        JOptionPane.showMessageDialog(null,inputLength2+" rectangle have been created successfully."); 
       } 
      } 
      else if (optionMenu==choices[4]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<rectangles.size();i++){ 
        System.out.println(rectangles.get(i)); 
       } 
       for (int i=0;i<squares.size();i++){ 
        System.out.println(squares.get(i)); 
       } 
       for (int i=0;i<circles.size();i++){ 
        System.out.println(circles.get(i));     
       } 


      } 
      else if (optionMenu==choices[5]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<rectangles.size();i++){ 
        System.out.println(rectangles.get(i)); 

       } 
       System.out.print("There are "+rectangles.size()+" rectangle with total area of "); 
      } 
      else if (optionMenu==choices[6]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<squares.size();i++){ 
        System.out.println(squares.get(i)); 

       } 
       System.out.print("There are "+squares.size()+" square with total area of "); 

      } 
      else if (optionMenu==choices[7]) { 
       System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape", 
         "Area","Full Shape", " Distance", "Properties"); 
       System.out.println(); 
       for (int i=0;i<circles.size();i++){ 
        System.out.println(circles.get(i)); 

       } 
       System.out.print("There are "+circles.size()+" circle with total area of "); 
      } 
      else if(optionMenu==choices[8]) { 
       System.exit(1); 

      } 
     } 

    } 

    public static void main(String [] args){ 
     new ShapeMaker(); 
     //  System.out.println(shape); 


    } 
} 
이 질문은

, 난 지역 행에서 값을 가져 총 면적을 계산해야하는 등 shapeId, 면적이 광장 클래스의 데이터를 얻을 수 이 모양들 중. Output

그래서 어떻게 생성 된 ArrayList의 모든 영역을 가져올 수 있습니까?

+0

전체 코드를 게시 할 수 있습니까? else 문맥을 지정하십시오. –

+0

모든 사각형을 인쇄하는 코드를보십시오. 인쇄중인 '영역'을 볼 수 있습니다. 이 스크린 샷에서 전체 면적을 계산할 수 있습니까? 네가 할 수있을거야. 이제 코드 – borowis

+0

에서 어떻게 할 수 있을지 생각해보십시오 @ CarlosVazquezLosada가 완전한 코드를 추가했습니다. – Egrimo

답변

1

Greatings, Egrimo. ID, Shape, Area, Full Shape, DistanceProperties :

당신이 말했듯이

, 당신은 모든 속성이있는 광장 클래스를 가지고있다.

당신이 할 경우 :

System.out.println(squares.get(i)); 

그리고 당신은 Area 속성에 액세스하려면, 당신은 할 필요가 :

  1. 이 광장 클래스의 방법 getArea()을 만듭니다. 이 메소드는 public이어야하고 사각형 영역을 리턴합니다. 당신의 사각형 영역을 인쇄 할

System.out.println(squares.get(i).getArea()); : 당신의에서 System.out.println에서

  • 통화의 getArea() 메소드는 사각형 영역을 인쇄합니다.

    편집 :

    당신이의 getArea() 메소드를 만드는 방법을 모르는 경우,이 예제를 살펴 :

    public class Square{ 
        int area; 
        int id; 
        // other attributes 
    
        // constructor 
    
        public int getArea(){ // This method will return your square area 
         return this.area; 
        } 
    } 
    

    좋은 하루 되세요!

  • +0

    상세한 답변 해 주셔서 감사합니다. 내 영역 메서드는 IShape 클래스에서 구현되며 public double area()입니다. { \t \t // 사각형 영역을 계산합니다. \t \t 반환 크기 * 크기; \t} – Egrimo