Guest User

Untitled

a guest
Jun 25th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. public static Cons bank(Cons accounts, Cons updates) {
  2.   updates = llmergesort(updates);
  3.   System.out.println("Sorted: " + updates);
  4.   Cons newAccounts = null;
  5.   Account currentAct = new Account("" , 0);
  6.  
  7.   return llmergesort(bankb(accounts, updates, newAccounts, currentAct));
  8. }
  9.  
  10. public static Cons bankb(Cons accounts, Cons updates, Cons newAccounts, Account currentAct)
  11. {
  12.   if(accounts == null && updates == null)
  13.   {
  14.     System.out.println("Done");
  15.     return newAccounts;
  16.   }
  17.   //No more accounts, check updates for positives.
  18.   else if(accounts == null)
  19.   {
  20.     System.out.println("No more accounts: " + first(updates));
  21.     if(((Account)first(updates)).amount() >= 0) //check if it's greater than 0
  22.     {
  23.       int newAmount = 0;
  24.       //is the previous update the same person?
  25.       if((((Account)first(updates)).name()).equals(currentAct.name())) //Yes
  26.       {
  27.         newAmount = ((Account)first(updates)).amount() + ((Account)currentAct).amount(); //add new update & old update
  28.         if(newAmount < 0)
  29.         {
  30.           newAmount -= 30;
  31.         }
  32.       }
  33.       else
  34.       {
  35.         newAmount = ((Account)first(updates)).amount();
  36.       }
  37.      
  38.       currentAct = currentAct.account(((Account)first(updates)).name(), newAmount);
  39.       return bankb(accounts, rest(updates), newAccounts, currentAct);
  40.     }
  41.     else //not greater than 0, so move on to next update
  42.     {
  43.       return bankb(accounts, rest(updates), newAccounts, currentAct);
  44.     }
  45.   }
  46.  
  47.   //No more updates, add on remaining accounts
  48.   else if(updates == null)
  49.   {
  50.     System.out.println("No updates left: " + first(accounts));
  51.     //currentAct has something
  52.     if(!currentAct.name().equals(""))
  53.     {
  54.       newAccounts = cons(currentAct, newAccounts);
  55.       currentAct = currentAct.account("", 0);
  56.      
  57.       return bankb(rest(accounts), updates, newAccounts, currentAct);
  58.     }
  59.     //new Accounts
  60.     else
  61.     {
  62.       newAccounts = cons(((Account)first(accounts)), newAccounts);
  63.       return bankb(rest(accounts), updates, newAccounts, currentAct);
  64.     }
  65.   }
  66.  
  67.   //Update involves current account
  68.   else if((((Account)first(updates)).name()).equals(((Account)first(accounts)).name()))
  69.   {
  70.     int newAmount = 0;
  71.    
  72.    
  73.     System.out.println("Update " + first(accounts) + " with " + first(updates) );
  74.       //is the currentAct I've stored same as the account I'm working on now?
  75.     if(((Account)currentAct).name().equals(((Account)first(accounts)).name()))
  76.     {
  77.       //Yes, so add previous update
  78.    
  79.       newAmount += currentAct.amount() + ((Account)first(updates)).amount();
  80.      
  81.       System.out.println("Carryover Account newAmount: " + newAmount);
  82.     }
  83.     else
  84.     {
  85.       //No, so store the currentAct from before & add update with account
  86.       if(!currentAct.name().equals(""))
  87.       {
  88.         newAccounts = cons(currentAct, newAccounts);
  89.       }
  90.      
  91.       //reset currentAct
  92.       currentAct = currentAct.account("", 0);
  93.      
  94.       newAmount = ((Account)first(accounts)).amount() + ((Account)first(updates)).amount();
  95.       //first one
  96.       if(newAmount < 0 && newAccounts == null)
  97.       {
  98.         newAmount -= 30;
  99.         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));
  100.       }
  101.       System.out.println("New Account newAmount: " + newAmount);
  102.     }
  103.    
  104.     //checks the rest
  105.     if(currentAct.amount() < 0)
  106.       {
  107.         newAmount -= 30;
  108.       }
  109.     currentAct = currentAct.account(((Account)first(accounts)).name(), newAmount);
  110.    
  111.     return bankb(accounts, rest(updates), newAccounts, currentAct);
  112.   }
  113.  
  114.   //account exists but has no update
  115.   else if(!memberBool(((Account)first(accounts)), updates))
  116.   {
  117.     System.out.println("Account has no updates: " + first(accounts));
  118.     //if the account hasn't been updating and is entirely new, add it on to newAccounts
  119.     if(!(currentAct.name()).equals(((Account)first(accounts)).name()))
  120.     {
  121.       newAccounts = cons(first(accounts), newAccounts);
  122.     }
  123.    
  124.     return bankb(rest(accounts), updates, newAccounts, currentAct);
  125.   }
  126.  
  127.   //update is not an account
  128.   else
  129.   {
  130.     System.out.println("Update not a member of Accounts: " + first(updates));
  131.     //Is the account's amount positive?
  132.     if(((Account)first(updates)).amount() >= 0)
  133.     {
  134.       int newAmount = 0;
  135.       //Is previous update same person?
  136.       if(currentAct.name().equals(((Account)first(updates)).name()))
  137.       {  
  138.         //Yes, so add previous update and store it
  139.         newAmount = currentAct.amount() + ((Account)first(updates)).amount();
  140.         currentAct = currentAct.account(((Account)first(updates)).name(), newAmount);
  141.        
  142.         System.out.println("Just stored: " + currentAct);
  143.        
  144.         return bankb(accounts, rest(updates), newAccounts, currentAct);
  145.       }
  146.       else
  147.       {
  148.         //reset currentAct
  149.         currentAct = currentAct.account("", 0);
  150.      
  151.         newAmount = ((Account)first(updates)).amount();
  152.         System.out.println("newAmount: " + newAmount);
  153.        
  154.         currentAct = currentAct.account(((Account)first(updates)).name(), newAmount);
  155.         System.out.println("Account added: " + currentAct);
  156.        
  157.         return bankb(accounts, rest(updates), newAccounts, currentAct);
  158.        
  159.       }
  160.     }
  161.    
  162.     else
  163.     {
  164.       System.out.println("Account denied: " + first(updates));
  165.       return bankb(accounts, rest(updates), newAccounts, currentAct);
  166.     }
  167.   }
  168. }
Add Comment
Please, Sign In to add comment