Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- public class Account
- {
- private int accNum;
- private String accName;
- private double balance;
- private static int numOfAccs;
- DecimalFormat df = new DecimalFormat("#.00"); //create object to round decimals
- /**
- * Default constructor
- * Sets default values to attributes
- */
- public Account()
- {
- accNum = 0;
- accName = "no mame";
- balance = 0;
- numOfAccs = 1;
- }
- /**
- * Constructor
- * sets account name and initial balance
- * @param accName
- * @param initBalance
- */
- public Account(String accName, double initBalance)
- {
- accNum = (int)(Math.random() * 999999999 ) + 100000000 ; //generates account number (9 digits)
- this.accName = accName;
- balance = initBalance; //sets initial balance
- numOfAccs++; //increase number of accounts
- }
- /**
- * Mutator
- * withdraw money from account, decreases balance.
- * @param amount
- */
- public void withDraw(double amount)
- {
- if ( balance >= amount )
- {
- balance -= amount; //reduce balance
- System.out.println(" You've withdrawn $" + df.format(amount) + " from your account");
- }
- else
- System.out.println(" Insufficient balance");
- }
- /**
- * Mutator
- * Deposits money to account, increases balance.
- * @param amount
- */
- public void deposit(double amount)
- {
- if ( amount < 0 )
- System.out.println(" Deposit must be positive");
- else
- {
- balance += amount;
- System.out.println(" You've deposited $" + df.format(amount) + " to your account.");
- }
- }
- /**
- * Accessor
- * @return balance
- */
- public double inquiry()
- {
- return balance;
- }
- /**
- * Accessor
- * @return accName, balance and accNum in a string
- */
- public String toString()
- {
- return " Account Name: " + accName +
- "\n Balance: $" + df.format(balance) +
- "\n Account Number: " + accNum + "\n";
- }
- /**
- * Accessor
- * @return total number of accounts created
- */
- public static int numOfAccs()
- {
- return numOfAccs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment