Results 1 to 12 of 12

Thread: Bank Account

  1. #1
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default Bank Account

    Hi I need to create Bank account which would give these values when the tester class is applied:

    The tester class is
    public class BankAccountTester
    {
    public static void main(String[] args)
    {
    BankAccount account = new BankAccount(100);
    account.deposit(-100);
    System.out.println(account.getBalance());
    System.out.println("Expected: 100");
    account.withdraw(-50);
    System.out.println(account.getBalance());
    System.out.println("Expected: 100");
    account.withdraw(200);
    System.out.println(account.getBalance());
    System.out.println("Expected: 100");
    }
    }


    MY CODE IS

    /**
    A bank account has a balance that can be changed by
    deposits and withdrawals.
    */
    public class BankAccount
    {
    private double balance;
    private int transaction;
    private double minimumBalance;
    //define the minimum balance method


    /**
    Constructs a bank account with a zero balance.
    */
    public BankAccount()
    {
    balance = 0;
    }

    /**
    Constructs a bank account with a given balance.
    @param initialBalance the initial balance
    */
    public BankAccount(double initialBalance)
    {
    balance = initialBalance;
    }

    /**
    Deposits money into the bank account.
    @param amount the amount to deposit
    */
    public void deposit(double amount)
    { if (amount<=0) {
    amount=0;
    double newbalance= balance +amount*0;
    balance=newbalance;
    transaction=transaction +1;}
    if (amount>=0){
    double newBalance = balance + amount;
    balance = newBalance;
    transaction = transaction +1; }}


    /**
    Withdraws money from the bank account.
    @param amount the amount to withdraw
    */
    public void withdraw(double amount)
    { if (amount<=balance){
    double newBalance = balance + amount;
    balance = newBalance;
    transaction = transaction +1;
    minimumBalance = Math.min(minimumBalance, balance);
    }
    if (amount>balance){
    double newBalance = balance + amount*0;
    balance = newBalance;}}


    /**
    Gets the current balance of the bank account.
    @return the current balance
    */
    public double getBalance()
    {
    return balance;
    }

    public void endOfMonth(double interestRate)
    {
    balance= balance - transaction;
    double interest = minimumBalance * interestRate / 100 / 12;
    transaction= 0;

    }
    // declare a minimumBlance Method
    public double getminimumBalance()
    { return minimumBalance;
    }}




    My Values comes out to be:
    100.0
    Expected: 100
    50.0
    Expected: 100
    50.0
    Expected: 100


    Can anyone inspect my code and tell me whats wrong?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello.

    Please use code tags and also tell us what is wrong: what is your current program doing/not doing that is incorrect? What error messages are you seeing?

    Best of luck!

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by dsohi1821 View Post
    Can anyone inspect my code and tell me whats wrong?

    it quite confusing if you use positive and negative amounts in your transactions/method-calls. my suggestion: always use positive amounts in transactions and then subtract this amount in the withdraw-method and add the amount in the deposit-method to the balace. your problem is quite easy to solve and i hope you're not really working as a developer for a bank ;)

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    what is this logic? Just put what u are going to do in each of the method above as comments.As per the logic its working....But really very dangerous bank.

    Deposited 100 and trying to withdraw 200 .............
    hahaha........
    Ramya:cool:

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Deposited 100 and trying to withdraw 200

    this is ok, because with this test you can see if your app behave right with overdraws.

  6. #6
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    what is this logic? Just put what u are going to do in each of the method above as comments.As per the logic its working....But really very dangerous bank.

    Deposited 100 and trying to withdraw 200 .............
    hahaha........
    No the program is suppose to block such a transaction. SO I'm having trouble with the withdraw method. Can someone help me fix my withdraw method. Which is :

    THIS METHOD IS SUPPOSE THE CANCEL TRANSACTION with amount more than the current balance. Also withdraw amount can't be negative such as -50, so such a transaction should also be canceled.

    /**
    Withdraws money from the bank account.
    @param amount the amount to withdraw
    */
    public void withdraw(double amount)
    { if (amount<=balance){
    double newBalance = balance + amount;
    balance = newBalance;
    transaction = transaction +1;
    minimumBalance = Math.min(minimumBalance, balance);
    }
    if (amount>balance){
    double newBalance = balance + amount*0;
    balance = newBalance;}}

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dsohi1821 View Post
    No the program is suppose to block such a transaction. SO I'm having trouble with the withdraw method. Can someone help me fix my withdraw method. Which is :

    THIS METHOD IS SUPPOSE THE CANCEL TRANSACTION with amount more than the current balance. Also withdraw amount can't be negative such as -50, so such a transaction should also be canceled.

    /**
    Withdraws money from the bank account.
    @param amount the amount to withdraw
    */
    public void withdraw(double amount)
    { if (amount<=balance){
    double newBalance = balance + amount;
    balance = newBalance;
    transaction = transaction +1;
    minimumBalance = Math.min(minimumBalance, balance);
    }
    if (amount>balance){
    double newBalance = balance + amount*0;
    balance = newBalance;}}
    There are no two ifs, if you want to withdraw more than is present on your account or if you want to withdraw a negative amount of money the lady at the counter just stares at you and won't listen to you:

    Java Code:
    if (amount <= balance && amount > 0) { // sensible amount for a withdrawal?
       balance-= amount;
       transactions++;
    }
    kind regards,

    Jos

  8. #8
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Talking

    Quote Originally Posted by JosAH View Post
    There are no two ifs, if you want to withdraw more than is present on your account or if you want to withdraw a negative amount of money the lady at the counter just stares at you and won't listen to you:

    Java Code:
    if (amount <= balance && amount > 0) { // sensible amount for a withdrawal?
       balance-= amount;
       transactions++;
    }
    kind regards,

    Jos
    Jos you work hard to make students pass Java. Thanks man. I actually thought my code will work but it doesn't seem to work idk why. In the book that were are using for java does have two if statements.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dsohi1821 View Post
    Jos you work hard to make students pass Java. Thanks man. I actually thought my code will work but it doesn't seem to work idk why. In the book that were are using for java does have two if statements.
    The mere fact that a book uses two if statements doesn't imply that you have to use two if statements. e.g. what is wrong with my version? Also, what does "doesn't seem to work" mean? Incorrect results? Doesn't compile? Crashes? Daemons flying out of your nose?

    kind regards,

    Jos

  10. #10
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The mere fact that a book uses two if statements doesn't imply that you have to use two if statements. e.g. what is wrong with my version? Also, what does "doesn't seem to work" mean? Incorrect results? Doesn't compile? Crashes? Daemons flying out of your nose?

    kind regards,

    Jos
    it does work.

  11. #11
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    I hope daemons dont fly out of your nose :)

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dsohi1821 View Post
    it does work.
    Good; does that mean that you could do it with just one if statement?

    kind regards,

    Jos

Similar Threads

  1. Transfer from one account to another
    By vividcooper in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 11:43 PM
  2. validating account
    By ServletEst in forum Java Servlet
    Replies: 3
    Last Post: 08-24-2009, 02:11 PM
  3. Java Question Bank
    By mgm2010 in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 07:45 PM
  4. Code bank
    By Eranga in forum Suggestions & Feedback
    Replies: 2
    Last Post: 01-01-2008, 06:23 AM
  5. Help MEEEEEEE! bank project
    By subwoofer717 in forum Advanced Java
    Replies: 1
    Last Post: 12-25-2007, 10:08 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
  •