【问题标题】:Bank Account TransferTo Method银行账户转帐方法
【发布时间】:2014-07-03 23:42:02
【问题描述】:

好的,我在创建将“钱”从一个帐户转移到另一个帐户的方法时遇到问题。转账后,每个账户的金额将被打印出来。我创建了代码,但是当我运行它时,转账金额设置为银行账户金额。它应该只是从一个帐户中扣除并添加到下一个帐户。我做错了什么?

这是我的类与构造函数和方法:

public class Account {
// Instance variables
private double balance;

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

public Account(int account, double balance) {
    this.balance = balance;
}

public Account() {
    balance = 0.0;
}

// Instance methods

public void setBalance() {
    balance = Math.random() * 1000;
    balance = Math.round((balance * 100.0)+.5) / 100.0;
}

public void deposit(double amount) {
    balance = balance + amount;
}

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

public double getBalance() {
    balance = Math.round((balance * 100.0)+.5) / 100.0;
    return balance;
}

public void close() {
    balance = 0.0;
}

public void transferTo(Account bank, double x) {
    if (x <= balance) {
        withdraw(x);
        bank.deposit(x);
        System.out.println("\nTransfer succesful. Tansfered: $" + bank.getBalance());
    } else if (x > balance) {
        System.out.println("\nTransfer failed, not enough balance!");
    }
}
}

这是我的主要方法的类

public class MyBank {

public static void main(String[] args) {

    Account[] bank = { new Account(), new Account(), new Account(),
            new Account() };

    bank[0].setBalance();
    bank[1].setBalance();
    bank[2].setBalance();
    bank[3].setBalance();

    System.out.println("Accounts 1 balance is: $" + bank[0].getBalance());
    System.out.println("Accounts 2 balance is: $" + bank[1].getBalance());
    System.out.println("Accounts 3 balance is: $" + bank[2].getBalance());
    System.out.println("Accounts 4 balance is: $" + bank[3].getBalance());

    double x = Math.random()*100;
    bank[0].transferTo(bank[1], x);
    System.out.println("Remaining balance of Account 1: $" + bank[0].getBalance());
    System.out.println("Remaining balance of Account 2: $" + bank[1].getBalance());

    double y = (Math.random()*300);
    bank[2].transferTo(bank[3], y);
    System.out.println("Remaining balance of Account 3: $" + bank[2].getBalance());
    System.out.println("Remaining balance of Account 4: $" + bank[3].getBalance());
}
}

【问题讨论】:

    标签: java methods transfer


    【解决方案1】:

    您确实遇到的一个问题是,您在 transferTo() 方法中不小心打印了 BALANCE 而不是实际转移的金额。我解决了这个问题。另一方面,如果您要打印这些数字,look into printf() 使它们显示为实际的美元金额。为了给你看一个例子,我做了其中的一行。

    PS - 尝试调试时尽量不要使用 Math.random()。

    public void transferTo(Account bank, double x) {
        if (x <= this.balance) {
            withdraw(x);
            bank.deposit(x);
            System.out.println("\nTransfer succesful. Tansfered: $" + x);
        } else { //does not need to be else if, because if not <=, it MUST be >.
            System.out.println("\nTransfer failed, not enough balance!");
        }
    }
    

    在这些编辑之后,我可以验证该方法在我的机器上运行良好。这是我用来测试的:

    public class MyBank {
    
    public static void main(String[] args) {
    
        Account[] bank = { new Account(), new Account(), new Account(),
                new Account() };
    
        bank[0].setBalance();
        bank[1].setBalance();
        bank[2].setBalance();
        bank[3].setBalance();
    
        double x = 100.00;
        System.out.printf("Transferring $%.2f from Acc 1 to Acc 2.\n", x); //printf example
        System.out.println("Acc 1 previous balance: " + bank[0].getBalance());
        System.out.println("Acc 2 previous balance: " + bank[1].getBalance());
        bank[0].transferTo(bank[1], x);
        
        
        System.out.println("Acc 1 new balance: " + bank[0].getBalance());
        System.out.println("Acc 2 new balance: " + bank[1].getBalance());
            
            
    }
    }
    

    最后,我的输出:

    将 100.00 美元从 Acc 1 转移到 Acc 2。

    Acc 1 之前的余额:106.76

    Acc 2 之前的余额:266.18

    传输成功。已转帐:$100.0

    Acc 1 新余额:6.77

    Acc 2 新余额:366.19

    【讨论】:

    • -1 这不是错误,也不会阻止代码工作。这纯粹是一个偏好问题。
    • @immibis 已编辑,感谢您的认可。意识到我错了。
    • x() 仍应为x
    • 谢谢!这非常有效。我知道这与我无法弄清楚的平衡有关。再次感谢您!
    • @Victor 没问题!如果您对我的答案感到满意,请考虑投票和/或将我的答案标记为正确。
    【解决方案2】:
    System.out.println("\nTransfer succesful. Tansfered: $" + bank.getBalance());
    

    这会打印另一个帐户的新余额,因为这是你告诉它的

    如果您想打印转账金额:

    System.out.println("\nTransfer succesful. Tansfered: $" + x);
    

    【讨论】:

      【解决方案3】:

      // Bank account class
      class BankAccount {
        constructor(fname, lname, account_number) {
          const minBalance = 10000;
          this.name = `${fname} ${lname}`;
          this.balance = minBalance;
          this.account_number = account_number;
        }
      
        details() {
          console.log(
            `Cleint name: ${this.name} \n Balance: ${this.balance} \n Account number: ${this.account_number}`
          );
        }
      
        isPositive(amount) {
          return amount > 0 ? true : false;
        }
      
        isVlaidAmount(amount) {
          return isNaN(amount) || !this.isPositive(amount) ? false : true;
        }
      
        deposit(amount) {
          if (this.isVlaidAmount(amount)) {
            this.balance += amount;
          } else {
            console.log("Sorry, this transcation cannot be completed!");
          }
        }
      
        withdraw(amount) {
          if (this.isVlaidAmount(amount)) {
            this.balance -= amount;
          } else {
            console.log("Sorry, you have insufficient funds.");
          }
        }
      
        transfer(amount, reciever) {
          if (this.isVlaidAmount(amount)) {
            this.withdraw(amount);
            reciever.deposit(amount);
          }
        }
      }
      
      // create objects
      let acc1 = new BankAccount("George", "Omara", 422);
      let acc2 = new BankAccount("Haward", "Walowets", 662);

      【讨论】:

      • 请解释问题以及您的答案如何解决问题。
      • 抱歉没有记录代码,有点像我的第一篇文章。下次我会做得更好。该代码是为了展示一种使“transferTo()”方法起作用的方法。但是在我的代码中,我将方法命名为“transfer”
      • 没问题。您可以使用描述编辑当前答案。
      猜你喜欢
      • 2014-08-27
      • 1970-01-01
      • 2019-07-22
      • 2020-10-26
      • 2015-09-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多