2017-03-05 2 views
0

내 모든 것이 더 깨끗하고 멋지게 보이도록 코드를 줄이려고하지만이 코드를 변경하는 방법을 모르므로 기능은 동일하지만 코드는 적게 유지됩니다. 사람이이 코드를 동일한 결과로 줄일 수 있습니까?

static class Action4 implements ActionListener { 

    @Override 
    public void actionPerformed(java.awt.event.ActionEvent e) { 

     String name = ((JTextField) e.getSource()).getText(); 

     if (name.equals("Test1")) { 
      name = JOptionPane.showInputDialog("Enter Name "); 

      String day; 
      int totalCost; 
      int visitors; 

      day = JOptionPane.showInputDialog("Enter what day you'd like to attend "); 

      visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting ")); 

      totalCost = visitors * 20; 

      JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost); 
     } else { 

      if (name.equals("test2")) { 
       name = JOptionPane.showInputDialog("Enter Name "); 

       String day; 
       day = JOptionPane.showInputDialog("Enter what day you'd like to attend "); 
       int visitors; 
       visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting ")); 

       int totalCost; 
       totalCost = visitors * 17; 

       JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost); 
      } else { 

       if (name.equals("test3")) { 
        name = JOptionPane.showInputDialog("Enter Name "); 

        String day; 
        day = JOptionPane.showInputDialog("Enter what day you'd like to attend "); 
        int visitors; 
        visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting ")); 

        int totalCost; 
        totalCost = visitors * 22; 

        JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost); 
       } else { 

        JOptionPane.showMessageDialog(null, "Wrong input!"); 

       } 
      } 
+0

가장 확실한 변경 : 'else {if'가 아니라'else if'를 사용하십시오. –

+1

이 질문은 [codereview.se]에 더 적합합니다. –

답변

0

별도의 방법으로 코드를 분할하는 방법을 말해 및 스위치 케이스를 사용할 수 있다면 나는 그것을 감사하는 것과 같은 출력이 코드를 작성하는 나를 위해 다른 방법이 있다면, 그래서 나는 또한 자바에 새로운 오전 (만약 자바> = 7) : 이것은 당신이

static class Action4 implements ActionListener { 

@Override 
public void actionPerformed(java.awt.event.ActionEvent e) { 

    String name = ((JTextField) e.getSource()).getText(); 
    name = JOptionPane.showInputDialog("Enter Name "); 
    String day; 
    int totalCost; 
    int visitors; 
    int multiplier = 0; 
    day = JOptionPane.showInputDialog("Enter what day you'd like to attend "); 
    visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting ")); 
    if (name.equals("Test1")) 
     multiplier = 20; 
    else if (name.equals("test2")) 
     multiplier = 17; 
    else if (name.equals("test3")) 
     multiplier = 22; 
    else 
     JOptionPane.showMessageDialog(null, "Wrong input!"); 
    totalCost = visitors * multiplier; 
    if(multiplier != 0) 
     JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost); 
} 
0
class Action4 implements ActionListener { 
    String name = null; 
    String day; 
    int totalCost; 
    int visitors; 

    @Override 
    public void actionPerformed(java.awt.event.ActionEvent e) { 
     name = ((JTextField) e.getSource()).getText(); 
     if (name.equals("Test1")) { 
      init(20); 
     } else if (name.equals("test2")) { 
      init(17); 
     } else if (name.equals("test3")) { 
      init(22); 
     } else { 
      JOptionPane.showMessageDialog(null, "Wrong input!"); 
     } 
    } 

    private void init(int value) { 
     name = JOptionPane.showInputDialog("Enter Name "); 
     day = JOptionPane.showInputDialog("Enter what day you'd like to attend "); 
     visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting ")); 
     totalCost = visitors * value; 
     JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost); 
    } 
} 
+0

이 작품! 덕분에 너무 많이 :) –

0

희망 요 enum을 사용하는 일련의 옵션이 있습니다. 이렇게하면 더 깔끔한 캡슐화가 가능하며 다른 코드를 변경하지 않고도 새 항목을 추가하는 것이 훨씬 쉽습니다. "그 이름을 가진 테스트"를 의미 없음

Optional<Test> possibleTest = Test.getTestWithName(name); 
if (possibleTest.isPresent()) { 
    ... 
    int totalCost = possibleTest.get().getTotalCost(visitor); 
} else { 
    showMessageDialog(null, "Wrong input!"); 
} 

그것은 Optional 사용하지만 당신은 그냥 쉽게 (덜 선명하게하지만) null을 사용할 수

public enum Test { 
    TEST1("test1", 20), 
    TEST2("test2", 17), 
    TEST3("test3", 22); 

    private final String name; 
    private final int costPerVisitor; 

    private Test(String name, int costPerVisitor) { 
     this.name = name; 
     this.costPerVisitor = costPerVisitor; 
    } 

    public static Optional<Test> getTestWithName(String name) { 
     for (Test test: values()) { 
      if (test.name.equals(name)) 
       return Optional.of(test); 
     } 
     return Optional.empty(); 
    } 

    public int getTotalCost(int visitors) { 
     return visitors * costPerVisitor; 
    } 
} 

는 사용할 수 있습니다.

귀하의 경우 name 필드를 피하고 name().toLower()을 사용할 수 있습니다.

+0

작은 문제는, 그것은 사용자가 텍스트 필드에있는 단어를 입력 할 수 있습니다, 그것은 단지 그들이 test1, test2, test3 입력하게해야하고 잘못된 입력이 끝에 나타납니다, 그것은 사용자가 표시되어야합니다 test1,2,3 이외의 다른 것을 입력합니다. –

0

내가 때마다하는 것이 좋습니다로 작동

static class Action4 implements ActionListener { 

    @Override 
    public void actionPerformed(java.awt.event.ActionEvent e) { 

     String name = ((JTextField) e.getSource()).getText(); 

     switch(name) { 
      case "test1": 
       process(20); 
       break; 
      case "test2": 
       process(17); 
       break; 
      case "test3": 
       process(22); 
       break; 
      default: JOptionPane.showMessageDialog(null, "Wrong input!"); 
     } 
    } 

    public function getInput(int factor) { 

     name = JOptionPane.showInputDialog("Enter Name "); 

      String day; 
      int totalCost; 
      int visitors; 

      day = JOptionPane.showInputDialog("Enter what day you'd like to attend "); 

      visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting ")); 

      totalCost = visitors * factor; 

      JOptionPane.showMessageDialog(null, " You are attending the " + name + " On " + day + visitors + " attending " + "total cost " + totalCost); 
    } 
} 
관련 문제