Guest User

Untitled

a guest
Oct 20th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. public class Account
  3. {
  4. private int accNum;
  5. private String accName;
  6. private double balance;
  7. private static int numOfAccs;
  8.  
  9. DecimalFormat df = new DecimalFormat("#.00"); //create object to round decimals
  10.  
  11. /**
  12. * Default constructor
  13. * Sets default values to attributes
  14. */
  15. public Account()
  16. {
  17. accNum = 0;
  18. accName = "no mame";
  19. balance = 0;
  20. numOfAccs = 1;
  21. }
  22.  
  23. /**
  24. * Constructor
  25. * sets account name and initial balance
  26. * @param accName
  27. * @param initBalance
  28. */
  29. public Account(String accName, double initBalance)
  30. {
  31. accNum = (int)(Math.random() * 999999999 ) + 100000000 ; //generates account number (9 digits)
  32. this.accName = accName;
  33. balance = initBalance; //sets initial balance
  34.  
  35. numOfAccs++; //increase number of accounts
  36. }
  37.  
  38. /**
  39. * Mutator
  40. * withdraw money from account, decreases balance.
  41. * @param amount
  42. */
  43. public void withDraw(double amount)
  44. {
  45. if ( balance >= amount )
  46. {
  47. balance -= amount; //reduce balance
  48. System.out.println(" You've withdrawn $" + df.format(amount) + " from your account");
  49. }
  50. else
  51. System.out.println(" Insufficient balance");
  52. }
  53.  
  54. /**
  55. * Mutator
  56. * Deposits money to account, increases balance.
  57. * @param amount
  58. */
  59. public void deposit(double amount)
  60. {
  61. if ( amount < 0 )
  62. System.out.println(" Deposit must be positive");
  63. else
  64. {
  65. balance += amount;
  66. System.out.println(" You've deposited $" + df.format(amount) + " to your account.");
  67. }
  68. }
  69.  
  70. /**
  71. * Accessor
  72. * @return balance
  73. */
  74. public double inquiry()
  75. {
  76. return balance;
  77. }
  78.  
  79. /**
  80. * Accessor
  81. * @return accName, balance and accNum in a string
  82. */
  83. public String toString()
  84. {
  85. return " Account Name: " + accName +
  86. "\n Balance: $" + df.format(balance) +
  87. "\n Account Number: " + accNum + "\n";
  88. }
  89.  
  90. /**
  91. * Accessor
  92. * @return total number of accounts created
  93. */
  94. public static int numOfAccs()
  95. {
  96. return numOfAccs;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment