2017-03-05 59 views
0

我試圖減少代碼,使我的一切看起來更乾淨,更好,但我不知道如何更改此代碼,以便功能保持不變,但代碼少。我也是新來的Java,所以如果有適合我的替代方法寫有相同的輸出這個代碼,我將不勝感激,如果有人能告訴我我可以使用相同的結果來減少此代碼嗎?

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

如何分割你的代碼放到不同的方法,並使用一個開關的情況下(如果用Java代碼> = 7):

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); 
    } 
} 
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

希望這個作品是你的

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

小問題,它讓用戶在文本框中輸入任何單詞,它應該只讓他們進入test1,test2,test3,並在最後顯示錯誤的輸入,它應該顯示出來,如果用戶輸入除test1,2,3以外的任何內容 –

0

我建議,只要喲你有一系列的選項你考慮使用enum。這樣可以實現更簡潔的封裝,並且無需更改任何其他代碼就可以輕鬆添加新條目。

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; 
    } 
} 

這可以通過使用:

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

它採用Optional但你可以很容易(雖然不太清晰)使用null表示「這個名字沒有測試」。

請注意,在你的情況下,你可以完全避免name字段,並使用name().toLower()