作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个银行 GUI 程序,该程序允许用户将钱转帐、提取或存入支票和储蓄帐户,并允许他们检查每个帐户的余额。余额和存款方式运行良好。
只要可用,转账方法应允许用户转账在文本字段中输入的金额。如果不是,则会抛出异常。问题是,即使有足够的数量,仍然会抛出异常。
至于我的提款方法,它允许用户以 20 美元为增量提款。它有效,但它不断显示一条消息,提示资金不足。当用户为两个帐户总共提款四次后,它还应该收取 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);
}
}
账户类别
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;
}
}
最佳答案
从你的解释中我只能看到两个问题,
*在withdraw方法中,您声明了一个新的计数器,这将使计数器在递增counter++后始终为1。
public void withdraw(double amount) throws InsufficientFunds{
int counter = 0; // should be removed ..
}
**如果计数器大于4,您应该在扣除提款金额之前检查账户是否有足够的资金支付1.50的费用
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++;
}
}
关于java - 银行计划的转账和提款方式出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40712379/
我已经被困了几个小时,无法找出正确的方法来做到这一点。所以基本上我知道循环并不是一个很好的做法,因为如果我们有很多用户,系统就会开始计时。所以我的想法是选择正确的方式来获得我需要的东西。 这是我们的核
我是一名优秀的程序员,十分优秀!