2016-11-08 13 views
-3

割当: 口座間で資金を移動できるように口座クラスを変更します。これを1つの口座から引き出して別の口座に預け入れるものと考えてください。 Bankingクラスのメインメソッドを変更して、この新しいサービスを表示します。Javaで1つの銀行口座から別の銀行口座に資金を振り替えるにはどうすればいいですか?

私は銀行口座残高から入金したり、引き出したりできる銀行口座クラスに取り組んでいます。私はあなたがドライバのすべてのメソッドを宣言する、割り当てのクラスの部分に取り組んでいます。私の任務は、私がある口座からお金を引き出し、その口座に別の口座に入金する方法を作ることを望んでいます。私はすでに引出しと預金の方法を知っている、私はちょうど1つのアカウントから別のアカウントにお金を転送する方法を知らない。これまでの転送方法のコードは次のとおりです。

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

転送方法の目的はありません。 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()); 
} 

と私はあなたの2番目の質問が正しく、あなたがデフォルトのアカウントを作成したい理解している場合、メソッドにこれを回すことは

public static void transfer (Account from, Account to, double amount, double fee) 
{ 
    from.withdraw(amount, fee); 
    to.deposit(amount); 
} 

EDIT

でしょうか?私が誤解した場合、詳細をお知らせください。 (譲渡か何かへのリンク)

あなたは

  1. 、あなたはすでにそれを持っていない、それのための6つの新しいメソッドを記述する必要がgetOwnerメソッド
  2. SETOWNER
  3. getAcctNumber
  4. setAcctNumber
  5. getBalance
  6. setBalance

Accountクラス

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

Drvierクラス

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

私が述べたように、私は別の口座からお金を転送する方法を知っておく必要があります。私はすでに私のコードは私の仕事のために働くつもりではないことを知っている、私は預金と預金の間に何を入れるべきかを知る必要があります – Nub

+0

@ナブこれは、上記の私の答えを参照してください。引き出しが成功したかどうかを確認し、それが他のアカウントに金額を追加していたかどうかを確認します。 – JordanGS

+0

ありがとうございます。アカウントメソッドで変数balanceAccountAとbalanceAccountBを同じ変数で宣言するにはどうすればよいですか?私はプログラム全体を表示するようにコードを更新しました。 unclarityのために申し訳ありません – Nub

関連する問題