Java 我想创建一个银行账户转账程序。但是卡住了

Java 我想创建一个银行账户转账程序。但是卡住了,java,Java,我想做一个银行账户转账程序。这就像一个账户转移到另一个账户,金额变化打印出来。我在多个帐户之间执行此程序。到目前为止,我写了一个方法,它接受要转账的金额、要转账的账户和从哪里转账的账户;作为参数。我在main()中提供设置这些金额的值,account1(要转账),account(从转账)。异常出现在Main()中,我正在传递account1和account2,其中在方法中它是唯一的Account,我现在不知道如何编写一个接受任何Account和更少代码的Account类。有人能给我写一个工作代码

我想做一个银行账户转账程序。这就像一个账户转移到另一个账户,金额变化打印出来。我在多个帐户之间执行此程序。到目前为止,我写了一个方法,它接受要转账的金额、要转账的账户和从哪里转账的账户;作为参数。我在main()中提供设置这些金额的值,account1(要转账),account(从转账)。异常出现在Main()中,我正在传递account1和account2,其中在方法中它是唯一的Account,我现在不知道如何编写一个接受任何Account和更少代码的Account类。有人能给我写一个工作代码吗

我的代码片段是

public class BankHandler {
    public void transfer(int amount, Account debit, Account credit) {

    }

    public static void main(String a[]) {
        BankHandler bank = new BankHandler();

        Account1 acc1 = new Account1(); //I don't understand how to make multiple accounts to pass. The method transfer() accepts Account type classes only.
        Account2 acc2 = new Account2();
        Account3 acc3 = new Account3();

        bank.transfer(1000, acc1, acc2);
        bank.transfer(2000, acc3, acc2);
        bank.transfer(3000, acc1, acc3);
    }
}

我是初学者。

不应该有课程
Account1
,或
Account2
,或
Account3
。只有一个类
帐户

要创建三个
Account
类实例,请执行以下操作

Account acc1 = new Account();
Account acc2 = new Account();
Account acc3 = new Account();

哈哈,我让这个代码起作用了

    package threadDemo;

    class Account {

private int balance;

// constructor
public Account(int initBalance) {
    // need to ensure that the initial balance is NOT negative
    if(initBalance < 0) {
        initBalance = 0;
    }

    balance = initBalance;
}

// to deposit amount
public int deposit(int amount) {
    balance = balance + amount;
    return balance;
}

// to withdraw amount
public int withdraw(int amount) {
    balance = balance - amount;
    return balance;

}

// returns the balance of the account
public int getBalance() {
    return balance;
}

// sets a new value to balance
public void setBalance(int bal) {
    // to ensure we don't set negative balance
    if(bal < 0) {
        bal = 0;
    }
    balance = bal;
}

    }

    public class BankHandler {
public void transfer(int amount, Account debit, Account credit) {
    debit.withdraw(amount); // removes amount from debit account
    credit.deposit(amount); // adds amount to credit amount
}

public static void main(String a[]) {
    BankHandler bank = new BankHandler();

    // All the bank accounts have been initialized with 5000
    Account acc1 = new Account(5000);
    Account acc2 = new Account(5000);
    Account acc3 = new Account(5000);

    bank.transfer(1000, acc1, acc2);
    bank.transfer(2000, acc3, acc2);
    bank.transfer(3000, acc1, acc3);

    System.out.println("Balance in Account 1 "+acc1.getBalance());
    System.out.println("Balance in Account 2 "+acc2.getBalance());
    System.out.println("Balance in Account 3 "+acc3.getBalance());


    }
    }
package-threadDemo;
类别帐户{
私人国际收支平衡;
//建造师
公共账户(国际账户余额){
//需要确保初始余额不为负值
如果(初始余额<0){
初始平衡=0;
}
平衡=初始平衡;
}
//存款
公共整数存款(整数金额){
余额=余额+金额;
收益余额;
}
//取款
公共整数取款(整数金额){
余额=余额-金额;
收益余额;
}
//返回帐户的余额
公共int getBalance(){
收益余额;
}
//设置要平衡的新值
公共资产净值(国际余额){
//以确保我们不会设置负平衡
if(bal<0){
bal=0;
}
余额=余额;
}
}
公营银行经办人{
公共无效转账(整数金额、账户借方、账户贷方){
借记.取款(金额);//从借记帐户中删除金额
贷方。存款(金额);//将金额添加到贷方金额
}
公共静态void main(字符串a[]{
BankHandler bank=新的BankHandler();
//所有银行账户都已初始化为5000
账户acc1=新账户(5000);
账户acc2=新账户(5000);
账户acc3=新账户(5000);
银行转账(1000,acc1,acc2);
银行转账(2000,acc3,acc2);
银行转账(3000,acc1,acc3);
System.out.println(“账户1中的余额”+acc1.getBalance());
System.out.println(“账户2中的余额”+acc2.getBalance());
System.out.println(“账户3中的余额+acc3.getBalance());
}
}

使用一个通用的
账户
接口,这三个类都实现了。好的,我的目标是从一个账户中扣除一笔金额,然后加入另一个账户。我应该在Account()构造函数中传递什么?@SharmisthaSakar您需要在
Account
类中定义一些方法来存入或提取帐户。也许初始余额应该在构造函数中传递,或者可以使用存款方法设置初始余额。无论如何,StackOverflow不是我们为初学者提供教程的网站,也不是我们“为您编写工作代码”的网站。我认为这是我们能给你的最多的了,你需要为初学者准备一本Java书籍,并完成它,或者在线寻找Java教程。