割当: 口座間で資金を移動できるように口座クラスを変更します。これを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));
}
}
私が述べたように、私は別の口座からお金を転送する方法を知っておく必要があります。私はすでに私のコードは私の仕事のために働くつもりではないことを知っている、私は預金と預金の間に何を入れるべきかを知る必要があります – Nub
@ナブこれは、上記の私の答えを参照してください。引き出しが成功したかどうかを確認し、それが他のアカウントに金額を追加していたかどうかを確認します。 – JordanGS
ありがとうございます。アカウントメソッドで変数balanceAccountAとbalanceAccountBを同じ変数で宣言するにはどうすればよいですか?私はプログラム全体を表示するようにコードを更新しました。 unclarityのために申し訳ありません – Nub