public class BankAccount
{
public double balance;
public BankAccount()
{
}
public BankAccount(double b)
{
balance = b;
}
public double makeDeposit(double d)
{
System.out.println("You’ve deposited $" + d + ". \nYour current balance is $" + (balance + d) + ".");
return balance += d;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
public double getBalance()
{
System.out.println("Your current balance is $" + balance + ".");
return balance;
}
}
public class SavingsAccount extends BankAccount
{
public double interest;
public SavingsAccount()
{
}
public SavingsAccount(double balance, double i)
{
super(balance);
interest = i;
}
public double addInterest()
{
return balance += balance * interest;
}
}
{
public double interest;
public SavingsAccount()
{
}
public SavingsAccount(double balance, double i)
{
super(balance);
interest = i;
}
public double addInterest()
{
return balance += balance * interest;
}
}
public class CheckingAccount extends BankAccount
{
public double minBalance;
public double fee;
public CheckingAccount()
{
}
public CheckingAccount(double balance)
{
super(balance);
minBalance = 50.0;
fee = 2.0;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
if (balance - w < 50.)
{
System.out.println("You’ve withdrawn $" + w + ", and have been charged $2.00. \nYour current balance is $" + (balance - (w + 2.)) + ".");
return balance -= w + 2.;
}
else
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
}
{
public double minBalance;
public double fee;
public CheckingAccount()
{
}
public CheckingAccount(double balance)
{
super(balance);
minBalance = 50.0;
fee = 2.0;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
if (balance - w < 50.)
{
System.out.println("You’ve withdrawn $" + w + ", and have been charged $2.00. \nYour current balance is $" + (balance - (w + 2.)) + ".");
return balance -= w + 2.;
}
else
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
}
−Phoenix
Hi Phoenix, one item of follow-up, for the instance variables of the CheckingAccount class:
ReplyDeleteEvery instance of a CheckingAccount object will have the exact same minimum balance value, and the exact same fee value. The minimum balance is $50.00, and the fee is $2.00
Since these values are the same for all checking accounts, there is a more suitable way to declare the instance variables. Just correct the declarations of those ivars as a response to this comment, and we're good to go. Thank you.
To make the code more suitable, one can replace the code that creates the instance variables with the following:
ReplyDeletepublic final double minBalance = 50.0;
public final double fee = 2.0;
One will also need to change the “CheckingAccount” constructor with the following:
public CheckingAccount(double balance)
{
super(balance);
}
−Phoenix
Why make the ivars for the CheckingAccount class publicly available?
ReplyDeleteVery well. One should have “minBalance” and “fee” as “private” variables. This will prevent them from being tampered with via another program. It is also practical to use, as “CheckingAccount” will not need to be referenced from any other program.
ReplyDelete−Phoenix
Good catch. 500 XPs awarded for this completion.
ReplyDelete