java如何计算

时间:2014-01-03 07:12:30

标签: java swing oop methods jframe

我如何计算余额?我想将提取的金额减去用户保存的输入金额。

这是我的代码:

AccountInformation.java

    public class AccountInformation
{
    private int acct_no;
    private String name;
    private String address;
    private String bday;;

    public void setAcct_No(int an)
    {
        this.acct_no = an;
    }
    public void setName(String n)
    {
        this.name = n;
    }
    public void setAddress(String ad)
    {
        this.address = ad;
    }
    public void setBday(String bd)
    {
        this.bday = bd;
    }

    public int getAcct_No()
    {
        return this.acct_no;
    }
    public String getName()
    {
        return this.name;
    }
    public String getAddress()
    {
        return this.address;
    }
    public String getBday()
    {
        return this.bday;
    }
}

SavingsAccount.java

    import javax.swing.*;
public class SavingsAccount extends AccountInformation
{   
    private int withdraw;
    private int balance;
    private int amount;

    public void setAmount(int am)
    {
        this.amount = am;
    }   
    public void setWithdraw(int w)
    {
        this.withdraw = w;
    }
    public void setBalance(int b)
    {
        this.balance = b;
    }

    public int getAmount()
    {
        return this.amount;
    }   
    public int getWithdraw()
    {
        return this.withdraw;
    }   
    public int getBalance()
    {
        return this.balance = amount - withdraw;
    }
}

CheckingAccount.java

    import javax.swing.*;

public class CheckingAccount extends AccountInformation
{
    private int issue_date;

    public void setIssue_Date(int id)
    {
        this.issue_date = id;
    }

    public int getIssue_Date()
    {
        return this.issue_date;
    }
}

BankAccount.java

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

public class BankAccount extends JFrame implements ActionListener
{
    private JLabel acct_noL, nameL, addressL, bdayL, amountL;
    private JTextField acct_noTF, nameTF, addressTF, bdayTF, amountTF;
    private JButton addB, checkB, clearB, exitB;

    SavingsAccount[] savings = new SavingsAccount[10];

    private int index = 0;
    private int acct_no = 1000;

    public BankAccount()
    {
        for(int ctr=0;ctr<=9;ctr++)
        {
            savings[ctr] = new SavingsAccount();
        }
        setTitle("Bank Account");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        acct_noL = new JLabel("Account Number",SwingConstants.RIGHT);
        nameL = new JLabel("Name",SwingConstants.RIGHT);
        addressL = new JLabel("Address",SwingConstants.RIGHT);
        bdayL = new JLabel("Birthday",SwingConstants.RIGHT);
        amountL = new JLabel("Amount to Deposit",SwingConstants.RIGHT);

        acct_noTF = new JTextField(10);
        nameTF = new JTextField(10);
        addressTF = new JTextField(10);
        bdayTF = new JTextField(10);
        amountTF = new JTextField(10);

        acct_noTF.setText("1000");

        addB = new JButton("Add");
        checkB = new JButton("Check Balance");
        clearB = new JButton("Clear");
        exitB = new JButton("Exit");

        addB.addActionListener(this);
        checkB.addActionListener(this);
        clearB.addActionListener(this);
        exitB.addActionListener(this);

        Container pane = getContentPane();
        pane.setLayout(new GridLayout(7,2));
        pane.add(acct_noL);
        pane.add(acct_noTF);
        pane.add(nameL);
        pane.add(nameTF);
        pane.add(addressL);
        pane.add(addressTF);
        pane.add(bdayL);
        pane.add(bdayTF);
        pane.add(amountL);
        pane.add(amountTF);
        pane.add(addB);
        pane.add(checkB);
        pane.add(clearB);
        pane.add(exitB);
    }

