如何在Java中将资金从一个银行帐户转移到另一个银行帐户?
任务: 更改帐户类别,以便资金可以从一个帐户转移到另一个帐户。可以把这看作是从一个账户取钱并存入另一个账户。更改Banking类的主要方法以显示此新服务 我正在开发一个银行账户类,可以从银行账户余额中存款和取款。我正在处理作业的类部分,在那个部分中,您将为驱动程序声明所有方法。我的任务要求我制定一种方法,从一个账户中提取资金,然后将这些资金存入另一个账户。我已经知道如何取款和存款,我只是不知道如何把钱从一个帐户转到另一个帐户。以下是迄今为止我的传输方法代码:如何在Java中将资金从一个银行帐户转移到另一个银行帐户?,java,string,class,Java,String,Class,任务: 更改帐户类别,以便资金可以从一个帐户转移到另一个帐户。可以把这看作是从一个账户取钱并存入另一个账户。更改Banking类的主要方法以显示此新服务 我正在开发一个银行账户类,可以从银行账户余额中存款和取款。我正在处理作业的类部分,在那个部分中,您将为驱动程序声明所有方法。我的任务要求我制定一种方法,从一个账户中提取资金,然后将这些资金存入另一个账户。我已经知道如何取款和存款,我只是不知道如何把钱从一个帐户转到另一个帐户。以下是迄今为止我的传输方法代码: import java.text.N
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));
}
}
导入java.text.NumberFormat;
公共类帐户
{
私有NumberFormat fmt=NumberFormat.getCurrencyInstance();
私人最终双倍利率=0.035;//利率为3.5%
私人长帐号;
私人双平衡;
私有字符串名称;
//-----------------------------------------------------------------
//通过定义帐户的所有者、帐号、,
//和初始余额。
//-----------------------------------------------------------------
公共帐户(字符串所有者、长帐户、双首字母)
{
名称=所有者;
账户编号=账户;
余额=初始值;
}
//-----------------------------------------------------------------
//验证交易,然后存入指定金额
//存入帐户。返回新的余额。
//-----------------------------------------------------------------
公众双倍存款(双倍金额)
{
如果(金额<0)//存款值为负数
{
System.out.println();
System.out.println(“错误:存款金额无效。”);
System.out.println(账号+“”+格式(金额));
}
其他的
余额=余额+金额;
收益余额;
}
//-----------------------------------------------------------------
//验证交易,然后提取指定金额
//从帐户返回。返回新的余额。
//-----------------------------------------------------------------
公共双支取(双倍金额,双倍费用)
{
金额+=费用;
如果(金额<0)//提取值为负
{
System.out.println();
System.out.println(“错误:提取金额无效。”);
System.out.println(“账户:+账号”);
System.out.println(“请求:+fmt.格式(金额));
}
其他的
如果(金额>余额)//取数超过余额
{
System.out.println();
System.out.println(“错误:资金不足”);
System.out.println(“账户:+账号”);
System.out.println(“请求:+fmt.格式(金额));
System.out.println(“可用:“+fmt.format(balance));
}
其他的
余额=余额-金额;
收益余额;
}
公开双倍转账(双倍金额,双倍费用)
{
金额+=费用;
如果(金额<0)//提取值为负
{
System.out.println();
System.out.println(“错误:提取金额无效。”);
System.out.println(“账户:+账号”);
System.out.println(“请求:+fmt.格式(金额));
}
其他的
如果(金额>余额)//取数超过余额
{
System.out.println();
System.out.println(“错误:资金不足”);
System.out.println(“账户:+账号”);
System.out.println(“请求:+fmt.格式(金额));
System.out.println(“可用:“+fmt.format(balance));
}
其他的
余额=余额-金额;
//我应该在这里存些什么把钱存入另一个帐户?
如果(金额<0)//存款值为负数
{
System.out.println();
System.out.println(“错误:存款金额无效。”);
System.out.println(账号+“”+格式(金额));
}
其他的
余额=余额+金额;
}
//-----------------------------------------------------------------
//向帐户添加利息并返回新余额。
//-----------------------------------------------------------------
公共双addInterest()
{
余额+=(余额*比率);
收益余额;
}
//-----------------------------------------------------------------
//返回帐户的当前余额。
//-----------------------------------------------------------------
公共双getBalance()
{
收益余额;
}
//-----------------------------------------------------------------
//返回帐号。
//-----------------------------------------------------------------
公共长getAccountNumber()
{
返回账号;
}
//-----------------------------------------------------------------
//以字符串形式返回帐户的单行说明。
//-----------------------------------------------------------------
公共字符串toString()
{
返回(账号+“\t”+名称+“\t”+格式(余额));
}
}
您的转账方式没有任何用途transfer
应该是主类中实例化帐户的方法。转让的例子是
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());
}
把它变成一种方法
public static void transfer (Account from, Account to, double amount, double fee)
{
from.withdraw(amount, fee);
to.deposit(amount);
}
编辑
如果我正确理解了你的第二个问题,你想创建一个默认帐户吗?如果我误解了,你能提供更多的细节吗
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));
}
}
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);
}
}