2017-03-05 63 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()

相关问题