    private String showAccountInfo(int an)
    {
        String info="";

        for(int s=0;s<=9;s++)
        {
            if(savings[s].getAcct_No()==an)
            {
                info = "Account Number: " + savings[s].getAcct_No() + "\nName: " + savings[s]. getName() +
                         "\nAddress: " + savings[s].getAddress() + "\nBirthday " + savings[s].getBday() + 
                         "\nBalance: " + savings[s].getAmount();
            }
        }
        return info;
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        int inputAcct_No;
        int ewithdraw;
        int eissue_date;

        if(source==addB)
        {
            if(index<=9)
            {
                savings[index].setAcct_No(acct_no);
                savings[index].setName(nameTF.getText());
                savings[index].setAddress(addressTF.getText());
                savings[index].setBday(bdayTF.getText());
                savings[index].setAmount(Integer.parseInt(amountTF.getText()));
                index++;
                acct_no++;
                acct_noTF.setText(""+acct_no);
                nameTF.setText(null);
                addressTF.setText(null);
                bdayTF.setText(null);
                amountTF.setText(null);
                JOptionPane.showMessageDialog(null,"Bank Account Saved!");
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Bank Accounts are Full!");
            }
        }

        if(source==checkB)
        {
            SavingsAccount savings = new SavingsAccount();
            CheckingAccount checking = new CheckingAccount();
            inputAcct_No = Integer.parseInt(JOptionPane.showInputDialog("Enter Account Number"));
            JOptionPane.showMessageDialog(null,this.showAccountInfo(inputAcct_No));
            int confirm_withdraw = JOptionPane.showConfirmDialog(null,"Would you like to withdraw?");
            if(confirm_withdraw==JOptionPane.YES_OPTION)
            {
            savings.setWithdraw(Integer.parseInt(JOptionPane.showInputDialog("Enter amount to be withdrawn")));
            JOptionPane.showMessageDialog(null,"Your balance is: " + (savings.getBalance()));
            int confirm_issue_date = JOptionPane.showConfirmDialog(null,"Would you like to issue cheque?");
            if(confirm_issue_date==JOptionPane.YES_OPTION)
            {
                String Receiver = JOptionPane.showInputDialog(null,"Enter name of the receiver");
                checking.setIssue_Date(Integer.parseInt(JOptionPane.showInputDialog("Enter amount to be issued")));
                JOptionPane.showMessageDialog(null,"Cheque is issued to " + Receiver + " with the amount of " +checking.getIssue_Date() + 
                                                                "\nYour balance is: " + (savings.getBalance()-checking.getIssue_Date()));
            }
            }
            else if(confirm_withdraw==JOptionPane.NO_OPTION)
            {
            }
            else if(confirm_withdraw==JOptionPane.CANCEL_OPTION)
            {
            }
        }

        if(source==clearB)
        {
            nameTF.setText(null);
            addressTF.setText(null);
            bdayTF.setText(null);
            amountTF.setText(null);
        }

        if(source==exitB)
        {
            System.exit(0);
        }
    }

    public static void main(String[] args)
    {
        BankAccount ba = new BankAccount();
        ba.setSize(500,400);
        ba.setVisible(true);
        ba.setResizable(false);
        ba.setLocationRelativeTo(null);
    }
}

2 个答案:

答案 0 :(得分:1)

  

我想将提取金额减去已保存的用户输入金额。”

这是您的withdraw代码

public void setWithdraw(int w)
{
    this.withdraw = w;
}

除非您想要一个您没有的交易列表或其他内容,否则甚至不需要设置withdraw字段。因此,请从课程中取出withdraw字段。它不需要设置。并且做这样的事情

public void withdraw(int amount)
{
   balance -= amount;
}

您需要做的就是从余额中减去金额。对您的存款也一样(如果您决定添加存款方法。毕竟,如果您无法存款,该帐户是什么)。


修改

每次调用actionPerformed时,您都会创建一个新帐户

SavingsAccount Savings = new SavingsAccount();
CheckingAccount checking = new CheckingAccount();
不,那样做。这就是宽度牵伸量始终与余额相同的原因。而是将它们作为类成员。并从actionPerformed

中删除上述代码

<强>更新

尝试运行此非常简单程序。你会看到退出应该如何运作

import java.util.Scanner;


public class AccountTest {

    public static void main(String[] args) {
        Account account = new Account(1000.00);

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a Amount to withdraw: ");
        double amountToWithdraw = scanner.nextDouble();

        account.withDraw(amountToWithdraw);

        System.out.println("You balance is now " + account.getBalance());
    }
}

class Account{
    double balance;

    public Account(double initialBalance) {
        balance = initialBalance;
    }

    public double getBalance(){
        return balance;
    }

    public void withDraw(double amount) {
        balance -= amount;
    }
}

修改

这是一个建议。我看到你有一个SavingsAccount数组。所以你可能想要多个帐户。我也是你,如果按下checkB,你试图通过actionPerformed中的帐户ID访问特定帐户。那么你应该做什么,如果循环遍历数组并且如果找到了id,那么使用该帐户来完成你的工作。像这样的东西

inputAcct_No = Integer.parseInt(JOptionPane.showInputDialog("Enter Account Number"));
SavingsAccount savings;
for (SavingsAccount account : SavingsAccounts){  <-- your array
    if (acound.getId() == inputAcct_No){
        savings = account;
    }
}
// do something with savings

现在您可以对该帐户执行某些操作,因为它正在引用数组中的帐户。

答案 1 :(得分:0)

最好告诉你所面临的确切问题,很难检查完整的代码..

我得到的是,在您的 SavingsAccount 类getBalance()方法中,您只需减去并返回,但对余额的实际值没有影响

我认为你必须在你的setWithdraw()方法中取消平衡。