3 Replies - 24955 Views - Last Post: 18 November 2009 - 10:30 AM Rate Topic: -----

#1 aeemma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 09

Bank Account program in Java using classes and objects

Post icon  Posted 18 November 2009 - 10:14 AM

Am trying to Design a class named Account that represents an individual’s bank account. But having some difficulty with it. Can someone please advise on how to do this. Any help will be appreciated.



The class contains:

An int data field named id for the account (default 0).

A double data field named balance for the account (default 0.0)

A double data field named annualInterestRate that stores the current interest rate (default 0.0)

A Date data field named dateCreated that stores the date when the account was created.

A no-arg constructor that creates a default account.

The accessor and mutator methods for id, balance, and annualInterestRate.

The accessor method for dateCreated.

A method named getMonthlyInterestRate() that returns the monthly interest rate.

A method named withdraw() that withdraws a specified amount from the account if the account has enough money in it. Otherwise, it prints a message saying “Insufficient funds”.

A method named deposit() that deposits a specified amount to the account.
Must use the above class to do the following:

NEED to have an account ID of 1122, a balance of $20,000 and an annual interest rate of 4.5%. use the withdraw method to withdraw $2500, use the deposit method to deposit $3000 and print the balance, the monthly interest rate and the date when this account was created. Create a second account object with an account ID 1123, a balance of $100 and an annual interest rate of 2.25%.

This is the code that I have so far but I am stuck. Please help....thank you.

/// Main class
import java.util.Scanner;

public class BankProgram {
public static void main(String args[]) {
Account acct1 = new Account();
acct1.setID(1122);
acct1.setBalance(20000);
acct1.setAnnualInterestRate(4.5);
System.out.print("\nDepositing $3000 into account, balance is now ");
acct1.deposit(3000);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing $2500, balance is now ");
acct1.withdraw(2500);
System.out.println(acct.getBalance());
}
}


public class Account {
int id;
Date dateCreated;
double balance, annualInteretRate;
// Other fields

public Account() {
// Here is where you create a default account.
}

public void setID(int i) {
id = i;
}

public int getID() {
return id;
}

// Method that checks to see if balance is sufficient for withdrawal.
// If so, reduces balance by amount; if not, prints message.
public void withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
}
else
{
System.out.println("Insufficient funds");
}
}

// Method that adds deposit amount to balance.
public void deposit(double amount)
{
balance += amount;
}
//-----------------------------------…
// Returns balance.
//-----------------------------------…
public double getBalance()
{
return balance;
}
// Adds interest to the account and returns the new balance.
/
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}


Locke: [code] tags.

This post has been edited by Locke: 18 November 2009 - 10:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Bank Account program in Java using classes and objects

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Bank Account program in Java using classes and objects

Posted 18 November 2009 - 10:20 AM

View Postaeemma, on 18 Nov, 2009 - 11:14 AM, said:

But having some difficulty with it. Can someone please advise on how to do this. Any help will be appreciated.


What part are you having trouble with?

And, please remember :code:. I'll edit your post this one time and do it for you, but please use the tags in the future. :)

This post has been edited by Locke: 18 November 2009 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 aeemma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 09

Re: Bank Account program in Java using classes and objects

Posted 18 November 2009 - 10:25 AM

Sorry I am new to this..... thank you for editing my post. I am having trouble with how to set up two objects. I can do one but not sure how to incorporate the second. I have been stuck on this forever.... I am completely new to programming and this is so foreign to me.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Bank Account program in Java using classes and objects

Posted 18 November 2009 - 10:30 AM

To have another object, just follow the template you have using the first one. You can create as many objects as you want and call whatever methods you want. You can manipulate the data however you'd like to make sure that your code works.

Basically, your second object could be named acct2, and the only difference in calling methods would be the variable name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1