当前位置: 首页 > 知识库问答 >
问题:

如何在爪哇将资金从一个银行账户转移到另一个银行账户?

赫连俊雄
2023-03-14

我正在开发一个银行账户类,可以从银行账户余额中存取款。我正在处理分配的类部分,在那里你声明了驱动程序的所有方法。我的任务是让我做一个方法,将钱从一个帐户,并存入另一个帐户。我已经知道如何取款和存钱了,只是不知道如何把钱从一个账户转到另一个账户。以下是到目前为止传输方法的代码:

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

共有1个答案

姬凡
2023-03-14

您的转移方法没有目的。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);
}

编辑

    null
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));
    }
}
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);
    }
}
 类似资料:
  • 我目前正在开发一个应用程序,用户A可以从用户B那里购买东西。 我想做的是让用户A用PayPal付款。然后在后端,我增加用户B在我的数据库中的帐户余额,然后允许用户B将这笔钱提取到他的银行帐户。这是我的问题,是否可以将钱从PayPal转移到各种银行账户?例如,用户B通过表单发送他的银行帐号,并在后端进行适当的API调用以转移资金。 如果没有使用PayPal,那么我如何执行这种类型的转移,可能是使用其

  • 我们不允许寻求书籍、工具、软件库等推荐的问题。你可以编辑这个问题,以便用事实和引用来回答。 有没有任何API可以让我们自动将钱转入其他银行账户?我只发现我无法使用PayPal API完成。 我在一个银行帐户上有用户资金,在应用程序中执行一些操作后,我需要将此资金转移到其他用户银行帐户。 总结一下,一步一步: > 用户1向申请银行账户汇款。 几个小时后,用户1通过单击按钮确认应用程序的某些操作。 在

  • 是否可以通过某种服务向欧洲或其他地方的银行账户付款?(我想找到双向运营服务——这样我就可以从用户银行账户(信用卡凭证)支付到服务和!!从服务到银行账户。 到目前为止,我发现Stripe只能向美国银行账户汇款,而PayPal只能向另一个PayPal账户汇款,而不是银行账户。。。

  • 本文向大家介绍Python实现银行账户资金交易管理系统,包括了Python实现银行账户资金交易管理系统的使用技巧和注意事项,需要的朋友参考一下 用类和对象实现一个银行账户的资金交易管理, 包括存款、取款和打印交易详情, 交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。 如: 下面按照要求定义一个账户 Account 类。账户 Account 类的属性: 1. 当前账户金额  

  • 我需要找到向银行账户自动付款的最佳解决方案。我的场景是,我们有一个银行账户的资金(如果需要,可以注册一个像贝宝或条纹提供商帐户)。 当用户提交资金请求时,我需要从我们的银行帐户(或支付提供商帐户)自动将该金额发送到他们的银行帐户。有没有办法实现自动化?它需要能够支持大多数欧洲银行账户。

  • 我对以下语法有一些问题。 我目前正在学习Java,并一直在复习过去的试卷,以帮助我积累Java知识。 问题是: 编写一个类帐户,其中包含帐户编号和当前余额的实例变量。实现一个构造函数和方法getAccountNumber()、getBalance()、借记(双倍金额)和贷记(双倍金额)。在执行借记和贷记时,请检查指定的金额是否为正数,并且在借记方法中不会导致透支。在这些情况下返回false。否则,