为什么我的工作不显示我的调动工作

时间:2020-09-14 01:58:47

标签: java

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

  1. 添加方法

    public void transfer(Account acct, double amount) 
    

到Account类,该类允许用户将资金从一个银行帐户转移到另一个银行帐户。如果acct1和acct2是Account对象,则方法acct1.transfer(acct2,957.80)调用应将957.80美元从acct1转移到acct2。确保清楚地记录转移的方向!

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

询问用户是否要从帐户1转移到帐户2,从帐户2转移到帐户1或退出。 如果选择了转账,则询问转账金额,执行操作,并打印每个帐户的新余额。 重复该操作,直到用户要求退出为止,然后为每个帐户打印摘要。

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

    

    
}

1 个答案:

答案 0 :(得分:-2)

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

}

相关问题