Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Arrays;
- import java.util.Stack;
- //Transaction list is an Integer because it will just be storing the amount entering and exiting the account,
- //if we wanted to store more info we would need another object to store where the money is coming/going
- public class Account {
- private int overdraftPreference;
- private Stack<Double> transactionList = new Stack<Double>();
- private String type;
- private double value = 0;
- private DecimalFormat df = new DecimalFormat("#.00");
- public Account(int overdraftPreference, String type) {
- this.overdraftPreference = overdraftPreference;
- this.type = type;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public int getOverdraftPreference() {
- return overdraftPreference;
- }
- public void setOverdraftPreference(int overdraftPreference) {
- this.overdraftPreference = overdraftPreference;
- }
- public Stack<Double> getTransactionList() {
- return transactionList;
- }
- public void setTransactionList(Stack<Double> transactionList) {
- this.transactionList = transactionList;
- }
- //Sarah Mottram
- public String odToString()
- {
- if(overdraftPreference == 2)
- {
- return "Overdraft Allowed";
- }
- else
- {
- return "Overdraft Denied";
- }
- }
- public double getTrueValue() //THIS RETURNS A DOUBLE AND SHOULD BE USED FOR MATH STUFF
- {
- return value;
- }
- public String getValue() //THIS RETURNS A STRING AND SHOULD BE USED FOR WHEN YOU WANT THE MONEY VALUE WITH 2 DECIMAL PLACES
- {
- return "$" + df.format(value);
- }
- public void addTransaction(double trans)
- {
- transactionList.push(trans);
- }
- public void deposit(double dep)
- {
- value += dep;
- addTransaction(dep);
- }
- public boolean withdraw(double with)
- {
- if((value - with) < 0)
- {
- switch(overdraftPreference) //1 is no od 2 is od and apply fine of 25
- {
- case 1:
- return false;
- case 2:
- value -= (with+25);
- addTransaction(-(with+25));
- return true;
- default:
- return false;
- }
- }
- else
- {
- value -= with;
- addTransaction(-with);
- return true;
- }
- }
- public boolean transfer(double tran, Account a)
- {
- if((value - tran) < 0)
- {
- switch(overdraftPreference) //1 is no od 2 is od and apply fine of 25
- {
- case 1:
- return false;
- case 2:
- value -= (tran+25);
- addTransaction(-(tran+25));
- a.deposit(tran);
- return true;
- default:
- return false;
- }
- }
- else
- {
- value -= tran;
- addTransaction(-tran);
- a.deposit(tran);
- return true;
- }
- }
- private String[] getTransactions()
- {
- @SuppressWarnings("unchecked") //needs to be cast
- Stack<Double> transactionListCopy = (Stack<Double>) transactionList.clone();
- String[] printable = new String[transactionList.size()];
- int i = 0;
- while(!transactionListCopy.isEmpty())
- {
- printable[i] = "$" + transactionListCopy.pop();
- i++;
- }
- return printable;
- }
- public String toString()
- {
- return this.type + " Account Balance: " + getValue() + "\nTransaction List: " + Arrays.toString(getTransactions()) + "\nOverdraft Preference: " + odToString();
- }
- //Sarah Mottram
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement