Advertisement
advictoriam

Untitled

Nov 26th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. /**
  2.    A bank account has a balance that can be changed by
  3.    deposits and withdrawals.
  4. */
  5. public class BankAccount
  6. {  
  7.    private double balance;
  8.    private int transactions;
  9.  
  10.    /**
  11.       Constructs a bank account with a zero balance.
  12.    */
  13.    public BankAccount()
  14.    {  
  15.       balance = 0;
  16.       transactions = 0;
  17.    }
  18.  
  19.    /**
  20.       Constructs a bank account with a given balance.
  21.       @param initialBalance the initial balance
  22.    */
  23.    public BankAccount(double initialBalance)
  24.    {  
  25.       balance = initialBalance;
  26.       transactions = 0;
  27.    }
  28.  
  29.    /**
  30.       Deposits money into the bank account.
  31.       @param amount the amount to deposit
  32.    */
  33.    public void deposit(double amount)
  34.    {  
  35.       double newBalance = balance + amount;
  36.       balance = newBalance;
  37.       transactions++;
  38.    }
  39.  
  40.    /**
  41.       Withdraws money from the bank account.
  42.       @param amount the amount to withdraw
  43.    */
  44.    public void withdraw(double amount)
  45.    {  
  46.       double newBalance = balance - amount;
  47.       balance = newBalance;
  48.       transactions++;
  49.    }
  50.  
  51.    /**
  52.       Gets the current balance of the bank account.
  53.       @return the current balance
  54.    */
  55.    public double getBalance()
  56.    {  
  57.       return balance;
  58.    }
  59.  
  60.    /**
  61.       Gets the total number of transactions of the bank account.
  62.       @return the transaction count
  63.    */
  64.    public double getTransactionCount()
  65.    {  
  66.       return transactions;
  67.    }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement