Create a java small class for a bank account!!?
I need to create a bank account class which consists of deposit and withdraw methods. I need to have a current account and savings account?
I also need help on how to extend it so I can ask the user to inpt their withdrawal amount and deposit and it gives out the balance using scanner funtion?
So far i have something like this, isit right?..
Code :
public abstract class BankAccount {
private double balance;
public BankAccount() {
balance = 0.;
}
public void deposit(double amount) {
balance += amount;
}
public withdraw(double amount) {
// check...
balance -= amount;
}
}
public class CurrentAccount extends BankAccount {
// additional stuff
}
public class SavingsAccount extends BankAccount {
// aditional stuff
}
Re: Create a java small class for a bank account!!?
If I had to make a Bank Account class I would prefer the following interface:
Code :
Class Bank Account{
public Bank Account()
public void withdraw(double amount)
public void deposit(double amount)
public Transaction getTransaction(int index)
public int transactionCount()
public double getCurrentBalance()
private void addTransaction(Transaction t)
}
in my withdraw and deposit method I change the current Balance and I create a new Transaction and add it to the collection of Transactions (since I hate arrays, I would probably use one of the Collection-classes from the java.util package to store my transactions).
The Print Transaction and Read Transaction methods I would not place in the Bank Account-class, neither in my Transaction- class. It makes my classes depend on the UI. Prove of this is, I need to change lot of code in order to get things right.It's better to do this printing-logic in a class of its own or in your Application-class.
Re: Create a java small class for a bank account!!?
My approach would be as follows:
1. I'd create an bank account interface with the two methods; withdraw and deposit:
Code :
public interface BankAccount {
private double bal;
public void withdraw( double amount );
public void deposit( double amount );
}
2. I'd create two two classes; for CurrentAccount and SavingsAccount that would implement the above interface, and polymorphically provide implementation for the two methods; withdraw and deposit:
Code :
public class CurrentAccount implements BankAccount {
public void deposit( double amount ) {
this.bal += bal;
}
public withdraw( double amount ) {
this.bal -= bal;
}
}
I would repeat this for the savings account.
To track changes made, I would use an AOP framework to take care of policy changes in security, and things like that.
I hope this helps.