2016-11-21 9 views
0

私は、銀行口座のGUIプログラムを作成しています。これにより、ユーザーは小切手と普通預金に金額を振り込んだり、引き出したり、入金したり、それぞれの残高を確認することができます。残高と入金方法はうまくいきます。銀行プログラムの転送方法と引き出しメソッドのトラブル

転送方法では、入力可能な限りテキストフィールドに入力した金額を転送することができます。そうでない場合は、例外がスローされます。問題は十分であっても、例外がスローされているということです。

私の引き出し方法は、ユーザーが$ 20単位でお金を引き出すことができます。それは動作しますが、十分な資金がないというメッセージを表示し続けます。また、ユーザーが両方のアカウントで合計4回の引き出しを行った後、$ 1.50を請求することになっています。

バンキングクラス

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Banking extends JPanel 
{ 
    private JFrame frame; 
    private JPanel panel; 
    private JButton withdraw; 
    private JButton deposit; 
    private JButton transfer; 
    private JButton balance; 
    private JRadioButton checking; 
    private JRadioButton savings; 
    private JTextField input; 
    private Account checkingAccount; 
    private Account savingsAccount; 
    private Account currentAccount; 
    private double amount; 

    public Banking(Account checkingAccount,Account savingsAccount) 
    { 

     frame=new JFrame("ATM"); 
     panel=new JPanel(); 
     withdraw=new JButton("Withdraw"); 
     deposit=new JButton("Deposit"); 
     transfer=new JButton("Transfer"); 
     balance=new JButton("Balance"); 
     checking=new JRadioButton("Checking"); 
     savings=new JRadioButton("Savings"); 
     input=new JTextField(""); 
     this.checkingAccount=checkingAccount; 
     this.savingsAccount=savingsAccount; 

     panel.setLayout(new GridLayout(4,2)); 

     panel.add(withdraw);panel.add(deposit); 
     panel.add(transfer);panel.add(balance); 
     panel.add(checking);panel.add(savings); 
     panel.add(input); 

     frame.add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setSize(600,300); 
     frame.setVisible(true); 


     checking.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e) 
      { 
       if(checking.isSelected()) 
       { 
        currentAccount=checkingAccount; 
        savings.setSelected(false); 
       } 
      } 
     }); 

     savings.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e) 
      { 
       if(savings.isSelected()) 
       { 
        currentAccount=savingsAccount; 
        checking.setSelected(false); 
       } 
      } 
     }); 


     withdraw.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e) 
      { 


      try 
      { 
       amount=Double.parseDouble(input.getText()); 
       if(amount%20==0) 
       { 
        currentAccount.withdraw(amount); 
        JOptionPane.showMessageDialog(null, "You've withdrawn $"+amount); 


       } 
       else 
       { 
        JOptionPane.showMessageDialog(null, "You can only withdraw money in increments of $20"); 

       } 
      } 
      catch(NumberFormatException a) 
      { 
       JOptionPane.showMessageDialog(null, "Please enter a numerical number"); 
      } 
      catch (InsufficientFunds e1) 
      { 
       JOptionPane.showMessageDialog(null, "Not enough funds"); 

      } 

      } 
     }); 


    transfer.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e) 
     { 
      if(currentAccount.equals(savingsAccount)) 
      { 

       try 
       { 
        currentAccount.transferTo(savingsAccount, amount); 
       } 
       catch(NumberFormatException a) 
       { 
        JOptionPane.showMessageDialog(null, "Please enter a numerical number"); 
       } 
       catch (InsufficientFunds e1) 
       { 
        JOptionPane.showMessageDialog(null, "Not enough funds"); 

       } 
      } 
      else 
      { 
       try 
       { 
        currentAccount.transferTo(checkingAccount, amount); 
       } 
       catch(NumberFormatException a) 
       { 
        JOptionPane.showMessageDialog(null, "Please enter a numerical number"); 
       } 
       catch (InsufficientFunds e1) 
       { 
        JOptionPane.showMessageDialog(null, "Not enough funds"); 
      } 
    } 
     } 
    }); 

    deposit.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e) 
     { 
      try 
      { 
       amount=Double.parseDouble(input.getText()); 
       currentAccount.deposit(amount); 
       JOptionPane.showMessageDialog(null, "You've deposited $"+amount); 


      } 
      catch(NumberFormatException a) 
      { 
       JOptionPane.showMessageDialog(null, "Please enter a numerical number"); 
      } 
     } 
    }); 


    balance.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e) 
     { 

      JOptionPane.showMessageDialog(null, "You're balance is $ "+currentAccount.getBalance()); 

     } 
    }); 


} 
    public static void main(String[] args) 
    { 
     Account checking=new Account(3000.00); 
     Account savings=new Account(5000.00); 
     Banking myBank=new Banking(checking,savings); 
    } 
} 

