Results 1 to 2 of 2
- 03-17-2010, 03:35 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
Create a java class for a bank account!!?
I need to create a bank account class which consists of deposit and withdraw methods. I need to have classes that inherit from superclass, a current account and savings account and a test/driver class that calls all methods and operates the system?
How would i call the methods from the main and ask the user which accouunt they would like to deposit or withdraw?
something like:
Java 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 }Last edited by Eranga; 03-18-2010 at 02:36 AM. Reason: added code tags
- 03-17-2010, 04:26 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
A magic touch of Factory pattern maybe?
Here is enum used for typology.
Java Code:public abstract class BankAccount { [B]public enum AccountType { Current, Savings }[/B] public static BankAccount createBankAccount(AccountType accountType) { switch (accountType) { case Current: return new CurrentAccount(); case Savings: return new SavingsAccount(); } throw new IllegalArgumentException("The Bank account type " + accountType + " is not recognized."); } public abstract void displayAccountNmae(); private double balance; public BankAccount() { balance = 0.; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { // check... balance -= amount; } }Java Code:public class CurrentAccount extends BankAccount { @Override public void displayAccountNmae() { System.out.println("This is CurrentAccount"); } // additional stuff }
Java Code:public class SavingsAccount extends BankAccount { @Override public void displayAccountNmae() { System.out.println("This is SavingsAccount"); } // aditional stuff }
Java Code:public class BankAccountDemo { public static void main(String[] args) { BankAccount account1 = BankAccount.createBankAccount(AccountType.Current); [B][U]//provide AccountType at runtime[/U][/B] BankAccount account2 = BankAccount.createBankAccount(AccountType.Savings); [B][U]//provide AccountType at runtime[/U][/B] account1.displayAccountNmae(); account2.displayAccountNmae(); } }
You OK with this?
regards
Similar Threads
-
Bank Account
By HPcompaq256 in forum New To JavaReplies: 11Last Post: 02-26-2010, 09:05 PM -
Java Question Bank
By mgm2010 in forum New To JavaReplies: 2Last Post: 07-31-2009, 06:45 PM -
java program for updating bank details
By java__beginner in forum New To JavaReplies: 5Last Post: 03-12-2009, 03:42 PM -
Using java to access internet account
By elgem in forum Advanced JavaReplies: 3Last Post: 01-18-2009, 06:49 PM -
How to create a class in java?
By pawankumarom in forum New To JavaReplies: 2Last Post: 09-05-2008, 07:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks