Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Cons bank(Cons accounts, Cons updates) {
- updates = llmergesort(updates);
- System.out.println("Sorted: " + updates);
- Cons newAccounts = null;
- Account currentAct = new Account("" , 0);
- return llmergesort(bankb(accounts, updates, newAccounts, currentAct));
- }
- public static Cons bankb(Cons accounts, Cons updates, Cons newAccounts, Account currentAct)
- {
- if(accounts == null && updates == null)
- {
- System.out.println("Done");
- return newAccounts;
- }
- //No more accounts, check updates for positives.
- else if(accounts == null)
- {
- System.out.println("No more accounts: " + first(updates));
- if(((Account)first(updates)).amount() >= 0) //check if it's greater than 0
- {
- int newAmount = 0;
- //is the previous update the same person?
- if((((Account)first(updates)).name()).equals(currentAct.name())) //Yes
- {
- newAmount = ((Account)first(updates)).amount() + ((Account)currentAct).amount(); //add new update & old update
- if(newAmount < 0)
- {
- newAmount -= 30;
- }
- }
- else
- {
- newAmount = ((Account)first(updates)).amount();
- }
- currentAct = currentAct.account(((Account)first(updates)).name(), newAmount);
- return bankb(accounts, rest(updates), newAccounts, currentAct);
- }
- else //not greater than 0, so move on to next update
- {
- return bankb(accounts, rest(updates), newAccounts, currentAct);
- }
- }
- //No more updates, add on remaining accounts
- else if(updates == null)
- {
- System.out.println("No updates left: " + first(accounts));
- //currentAct has something
- if(!currentAct.name().equals(""))
- {
- newAccounts = cons(currentAct, newAccounts);
- currentAct = currentAct.account("", 0);
- return bankb(rest(accounts), updates, newAccounts, currentAct);
- }
- //new Accounts
- else
- {
- newAccounts = cons(((Account)first(accounts)), newAccounts);
- return bankb(rest(accounts), updates, newAccounts, currentAct);
- }
- }
- //Update involves current account
- else if((((Account)first(updates)).name()).equals(((Account)first(accounts)).name()))
- {
- int newAmount = 0;
- System.out.println("Update " + first(accounts) + " with " + first(updates) );
- //is the currentAct I've stored same as the account I'm working on now?
- if(((Account)currentAct).name().equals(((Account)first(accounts)).name()))
- {
- //Yes, so add previous update
- newAmount += currentAct.amount() + ((Account)first(updates)).amount();
- System.out.println("Carryover Account newAmount: " + newAmount);
- }
- else
- {
- //No, so store the currentAct from before & add update with account
- if(!currentAct.name().equals(""))
- {
- newAccounts = cons(currentAct, newAccounts);
- }
- //reset currentAct
- currentAct = currentAct.account("", 0);
- newAmount = ((Account)first(accounts)).amount() + ((Account)first(updates)).amount();
- //first one
- if(newAmount < 0 && newAccounts == null)
- {
- newAmount -= 30;
- System.out.println(((Account)first(accounts)).name() + " receives an overdraft fee.\nBefore: " + currentAct.account(((Account)first(accounts)).name(), newAmount + 30) + "; After: " + currentAct.account(((Account)first(accounts)).name(), newAmount));
- }
- System.out.println("New Account newAmount: " + newAmount);
- }
- //checks the rest
- if(currentAct.amount() < 0)
- {
- newAmount -= 30;
- }
- currentAct = currentAct.account(((Account)first(accounts)).name(), newAmount);
- return bankb(accounts, rest(updates), newAccounts, currentAct);
- }
- //account exists but has no update
- else if(!memberBool(((Account)first(accounts)), updates))
- {
- System.out.println("Account has no updates: " + first(accounts));
- //if the account hasn't been updating and is entirely new, add it on to newAccounts
- if(!(currentAct.name()).equals(((Account)first(accounts)).name()))
- {
- newAccounts = cons(first(accounts), newAccounts);
- }
- return bankb(rest(accounts), updates, newAccounts, currentAct);
- }
- //update is not an account
- else
- {
- System.out.println("Update not a member of Accounts: " + first(updates));
- //Is the account's amount positive?
- if(((Account)first(updates)).amount() >= 0)
- {
- int newAmount = 0;
- //Is previous update same person?
- if(currentAct.name().equals(((Account)first(updates)).name()))
- {
- //Yes, so add previous update and store it
- newAmount = currentAct.amount() + ((Account)first(updates)).amount();
- currentAct = currentAct.account(((Account)first(updates)).name(), newAmount);
- System.out.println("Just stored: " + currentAct);
- return bankb(accounts, rest(updates), newAccounts, currentAct);
- }
- else
- {
- //reset currentAct
- currentAct = currentAct.account("", 0);
- newAmount = ((Account)first(updates)).amount();
- System.out.println("newAmount: " + newAmount);
- currentAct = currentAct.account(((Account)first(updates)).name(), newAmount);
- System.out.println("Account added: " + currentAct);
- return bankb(accounts, rest(updates), newAccounts, currentAct);
- }
- }
- else
- {
- System.out.println("Account denied: " + first(updates));
- return bankb(accounts, rest(updates), newAccounts, currentAct);
- }
- }
- }
Add Comment
Please, Sign In to add comment