java 如何在Java中将资金从一个银行账户转移到另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40477719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:15:02  来源:igfitidea点击:

How to transfer funds from one bank account to another in Java?

javastringclass

提问by Nub

Assignment: Change the Account class so that funds can be moved form one account to another. Think of this as withdrawing money from one account and depositing it into another. Change the main method of Banking class to show this new service.

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

I am working on a bank account class that can deposit and withdraw money from bank account balances. I am working on the class part of the the assignment where you declare all the methods for the driver. My assignment wants me to make a method that will withdraw money from one account and deposit that money into another account. I already know how to withdraw and deposit, I just don't know how to transfer money from one account to another account. Here is my code for the transfer method so far:

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

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));
    }
}

回答by JordanGS

There is no purpose to your transfer method. Tranfershould be a method in your main class which instantiates the accounts. Example of a transfer would be.

您的传输方法没有任何目的。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());
}

and turning this into a method would be

并将其转化为一种方法将是

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

EDIT

编辑

If i understood your second question correctly, you want to create a default account? If i misunderstood, can you provide more details? (link to the assignment or something)

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

You need to write 6 new methods for it, you don't already have them,

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

  1. getOwner
  2. setOwner
  3. getAcctNumber
  4. setAcctNumber
  5. getBalance
  6. setBalance
  1. 获取所有者
  2. 集合所有者
  3. 获取账号
  4. 设置帐户编号
  5. 获取余额
  6. 设置余额

Account CLass

账户类别

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 Class

司机班

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);
    }
}

回答by Paul Hayes

Create a method transfer(String ID, double amount) that utilises the deposit(String Id, double amt) and withdrawal(double amount) methods to transfer the funds.

创建一个方法 transfer(String ID, double amount) 使用存款(字符串 ID,双金额)和提款(双金额)方法来转移资金。