首页 > 解决方案 > 为什么我的工作没有显示我的转移工作

问题描述

文件 Account.java 包含一个简单银行帐户类的定义,该类具有提款、存款、获取余额和帐号以及打印摘要的方法。编写以下附加代码:

  1. 添加方法

    public void transfer(Account acct, double amount) 
    

到 Account 类,该类允许用户将资金从一个银行账户转移到另一个银行账户。如果 acct1 和 acct2 是 Account 对象,那么方法调用 acct1.transfer(acct2,957.80) 应该将 $957.80 从 acct1 转移到 acct2。一定要清楚地记录转移的方式!

  1. 编写一个带有 main 方法的类 TransferTest,该方法创建两个银行帐户对象并进入执行以下操作的循环:

询问用户是想从 account1 转移到 account2,从 account2 转移到 account1,还是退出。如果选择转账,询问转账金额,执行操作,并打印每个账户的新余额。重复直到用户要求退出,然后打印每个帐户的摘要。

  1. 向 Account 类添加一个静态方法,该方法允许用户在两​​个帐户之间转移资金,而无需通过任何一个帐户。您可以(并且应该)像调用另一个方法一样调用方法转移 - 您正在重载此方法。您的新方法应采用两个 Account 对象和一个金额,并将金额从第一个帐户转移到第二个帐户。签名将如下所示:

     public static void transfer(Account acct1, Account acct2, double amount)
    

修改您的 TransferTest 类以使用静态传输而不是实例版本。

public class Account {
    //instance variables
    private long accountNum; //bank account number
    private String name; //name
    private double balance; //balance
     
    
    //Constructor
    public Account () {
        accountNum = 0;
        name = "Nobody";
        balance = 0.0;
    }
     
    public Account (long accountNum, String name, double balance) {
        this.accountNum = accountNum;
        this.name = name;
        this.balance = balance;
    }
    
    //Accessor (get) methods
    public long getAccountNum() {
        return accountNum;
    }
    
    public String getName() {
        return name;
    }
    
    public double getBalance() {
        return balance;
    }

    //Mutator (set) methods
    public void setAccountNum (long newAccountNum) {
        this.accountNum = newAccountNum;
    }
    
    public void setName (String newName)
    {
        this.name = newName;
    }

    public void deposit (double amount) {
        if (amount >= 0) //enough money to withdraw 
         this.balance += amount;
    }

    public void withdraw (double amount) {
        if (amount >= 0 && balance >= amount)
         this.balance -= amount;
        
        else {
            System.out.println("Insufficient payment"); 
        }
        
    }
    
    public void transfer(Account acct, double amount) {
        //amount= 957.80; 

         if(this.balance >= amount) {
            acct.balance += amount; 
            this.balance-=amount; 
            System.out.println(amount + "successfully transfered from account1 to account 2" );
        }
    }
    
    public String toString() {
        return "Account Number: " + this.accountNum + "\nAccount Name: " + this.name + 
                "\nBalance: " + this.balance;
    }
    
    public boolean equals(Account otherAccount) {
        return this.accountNum == otherAccount.accountNum && 
                this.name.equalsIgnoreCase(otherAccount.name) &&
                (Math.abs(this.balance - otherAccount.balance) <= 0.001);
                
        
    }
    
    
    //Transfer from acct1 to acct2
    public static void transfer(Account acct1, Account acct2, double amount) {
        acct1.transfer(acct2, amount);
    }

    

    
}

标签: java

解决方案


    String confirm; 
    boolean done = false; 
    double amount1 = 0; 
    int choice; 
    
    while (true) {
        System.out.println("Would you like to: " + "\n (1) Transfer money from account # "+acct1.getAccountNum()+
                " to account # " +acct2.getAccountNum()+ "\n (2) Transfer money from account # " +acct2.getAccountNum()+
                " to account # "+ acct1.getAccountNum()+ "\n (3) Quit");
        if(scan.hasNextInt()) {
            choice= scan.nextInt(); 
            scan.nextLine(); 
            if (choice==1|| choice ==2) {
                System.out.println("How much amount would you like to transfer? " ); 
                amount1=scan.nextDouble(); 
                scan.nextLine(); 
                }
            if(choice==1) {
                Account.transfer(acct1, acct2, amount1);
            }
            else if (choice==2) {
                acct2.transfer(acct1, amount1);
            }
            else {
                System.out.println("Quit \n Bye have a great day ");
            }
        }
    }
    
}

}


推荐阅读