Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Arrays;
  3. import java.util.Stack;
  4. //Transaction list is an Integer because it will just be storing the amount entering and exiting the account,
  5. //if we wanted to store more info we would need another object to store where the money is coming/going
  6.  
  7. public class Account {
  8. private int overdraftPreference;
  9. private Stack<Double> transactionList = new Stack<Double>();
  10. private String type;
  11. private double value = 0;
  12. private DecimalFormat df = new DecimalFormat("#.00");
  13.  
  14. public Account(int overdraftPreference, String type) {
  15. this.overdraftPreference = overdraftPreference;
  16. this.type = type;
  17. }
  18.  
  19. public String getType() {
  20. return type;
  21. }
  22.  
  23. public void setType(String type) {
  24. this.type = type;
  25. }
  26.  
  27. public int getOverdraftPreference() {
  28. return overdraftPreference;
  29. }
  30.  
  31. public void setOverdraftPreference(int overdraftPreference) {
  32. this.overdraftPreference = overdraftPreference;
  33. }
  34.  
  35. public Stack<Double> getTransactionList() {
  36. return transactionList;
  37. }
  38.  
  39. public void setTransactionList(Stack<Double> transactionList) {
  40. this.transactionList = transactionList;
  41. }
  42. //Sarah Mottram
  43. public String odToString()
  44. {
  45. if(overdraftPreference == 2)
  46. {
  47. return "Overdraft Allowed";
  48. }
  49. else
  50. {
  51. return "Overdraft Denied";
  52. }
  53. }
  54. public double getTrueValue() //THIS RETURNS A DOUBLE AND SHOULD BE USED FOR MATH STUFF
  55. {
  56. return value;
  57. }
  58. public String getValue() //THIS RETURNS A STRING AND SHOULD BE USED FOR WHEN YOU WANT THE MONEY VALUE WITH 2 DECIMAL PLACES
  59. {
  60. return "$" + df.format(value);
  61. }
  62. public void addTransaction(double trans)
  63. {
  64. transactionList.push(trans);
  65. }
  66. public void deposit(double dep)
  67. {
  68. value += dep;
  69. addTransaction(dep);
  70. }
  71. public boolean withdraw(double with)
  72. {
  73. if((value - with) < 0)
  74. {
  75. switch(overdraftPreference) //1 is no od 2 is od and apply fine of 25
  76. {
  77. case 1:
  78. return false;
  79. case 2:
  80. value -= (with+25);
  81. addTransaction(-(with+25));
  82. return true;
  83. default:
  84. return false;
  85. }
  86. }
  87. else
  88. {
  89. value -= with;
  90. addTransaction(-with);
  91. return true;
  92. }
  93. }
  94. public boolean transfer(double tran, Account a)
  95. {
  96. if((value - tran) < 0)
  97. {
  98. switch(overdraftPreference) //1 is no od 2 is od and apply fine of 25
  99. {
  100. case 1:
  101. return false;
  102. case 2:
  103. value -= (tran+25);
  104. addTransaction(-(tran+25));
  105. a.deposit(tran);
  106. return true;
  107. default:
  108. return false;
  109. }
  110. }
  111. else
  112. {
  113. value -= tran;
  114. addTransaction(-tran);
  115. a.deposit(tran);
  116. return true;
  117. }
  118. }
  119. private String[] getTransactions()
  120. {
  121. @SuppressWarnings("unchecked") //needs to be cast
  122. Stack<Double> transactionListCopy = (Stack<Double>) transactionList.clone();
  123. String[] printable = new String[transactionList.size()];
  124. int i = 0;
  125. while(!transactionListCopy.isEmpty())
  126. {
  127. printable[i] = "$" + transactionListCopy.pop();
  128. i++;
  129. }
  130. return printable;
  131. }
  132. public String toString()
  133. {
  134. return this.type + " Account Balance: " + getValue() + "\nTransaction List: " + Arrays.toString(getTransactions()) + "\nOverdraft Preference: " + odToString();
  135. }
  136. //Sarah Mottram
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement