Re: Bank Account Program.
Code :
public SavingsAccount(String string, double rate)
{
interestRate = rate;
}
public SavingsAccount(SavingsAccount yourAccount, int rate) {
}
Do you want the constructors to do anything with those parameters? Typically you will call super to call the parent constructor if you wish to assign those values to variables defined in the parent class.
Re: Bank Account Program.
BankAccount isn't abstract even though you've labeled it as such. It needs at least one abstract method.
You can have all other classes extend it directly or indirectly without having to make it abstract.
Abstract means that you might have a method that won't make sense in the superclass.
For instance,
if your BankAccount class had a method called getInterest(), it would be silly to define it in BankAccount as you'd need to know the account type in order to figure out how to do it.
Therefore, you'd make it abstract and all your subclasses, those that you could get the interest from because you know the account type and how interest is calculated with that account type, would have to define it or else be abstract themselves.
Re: Bank Account Program.
public void deposit(double amount) {}
public boolean withdraw(double amount) {
return false;}
Your BankAccount class has a method called deposit that its subclasses all have and use unless they make their own by overridng or overloading it.
Any method you don't plan to overload or override, don't make it a method of your subclass. It already is by inheritance and you don't need to show it again.
Re: Bank Account Program.
myFormat = new DecimalFormat("#.00");
What's that doing?
Could you just use %.2d or something?
Re: Bank Account Program.
Perhaps if your BankAccount class had a method called setOwner()
Code java:
public void setOwner(String owner2)
{
owner = owner2;
}
it might work.
Re: Bank Account Program.
Quote:
Originally Posted by
javapenguin
BankAccount isn't abstract even though you've labeled it as such. It needs at least one abstract method.
Abstract means that you might have a method that won't make sense in the superclass.
Not true. Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
Re: Bank Account Program.
Yeah, well technically abstract means that you have at least one method that you don't define in that class and leave it for subclasses to define.
Re: Bank Account Program.
How do I set my initial balance of my savings account to be $400.
Would it be something like this?
double initialBalance = getBalance();
How to set initial balance of savings account?
I created a bank account program but not sure how to set my initial balance of my savings account to $400.
Code :
//Defines any type of bank account
public abstract class BankAccount
{
// class variable so that each account has a unique number
protected static int numberOfAccounts = 100001;
// current balance in the account
private double balance;
// name on the account
private String owner;
// number bank uses to identify account
private String accountNumber;
//default constructor
public BankAccount()
{
balance = 0;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//standard constructor
public BankAccount(String name, double amount)
{
owner = name;
balance = amount;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//copy constructor
public BankAccount(BankAccount oldAccount, double amount)
{
owner = oldAccount.owner;
balance = amount;
accountNumber = oldAccount.accountNumber;
}
//allows you to add money to the account
public void deposit(double amount)
{
balance = balance + amount;
}
//allows you to remove money from the account if
//enough money is available
//returns true if the transaction was completed
//returns false if the there was not enough money
public boolean withdraw(double amount)
{
boolean completed = true;
if (amount <= balance)
{
balance = balance - amount;
}
else
{
completed = false;
}
return completed;
}
//accessor method to balance
public double getBalance()
{
return balance;
}
//accessor method to owner
public String getOwner()
{
return owner;
}
//accessor method to account number
public String getAccountNumber()
{
return accountNumber;
}
//mutator method to change the balance
public void setBalance(double newBalance)
{
balance = newBalance;
}
//mutator method to change the account number
public void setAccountNumber(String newAccountNumber)
{
accountNumber = newAccountNumber;
}
}
SavingsAccount:
Code :
public class SavingsAccount extends BankAccount
{
int savingsNumber=0;
double initialBalance = 400;
public SavingsAccount(String string, double rate)
{
interestRate = 2.5;
}
public SavingsAccount(SavingsAccount yourAccount, int rate) {
}
public void addInterest()
{
double interest = (getBalance() * interestRate / 100) / 12;
deposit(interest);
}
private double interestRate;
public void postInterest() {
double balance = getBalance();
balance += (balance * interestRate);
deposit(balance);
}
}
Here is expected output:
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
Here is the output I'm getting:
Account Number 100002 belonging to null
Initial balance = $.00
After deposit of $500.00, balance = $500.00
Insuffient funds to withdraw $1000.00, balance = $500.00
After monthly interest has been posted,balance = $2250.00
Re: Bank Account Program.
Please do not re-post your same question in a new thread. I have merged the new thread with this one.