2017-03-05 10 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

は別々のメソッドにコードを分割する方法を教えてスイッチケースを使用することができれば、私はそれを感謝するのと同じ出力でこのコードを書くための私のための代替方法があるので、もし私は、Javaに新しいです(Javaの> = 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を使用すると考えられる一連のオプションがあります。これにより、よりカプセル化が容易になり、他のコードを変更することなく新しいエントリを追加する方がはるかに簡単です。これを使用することができ

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を使用していますが、同じように簡単に(ただしあまり明快で)可能性

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

「その名前のテスト」を意味しないようにnullを使用しています。

あなたの場合、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); 
    } 
} 
関連する問題