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

Thread: Bank Account Program re-post

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Bank Account Program re-post

    I code have run into issues


    //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() +
    			" 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() +
    				" 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()+
    				" 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());
     
    	}
    }

    public abstract class BankAccount
    {
     
    	protected static int numberOfAccounts = 100001;
    	private double balance;
    	private String owner;
    	private String accountNumber;
     
    	public BankAccount()
    	{
    		balance = 0;
    		accountNumber = numberOfAccounts + "";
    		numberOfAccounts++;
    	}
     
    	public BankAccount(String name, double amount)
    	{
    		owner = name;
    		balance = amount;
    		accountNumber = numberOfAccounts + "";
    		numberOfAccounts++;
    	}
     
    	public BankAccount(BankAccount oldAccount, double amount)
    	{
    		owner = oldAccount.owner;
    		balance = amount;
    		accountNumber = oldAccount.accountNumber;
    	}
     
     
    	public void deposit(double amount)
    	{
    		balance = balance + amount;
    	}
     
     
    	public boolean withdraw(double amount)
    	{
    		boolean completed = true;
     
    		if (amount <= balance)
    		{
    			balance = balance - amount;
    		}
    		else
    		{
    			completed = false;
    		}
    		return completed;
    	}
     
     
    	public double getBalance()
    	{
    		return balance;
    	}
     
     
    	public String getOwner()
    	{
    		return owner;
    	}
     
     
    	public String getAccountNumber()
    	{
    		return accountNumber;
    	}
     
     
    	public void setBalance(double newBalance)
    	{
    		balance = newBalance;
    	}
     
     
    	public void setAccountNumber(String newAccountNumber)
    	{
    		accountNumber = newAccountNumber;
    	}
    }

    public class SavingsAccount extends BankAccount
    {
    int savingsNumber=0;
    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 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);
    }
    }

    public class CheckingAccount extends BankAccount  {
     
    	 private int transactionCount;
     
         private static final int FREE_TRANSACTIONS = 0;
         private static final double TRANSACTION_FEE = 0.15;
     
    	   public CheckingAccount(String string, double initialBalance)
    	{  
     
    	      super(string, initialBalance);
     
     
    	      transactionCount = 0; 
    	}
    	   public void deposit(double amount) 
    	{  
    	      transactionCount++;
     
    	      super.deposit(amount); 
    	}
     
    	   public boolean withdraw(double amount) 
    	{  
    	      transactionCount++;
    	      return super.withdraw(amount); 
    	}
    	   public void deductFees()
    	{  
     
    	}
     
     
    	}


    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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Bank Account Program re-post

    You should have edited your other post instead of creating a new post. I've deleted the duplicate.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Bank Account Program re-post

    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


    The balance looks like a fee? If it is.. your deductFees() method is empty,

    Your savings account has no reference to the super class to have the fields you want.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bank Account Program re-post

    i see that my deductFees() method is not neccessary for this class, but i am not sure wat i should add so that the output will show the Initial balance!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Bank Account Program re-post

    What does the output show now? Please post what the program does now and also add comments to show what you want it to output.

Similar Threads

  1. help with my bank account program
    By captain_turkiye in forum Object Oriented Programming
    Replies: 3
    Last Post: October 14th, 2011, 10:33 AM
  2. Bank Account Program.
    By Punky0214 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 17th, 2010, 09:42 PM
  3. Bank account GUI using swing
    By AlanM595 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2009, 04:39 AM
  4. 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