Results 1 to 2 of 2
  1. #1
    singh345 is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default 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 03:36 AM. Reason: added code tags

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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

  1. Bank Account
    By HPcompaq256 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 10:05 PM
  2. Java Question Bank
    By mgm2010 in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 07:45 PM
  3. java program for updating bank details
    By java__beginner in forum New To Java
    Replies: 5
    Last Post: 03-12-2009, 04:42 PM
  4. Using java to access internet account
    By elgem in forum Advanced Java
    Replies: 3
    Last Post: 01-18-2009, 07:49 PM
  5. How to create a class in java?
    By pawankumarom in forum New To Java
    Replies: 2
    Last Post: 09-05-2008, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •