Java复制构造函数银行帐户问题
在我的学校开始深入学习Java,我终于撞上了我的墙,我不知道我哪里出错了。我将列出4个类,一个是AccountDriver或main方法,BankAccount,CheckingAccount和SavingsAccount派生的父类。现在我的问题是储蓄账户。这是代码 账户驱动程序Java复制构造函数银行帐户问题,java,banking,Java,Banking,在我的学校开始深入学习Java,我终于撞上了我的墙,我不知道我哪里出错了。我将列出4个类,一个是AccountDriver或main方法,BankAccount,CheckingAccount和SavingsAccount派生的父类。现在我的问题是储蓄账户。这是代码 账户驱动程序 //Demonstrates the BankAccount and derived classes import java.text.*; // to use Decimal Format pub
//Demonstrates the BankAccount and derived classes
import java.text.*; // to use Decimal Format
public class AccountDriver
{
public static void main(String[] args)
{
double put_in = 500;
double take_out = 1000;
DecimalFormat myFormat;
String money;
String money_in;
String money_out;
boolean completed;
// to get 2 decimals every time
myFormat = new DecimalFormat("#.00");
//to test the Checking Account class
CheckingAccount myCheckingAccount =
new CheckingAccount ("Benjamin Franklin", 1000);
System.out.println ("Account Number "
+ myCheckingAccount.getAccountNumber() +
" belonging to " + myCheckingAccount.getOwner());
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("Initial balance = $" + money);
myCheckingAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = myCheckingAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(myCheckingAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test the savings account class
SavingsAccount yourAccount =
new SavingsAccount ("William Shakespeare", 400);
System.out.println ("Account Number "
+ yourAccount.getAccountNumber() +
" belonging to " + yourAccount.getOwner());
money = myFormat.format(yourAccount.getBalance());
System.out.println ("Initial balance = $" + money);
yourAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = yourAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(yourAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
yourAccount.postInterest();
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After monthly interest has been posted,"
+ "balance = $" + money);
System.out.println();
// to test the copy constructor of the savings account class
SavingsAccount secondAccount =
new SavingsAccount (yourAccount,5);
System.out.println ("Account Number "
+ secondAccount.getAccountNumber()+
" belonging to " + secondAccount.getOwner());
money = myFormat.format(secondAccount.getBalance());
System.out.println ("Initial balance = $" + money);
secondAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(secondAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
secondAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(secondAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test to make sure new accounts are numbered correctly
CheckingAccount yourCheckingAccount =
new CheckingAccount ("Issac Newton", 5000);
System.out.println ("Account Number "
+ yourCheckingAccount.getAccountNumber()
+ " belonging to "
+ yourCheckingAccount.getOwner());
}
}
银行账户(父类)
最后是储蓄账户
public class SavingsAccount extends BankAccount{
double rate = .025;
static int savingsNumber = 0;
private String accountNumber = super.getAccountNumber();
//Default constructor
public SavingsAccount(String name, double amount){
super(name, amount);
accountNumber = accountNumber + "-" + Integer.toString(savingsNumber);
}
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
}
public void postInterest(){
double interestAmount = getBalance() * .025/12;
double finalAmount = getBalance() + interestAmount;
setBalance(finalAmount);
}
public String getAccountNumber()
{
return accountNumber;
}
}
这是AccountDriver运行时的输出
Account Number 100001-10 belonging to Benjamin Franklin
Initial balance = $1000.00
After deposit of $500.00, balance = $1500.00
After withdrawal of $1000.00, balance = $499.85
Account Number 100002-0 belonging to William Shakespeare
Initial balance = $400.00
After deposit of $500.00, balance = $900.00
Insuffient funds to withdraw $1000.00, balance = $900.00
After monthly interest has been posted,balance = $901.88
Account Number 100002-01 belonging to null **Should be 10002-1 and William Shakespeare**
Initial balance = $.00 **Should be $5.00**
After deposit of $500.00, balance = $500.00 **Should be changed in accordance with an initial balance of $5.00
Insuffient funds to withdraw $1000.00, balance = $500.00 **" "**
Account Number 100004-10 belonging to Issac Newton **Should be # 10003-10
所以我的问题都列在星号上,我花了2个小时尝试不同的方法,要么得到相同的结果,要么得到更差的结果。希望有人能指出我错在哪里。谢谢大家。所有者是BankAccount类别的成员。您还必须在副本中设置所有者成员。其他成员也是如此
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
owner = oldAccount.getOwner();
balance = oldAccount.getBalance();
//...
}
所有者是BankAccount类的成员。您还必须在副本中设置所有者成员。其他成员也是如此
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
owner = oldAccount.getOwner();
balance = oldAccount.getBalance();
//...
}
在复制构造函数中,应初始化传递的
SavingsAccount
实例中的所有成员,如:
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
super(oldAccount,amount); // <--- SUPER COPY CONSTRUCTOR CALLED
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
}
//复制构造函数
公共储蓄账户(储蓄账户旧账户,双倍金额)
{
super(oldcount,amount);//在复制构造函数中,您应该初始化传递的SavingsAccount
实例中的所有成员,如下所示:
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
super(oldAccount,amount); // <--- SUPER COPY CONSTRUCTOR CALLED
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
}
//复制构造函数
公共储蓄账户(储蓄账户旧账户,双倍金额)
{
super(oldcount,amount);//尝试在所有者和余额上显示“BankAccount.owner/balance不可见”时出错。您应该在基类中为成员引入公共setter或使成员受保护。尝试这样做会在所有者和余额上显示错误“BankAccount.owner/balance不可见”您应该为基类中的成员引入公共setter,或者使该成员受到保护。