Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Create a java small class for a bank account!!?

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?..


    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 helloworld922; March 24th, 2010 at 04:38 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 2 Posts

    Default 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:
    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.
    Last edited by helloworld922; March 24th, 2010 at 04:39 PM.

  3. The Following 3 Users Say Thank You to heronfisher For This Useful Post:

    javapenguin (January 22nd, 2011), jon123 (March 15th, 2010), Json (March 15th, 2010)

  4. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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:
    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.
    Last edited by helloworld922; March 24th, 2010 at 04:38 PM.

Similar Threads

  1. Java error "could not create java virtual machine"
    By aubrey4444 in forum Java Theory & Questions
    Replies: 17
    Last Post: October 3rd, 2010, 12:51 PM
  2. Java Source/APIs to create a Fourm
    By softwarebuzz in forum Java Theory & Questions
    Replies: 2
    Last Post: January 9th, 2010, 01:46 AM
  3. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  4. Bank account GUI using swing
    By AlanM595 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2009, 04:39 AM
  5. How to delete records from a Random-Access File?
    By keith in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: March 31st, 2009, 11:40 AM