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 11 of 11

Thread: Bank Account Program.

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

    Default Bank Account Program.

    I have created a Bank Account program and have run into an issue.

    Here is my code:

    AccountDriver:

    //Demonstrates the BankAccount and derived classes
     
    import java.text.*;			// to use Decimal Format
     
    public class AccountDriver
    {
    	public static void main(String[] args)
    	{
    		double put_in = 500;
    		double take_out = 1000;
     
    		DecimalFormat myFormat;
    		String money;
    		String money_in;
    		String money_out;
    		boolean completed;
     
    		// to get 2 decimals every time
    		myFormat = new DecimalFormat("#.00");
     
    		//to test the Checking Account class
    		CheckingAccount myCheckingAccount =
    				new CheckingAccount ("Benjamin Franklin", 1000);
    		System.out.println ("Account Number "
    			+ myCheckingAccount.getAccountNumber() + "-10"+
    			" belonging to " + myCheckingAccount.getOwner());
    		money  = myFormat.format(myCheckingAccount.getBalance());
    		System.out.println ("Initial balance = $" + money);
    		myCheckingAccount.deposit (put_in);
    		money_in = myFormat.format(put_in);
    		money  = myFormat.format(myCheckingAccount.getBalance());
    		System.out.println ("After deposit of $" + money_in
    			+ ",  balance = $" + money);
    		completed = myCheckingAccount.withdraw(take_out);
    		money_out = myFormat.format(take_out);
    		money  = myFormat.format(myCheckingAccount.getBalance());
    		if (completed)
    		{
    			System.out.println ("After withdrawal of $" + money_out
    					+ ",  balance = $" + money);
    		}
    		else
    		{
    			System.out.println ("Insuffient funds to withdraw $"
    					+ money_out	+ ",  balance = $" + money);
    		}
    		System.out.println();
     
    		//to test the savings account class
    		SavingsAccount yourAccount =
    				new SavingsAccount ("William Shakespeare", 400);
    		System.out.println ("Account Number "
    				+ yourAccount.getAccountNumber() +"-0"+
    				" belonging to " + yourAccount.getOwner());
    		money  = myFormat.format(yourAccount.getBalance());
    		System.out.println ("Initial balance = $" + money);
    		yourAccount.deposit (put_in);
    		money_in = myFormat.format(put_in);
    		money  = myFormat.format(yourAccount.getBalance());
    		System.out.println ("After deposit of $" + money_in
    				+ ",  balance = $" + money);
    		completed = yourAccount.withdraw(take_out);
    		money_out = myFormat.format(take_out);
    		money  = myFormat.format(yourAccount.getBalance());
    		if (completed)
    		{
    			System.out.println ("After withdrawal of $" + money_out
    				+ ",  balance = $" + money);
    		}
    		else
    		{
    			System.out.println ("Insuffient funds to withdraw $"
    				+ money_out	+ ",  balance = $" + money);
    		}
    		yourAccount.postInterest();
    		money  = myFormat.format(yourAccount.getBalance());
    		System.out.println ("After monthly interest has been posted,"
    				+ "balance = $"	+ money);
    		System.out.println();
     
     
    		// to test the copy constructor of the savings account class
    		SavingsAccount secondAccount =
    				new SavingsAccount (yourAccount,5);
    		System.out.println ("Account Number "
    				+ secondAccount.getAccountNumber()+"-1"+
    				" belonging to " + secondAccount.getOwner());
    		money  = myFormat.format(secondAccount.getBalance());
    		System.out.println ("Initial balance = $" + money);
    		secondAccount.deposit (put_in);
    		money_in = myFormat.format(put_in);
    		money  = myFormat.format(secondAccount.getBalance());
    		System.out.println ("After deposit of $" + money_in
    				+ ", balance = $" + money);
    		secondAccount.withdraw(take_out);
    		money_out = myFormat.format(take_out);
    		money  = myFormat.format(secondAccount.getBalance());
    		if (completed)
    		{
    			System.out.println ("After withdrawal of $" + money_out
    					+ ",  balance = $" + money);
    		}
    		else
    		{
    			System.out.println ("Insuffient funds to withdraw $"
    					+ money_out	+ ",  balance = $" + money);
    		}
    		System.out.println();
     
    		//to test to make sure new accounts are numbered correctly
    		CheckingAccount yourCheckingAccount =
    				new CheckingAccount ("Issac Newton", 5000);
    		System.out.println ("Account Number "
    				+ yourCheckingAccount.getAccountNumber()
    				+ " belonging to "
    				+ yourCheckingAccount.getOwner());
     
    	}
    }

    BankAccount:

    //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:

    public class SavingsAccount extends BankAccount
       {
          public SavingsAccount(String string, double rate)
          {
          interestRate = rate; 
          }
          public SavingsAccount(SavingsAccount yourAccount, int rate) {
     
          }
          public void addInterest() 
          {
          double interest = getBalance() * interestRate / 100; 
          deposit(interest); 
          }
          private double interestRate; 
     
     
     
          public void deposit(double amount) {} 
          public boolean withdraw(double amount) {
          return false;} 
          public void deductFees() {} 
          private int transactionCount;
     
     
     
       public void postInterest() {
          double balance = getBalance();
          balance += (balance * interestRate);
          deposit(balance);
     
       }     
       }


    My issue is in the SavingsAccount class.

    This is expected output:

    Account Number 100001-10 belonging to Benjamin Franklin
    Initial balance = $1000.00
    After deposit of $500.00,  balance = $1500.00
    After withdrawal of $1000.00,  balance = $499.85
     
    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
     
    Account Number 100002-1 belonging to William Shakespeare
    Initial balance = $5.00
    After deposit of $500.00, balance = $505.00
    Insuffient funds to withdraw $1000.00,  balance = $505.00
     
    Account Number 100003-10 belonging to Issac Newton

    This is the output I'm getting:

    Account Number 100001-10 belonging to Benjamin Franklin
    Initial balance = $1000.00
    After deposit of $500.00,  balance = $1500.00
    After withdrawal of $1000.00,  balance = $500.00
     
    Account Number 100002-0 belonging to null
    Initial balance = $.00
    After deposit of $500.00,  balance = $.00
    Insuffient funds to withdraw $1000.00,  balance = $.00
    After monthly interest has been posted,balance = $.00
     
    Account Number 100003-1 belonging to null
    Initial balance = $.00
    After deposit of $500.00, balance = $.00
    Insuffient funds to withdraw $1000.00,  balance = $.00
     
    Account Number 100004 belonging to Issac Newton

    Why are none of my transactions working correctly?? How do I fix it?

    Here were my instructions from my assignment.

    5. Create a new class called SavingsAccount that extends BankAccount.

    6. It should contain an instance variable called rate that represents the annual interest rate. Set it equal to 2.5%.

    7. It should also have an instance variable called savingsNumber, initialized to 0. In this bank, you have one account number, but can have several savings accounts with that same number. Each individual savings account is identified by the number following a dash. For example, 100001-0 is the first savings account you open, 100001-1 would be another savings account that is still part of your same account. This is so that you can keep some funds separate from the others, like a Christmas club account.

    8. An instance variable called accountNumber that will hide the accountNumber from the superclass, should also be in this class.

    9. Write a constructor that takes a name and an initial balance as parameters and calls the constructor for the superclass. It should initialize accountNumber to be the current value in the superclass accountNumber (the hidden instance variable) concatenated with a hyphen and then the savingsNumber.

    10. Write a method called postInterest that has no parameters and returns no value. This method will calculate one month’s worth of interest on the balance and deposit it into the account.

    Hint: formula to calculate one month interest is (balance * annual interest rate) / number of months

    11. Write a method that overrides the getAccountNumber method in the superclass.

    12. Write a copy constructor that creates another savings account for the same person. It should take the original savings account and an initial balance as parameters. It should call the copy constructor of the superclass, assign the savingsNumber to be one more than the savingsNumber of the original savings account. It should assign the accountNumber to be the accountNumber of the superclass concatenated with the hypen and the savingsNumber of the new account.
    Last edited by Punky0214; November 17th, 2010 at 03:31 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Bank Account Program.

          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.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Bank Account Program.

    myFormat = new DecimalFormat("#.00");

    What's that doing?

    Could you just use %.2d or something?

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Bank Account Program.

    Perhaps if your BankAccount class had a method called setOwner()

    public void setOwner(String owner2)
    {
    owner = owner2;
    }

    it might work.

  7. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Bank Account Program.

    Quote Originally Posted by javapenguin View Post
    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)

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();

  10. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    //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:

    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

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

Similar Threads

  1. Pls Help me run bank java application
    By chukster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2010, 07:28 PM
  2. Please help - Bank account application
    By brandonssss in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 2nd, 2010, 03:10 PM
  3. Create a java small class for a bank account!!?
    By jon123 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 24th, 2010, 11:00 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