Accountクラス

public class Account 
{ 
private double balance; 
static int counter=0; 

public Account(double balance) 
{ 
    this.balance=balance; 
} 

public void withdraw(double amount) throws InsufficientFunds 
{ 
    int counter=0; 

    if(amount<=balance) 
    { 
     balance=balance-amount; 
     counter++; 

     if(counter>4) 
     { 
      if(balance<1.50) 
      { 
       throw new InsufficientFunds(1.50); 
      } 
      else 
      { 
       balance=balance-1.50; 
      } 
     } 

    else 
    { 
     double needed=amount-balance; 
     throw new InsufficientFunds(needed); 
    } 
    } 


} 

public double getBalance() 
{ 
    return balance; 
} 

public void deposit(double amount) 
{ 
    balance=balance+amount; 
} 

public void transferTo(Account bank, double amount) throws InsufficientFunds 
{ 
    if(amount<=balance) 
    { 
     withdraw(amount); 
     bank.deposit(amount); 
    } 
    else 
    { 
     throw new InsufficientFunds(amount); 
    } 
} 
} 

不十分な資金クラス

import java.io.*; 
public class InsufficientFunds extends Exception 
{ 
private double amount; 

public InsufficientFunds(double amount) 
{ 
     this.amount = amount; 
} 

public double getAmount() 
{ 
     return amount; 
} 
} 
+0

引き出し額を控除する前に1.50の手数料のための十分なお金を持っているかどうかを確認する必要があります。 .. then ... else'中カッコの問題。 'withdraw()'では、 'else throw InsufficientFunds()'は外側 'account <= balance'でインデントされていますが、実際には' counter> 4'とペアになっています。さらに、 'counter 'はクラスメンバではなくローカル変数なので正しく動作しません。 –

+0

ユーザーが$ 1.50の料金を支払うのに十分なお金がない場合、私は例外をスローしようとしていました。また、静的変数はクラスメンバーであると思いましたか? – WILLO567

+0

ありがとう、その問題を修正しました – WILLO567

答えて

1

私はあなたが説明したものから2つの問題を見ることができ、

* Insidあなたはインクリメントカウンタ++の後にカウンタを常に1にする新しいカウンタを宣言しています。

public void withdraw(double amount) throws InsufficientFunds{ 
    int counter = 0; // should be removed .. 
} 

カウンタが4より大きい場合**あなたは既にアカウントが、私はあなたが `場合があると思う

if(amount<=balance) 
    { 

    if(counter>4) 
    { 

     if(balance<(amount+1.50)) 
     { 
      throw new InsufficientFunds((amount+1.50)-balance); 
     } 
     else 
     { 
      balance=balance-amount-1.50; 
      counter++; 
     } 

    }else{ 

    balance=balance-amount; 
    counter++; 
    } 
} 
+0

私は実際にこれを修正しましたが、もう1つの問題が発生しました。私がクリックしたときに私の転送ボタンがまったく機能しませんでした – WILLO567

+0

* "しかしその後もう一度問題が発生しました"もう1つの質問スレッドでもう1つの質問。 SOはQ&Aサイトであり、ヘルプデスクではありません。問題の解決に役立つ場合は、[回答を受け入れてください](http://meta.stackexchange.com/a/5235/155831)してください。 –

関連する問題