【问题标题】:How to transfer funds from one bank account to another in Java?如何在 Java 中将资金从一个银行账户转移到另一个银行账户?
【发布时间】:2017-03-21 12:52:55
【问题描述】:

任务: 更改 Account 类,以便可以将资金从一个帐户转移到另一个帐户。可以将其视为从一个帐户中提取资金并将其存入另一个帐户。更改 Banking 类的 main 方法以显示此新服务。

我正在开发一个可以从银行账户余额中存款和取款的银行账户类。我正在处理任务的类部分,您在其中声明驱动程序的所有方法。我的任务希望我制定一种方法,可以从一个帐户中提取资金并将该资金存入另一个帐户。我已经知道如何取款和存款,只是不知道如何将钱从一个账户转移到另一个账户。到目前为止,这是我的传输方法代码:

import java.text.NumberFormat;
public class Account
{
    private NumberFormat fmt = NumberFormat.getCurrencyInstance();
    private final double RATE = 0.035; // interest rate of 3.5%
    private long acctNumber;
    private double balance;
    private String name;
    //-----------------------------------------------------------------
    // Sets up the account by defining its owner, account number,
    // and initial balance.
    //-----------------------------------------------------------------
    public Account (String owner, long account, double initial)
    {
        name = owner;
        acctNumber = account;
        balance = initial;
    }
    //-----------------------------------------------------------------
    // Validates the transaction, then deposits the specified amount
    // into the account. Returns the new balance.
    //-----------------------------------------------------------------
    public double deposit (double amount)
    {
        if (amount < 0) // deposit value is negative
        {
            System.out.println ();
            System.out.println ("Error: Deposit amount is invalid.");
            System.out.println (acctNumber + " " + fmt.format(amount));
        }
        else
            balance = balance + amount;
        return balance;
    }
    //-----------------------------------------------------------------
    // Validates the transaction, then withdraws the specified amount
    // from the account. Returns the new balance.
    //-----------------------------------------------------------------
    public double withdraw (double amount, double fee)
    {
        amount += fee;
        if (amount < 0) // withdraw value is negative
        {
        System.out.println ();
        System.out.println ("Error: Withdraw amount is invalid.");
        System.out.println ("Account: " + acctNumber);
        System.out.println ("Requested: " + fmt.format(amount));
        }
        else
        if (amount > balance) // withdraw value exceeds balance
        {
        System.out.println ();
        System.out.println ("Error: Insufficient funds.");
        System.out.println ("Account: " + acctNumber);
        System.out.println ("Requested: " + fmt.format(amount));
        System.out.println ("Available: " + fmt.format(balance));
        }
        else
        balance = balance - amount;
        return balance;
    }

    public double transfer (double amount, double fee)
    {
        amount += fee;
        if (amount < 0) // withdraw value is negative
        {
            System.out.println ();
            System.out.println ("Error: Withdraw amount is invalid.");
            System.out.println ("Account: " + acctNumber);
            System.out.println ("Requested: " + fmt.format(amount));
        }
        else
        if (amount > balance) // withdraw value exceeds balance
        {
            System.out.println ();
            System.out.println ("Error: Insufficient funds.");
            System.out.println ("Account: " + acctNumber);
            System.out.println ("Requested: " + fmt.format(amount));
            System.out.println ("Available: " + fmt.format(balance));
        }
        else
            balance = balance - amount;

         //What should I put here to deposit the amount into another account?

        if (amount < 0) // deposit value is negative
        {
            System.out.println ();
            System.out.println ("Error: Deposit amount is invalid.");
            System.out.println (acctNumber + " " + fmt.format(amount));
        }
        else
            balance = balance + amount;
    }

