BankAccount Class I made in AP

Discussion in 'Programming General' started by Xeox, Oct 3, 2010.

BankAccount Class I made in AP
  1. Unread #1 - Oct 3, 2010 at 5:57 PM
  2. Xeox
    Joined:
    Jun 17, 2010
    Posts:
    357
    Referrals:
    0
    Sythe Gold:
    0

    Xeox Forum Addict
    Banned

    BankAccount Class I made in AP

    Hey guys since the java threads have been pretty slow lately I wanted to post my BankAccount Class that I have been working on in AP coding at my school! Right now the class can carry out some simple commands such as withdraw, deposit, add interest, and get balance. I'll be adding on to it throughout the year but I thought you guys might want it for yourselves to fool around with or w/e u crazy people do =D.

    I used BlueJ to compile this. There are TWO CLASSES, one is the SavingsAccount class and one is the TESTER. You need to compile them both in order to atain results. In order to use this you need to understand some basic java, like using objects by calling their methods. I basically created the object for you, and I created the methods for withdrawing, depositing, etc... ENJOY =P

    EDIT: SORRY i accadentally left in an initial 10% interest rate when I typed this in, along with a couple other mistakes...this should be fixed.

    SavingsAccount Class:
    Code:
    /**
     * Write a description of class SavingsAccount here.
     * 
     * @author (Mike Perugini) 
     * @version (10/3/10)
     */
    public class SavingsAccount
    {
        /**
         * Constructs a bank account with zero balance.
         */
        private double balance;
        public SavingsAccount()
        {
            balance = 0;
        }
        /**
         * Constructs a bank account with a given balance.
         * @param initialBalance the initial balance.
         */
        public SavingsAccount(double initialBalance)
        {
            balance = initialBalance;
        }
        /**
         * Deposites money into the bank account.
         * @param amount the ammount to deposit.
         */
        public void deposite(double amount)
        {
            double newBalance = balance + amount;
            balance = newBalance;
        }
        /**
         * Withdraws money from the bank account.
         * @param amount the ammount to withdraw.
         */
        public void withdraw(double amount)
        {
            double newBalance = balance - amount;
            balance = newBalance;
        }
        /**Adds interest to bank account balance.
         * @param amount to add interest.
         */
        public void addInterest(double amount)
        {
            double interestRate = (amount);
            double newBalance = balance + (balance * interestRate / 100);
            balance = newBalance;
        }
        /**
         * Gets the current balance of the bank account.
         * @return the current balance
         */
        public double getBalance()
        {
            return balance;
        }
    }
    
    THE TESTER CLASS:
    Code:
    /**
     * Write a description of class SavingsAccountTester here.
     * 
     * @author (Mike Perugini) 
     * @version (10/3/10)
     */
    public class SavingsAccountTester
    {
       public static void main(String[] args)
       {
           //Creates a new bank account.
           SavingsAccount mikesAccount = new SavingsAccount(1000);
           
           mikesAccount.addInterest(5);
           
           double balance = mikesAccount.getBalance();
           System.out.println(balance);
           System.out.println("Expected: 1100.55");
        }
    }
    
    The tester has just one method in there, but you can edit this. The Expected string was from a HW problem, same with the addInterest + getBalance.

    Post questions if you have any!
     
  3. Unread #2 - Oct 4, 2010 at 6:35 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    BankAccount Class I made in AP

    A lot of what you're doing is redundant and could be written much more clearly.
    For example
    Code:
    double newBalance = balance + amount;
    balance = newBalance;
    Could be better written as
    Code:
    balance += amount;
    Also, when dealing with money it is best to use the java.math.BigDecimal class because primitive floating point data types aren't precise enough.
     
  5. Unread #3 - Oct 5, 2010 at 5:09 AM
  6. Travis
    Joined:
    May 14, 2010
    Posts:
    4,237
    Referrals:
    70
    Sythe Gold:
    345

    Travis Grand Master
    Banned

    BankAccount Class I made in AP

    Wonder's to self what a bank account Class is and what do you do?

    I worked in a bank for 6months as a cashier/teller and we had nothing like this on the screen.


    For us nubs can you please explain what this does?
     
  7. Unread #4 - Oct 5, 2010 at 2:01 PM
  8. pkwithpink
    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0

    pkwithpink Apprentice
    Banned

    BankAccount Class I made in AP

    I am taking my first java course in college (even though I already would rate myself as an intermediate because I have been coding for 2 years.) And one of the first things they taught us is that double data types are not accurate, they even actually told us that banks do not use them ever because of that. It is better to use an int and count in pennies then for output convert to double, or better yet use BigDecimal as was suggested.

    I don't see how this is an AP class, are you in middle school/high school/college? I am just wondering because in our fifth week of java we are already doing more advanced coding than that.
     
  9. Unread #5 - Oct 5, 2010 at 4:42 PM
  10. Zed'
    Joined:
    Oct 4, 2010
    Posts:
    93
    Referrals:
    0
    Sythe Gold:
    0

    Zed' Member

    BankAccount Class I made in AP

    Its for use in a Java program to make the program that you see on the screen, pretty much.

    They are not accurate, but for most applications they are fine. Banks want to be accurate because they do not want to lose their one penny for the double not working well. And, it doesn't seem like hard coding, but AP courses go half the speed of normal college courses and if he just started the course this seems like something they would be making.
     
  11. Unread #6 - Oct 5, 2010 at 6:06 PM
  12. Xeox
    Joined:
    Jun 17, 2010
    Posts:
    357
    Referrals:
    0
    Sythe Gold:
    0

    Xeox Forum Addict
    Banned

    BankAccount Class I made in AP

    Exactly right. At this point my only three commands I know are int, double, and String in terms of saving a variable as a number/string. So please don't troll about double not being accurate, because as of right now that is the only way I know to be able to process floating point numbers. This was just to show a program I was kind of proud of because it's the first one I created where I made a class, created my own object, and gave that object methods that I created.

    By the way to answer the previous question, I'm a senior in high school
     
  13. Unread #7 - Oct 13, 2010 at 2:10 PM
  14. xRSx Newb
    Joined:
    Sep 27, 2010
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0

    xRSx Newb Member

    BankAccount Class I made in AP

    Definitely a good start for your first OO programming course. Make sure you keep up the detailed commenting... I can tell you from experience that looking back on something your wrote a year ago is like trying to read a foreign language :p
     
< User Input Java Help *Advanced?* | EliteSwitch - My version in VB.Net >

Users viewing this thread
1 guest


 
 
Adblock breaks this site