    //-----------------------------------------------------------------
    // Adds interest to the account and returns the new balance.
    //-----------------------------------------------------------------
    public double addInterest ()
    {
        balance += (balance * RATE);
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns the current balance of the account.
    //-----------------------------------------------------------------
    public double getBalance ()
    {
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns the account number.
    //-----------------------------------------------------------------
    public long getAccountNumber ()
    {
        return acctNumber;
    }
    //-----------------------------------------------------------------
    // Returns a one-line description of the account as a string.
    //-----------------------------------------------------------------
    public String toString ()
    {
        return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
    }
}

【问题讨论】:

    标签: java string class


    【解决方案1】:

    您的转移方法没有任何目的。 Tranfer 应该是您的主类中实例化帐户的方法。转移的例子是。

    public static void main(String[] args)
    {
        // This creates two different accounts :)
        Account a = new Account("userA", 123, 200);
        Account b = new Account("userB", 234, 500);
    
        // Tranfer
        a.withdraw(100, 5);
        System.out.println(a.getBalance());
        b.deposit(100);
        System.out.println(b.getBalance());
    }
    

    把它变成一个方法就是

    public static void transfer (Account from, Account to, double amount, double fee)
    {
        from.withdraw(amount, fee);
        to.deposit(amount);
    }
    

    编辑

    如果我正确理解了您的第二个问题,您想创建一个默认帐户吗?如果我误解了,你能提供更多细节吗? (链接到作业或其他东西)

    你需要为它写6个新方法,你还没有,

    1. getOwner
    2. setOwner
    3. getAcctNumber
    4. setAcctNumber
    5. 获取余额
    6. 设置平衡

    帐户类别

    import java.text.NumberFormat;
    public class Account
    {
        private NumberFormat fmt = NumberFormat.getCurrencyInstance();
        private final double RATE = 0.035; // interest rate of 3.5%
        private long acctNumber;
        private double balance;
        private String name;
        //-----------------------------------------------------------------
        // Sets up the account by defining its owner, account number,
        // and initial balance.
        //-----------------------------------------------------------------
        public Account (String owner, long account, double initial)
        {
            name = owner;
            acctNumber = account;
            balance = initial;
        }
        public Account()
        {
            // This would be the default constructor and default account
            name ="N/A";
            acctNumber = 0;
            balance = 0.0;
        }
    
        //-----------------------------------------------------------------
        // Validates the transaction, then deposits the specified amount
        // into the account. Returns the new balance.
        //-----------------------------------------------------------------
        public double deposit (double amount)
        {
            if (amount < 0) // deposit value is negative
            {
                System.out.println ();
                System.out.println ("Error: Deposit amount is invalid.");
                System.out.println (acctNumber + " " + fmt.format(amount));
            }
            else
                balance = balance + amount;
            return balance;
        }
        //-----------------------------------------------------------------
        // Validates the transaction, then withdraws the specified amount
        // from the account. Returns the new balance.
        //-----------------------------------------------------------------
        public double withdraw (double amount, double fee)
        {
            amount += fee;
            if (amount < 0) // withdraw value is negative
            {
            System.out.println ();
            System.out.println ("Error: Withdraw amount is invalid.");
            System.out.println ("Account: " + acctNumber);
            System.out.println ("Requested: " + fmt.format(amount));
            }
            else
            if (amount > balance) // withdraw value exceeds balance
            {
            System.out.println ();
            System.out.println ("Error: Insufficient funds.");
            System.out.println ("Account: " + acctNumber);
            System.out.println ("Requested: " + fmt.format(amount));
            System.out.println ("Available: " + fmt.format(balance));
            }
            else
            balance = balance - amount;
            return balance;
        }
    
        //-----------------------------------------------------------------
        // Adds interest to the account and returns the new balance.
        //-----------------------------------------------------------------
        public double addInterest ()
        {
            balance += (balance * RATE);
            return balance;
        }
        //-----------------------------------------------------------------
        // Returns the current balance of the account.
        //-----------------------------------------------------------------
        public double getBalance ()
        {
            return balance;
        }
        //-----------------------------------------------------------------
        // Returns the account number.
        //-----------------------------------------------------------------
        public long getAccountNumber ()
        {
            return acctNumber;
        }
        //-----------------------------------------------------------------
        // Returns a one-line description of the account as a string.
        //-----------------------------------------------------------------
        public String toString ()
        {
            return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
        }
    }
    

    Drvier 类

    public class test
    {
        public static void main(String[] args)
        {
            // Created here
            Account defaultAccount = new Account();
            Account a = new Account("userA", 123, 200);
            Account b = new Account("userB", 234, 500);
    
            System.out.println(defaultAccount.getBalance());
            // Tranfer
            a.withdraw(100, 5);
            System.out.println(a.getBalance());
            b.deposit(100);
            System.out.println(b.getBalance());
        }
    
        public static void transfer (Account from, Account to, double amount, double fee)
        {
            from.withdraw(amount, fee);
            to.deposit(amount);
        }
    }
    

    【讨论】:

    • 正如我所提到的,我只需要知道如何将资金从一个帐户转移到另一个帐户。我已经知道我的代码不适用于我的任务,我需要知道在提款和存款之间放什么
    • @Nub 回答了这个问题,请参阅上面的回答。检查提现是否成功,然后是否将钱添加到另一个帐户。
    • 谢谢,如何在Account方法中用相同的变量声明变量balanceAccountA和balanceAccountB?我还更新了代码以显示我的整个程序。很抱歉不清楚
    • 非常感谢。我很抱歉我的不清楚。
    • @Nub Transfer 不是帐户类的一部分。
    【解决方案2】:

    创建一个方法 transfer(String ID, double amount),利用 deposit(String Id, double amt) 和提款(double amount) 方法来转移资金。

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2011-01-28
      • 2014-12-09
      • 2018-04-16
      • 1970-01-01
      • 2020-10-26
      • 2014-08-27
      • 2012-03-01
      • 2015-09-08
      相关资源
      最近更新 更多