Raizekas

Untitled

Mar 14th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. /**
  2.  * Your preamble here!
  3.  *
  4.  * @author: u1524027
  5.  */
  6.  
  7. package uk.ac.warwick.java.cs126.services;
  8.  
  9. import uk.ac.warwick.java.cs126.models.User;
  10.  
  11. import java.util.Date;
  12.  
  13.  
  14. class MyList<E> {  
  15. private Object[] array;         // size of array
  16. private int size;               // the size of a list (in other words, count of the elements list contains)
  17. private int capacity;           // the capacity of a list (in other words, how much elements the list could still take before rescaling)
  18. private int factor;
  19.  
  20.   public MyList()
  21.   {
  22.     size = 0;
  23.     capacity = 256;             // since I'm increasing capacity by 2 factor, I decided to make base capacity a factor of 2 too.
  24.     factor = 2;            
  25.     array = new Object[capacity];
  26.   }
  27.  
  28.   // Simple method that returns size
  29.   public int size()
  30.   {
  31.     return size;
  32.   }
  33.    
  34.   /*    Adds element to the end of the array
  35.         If array size already exceeds the capacity, it increases the capacity (by copying the array to the new array with bigger capacity)and only then adds the element to the end.
  36.         Returns true if element is added, false otherwise. */
  37.   public boolean add(E element)
  38.   {
  39.     if (size >= capacity)
  40.     {
  41.         capacity *= factor;     //  I chose to rescale array by multiplying by factor (2)
  42.         Object[] temp = new Object[size];
  43.                 for (int i = 0; i < size; i++)
  44.                     temp[i] = array[i];
  45.         this.array = new Object[capacity];
  46.                 for (int i = 0; i < size; i++)
  47.                 {
  48.                     array[i] = temp[i];
  49.                 }
  50.     }
  51.     // basically, if there were some kind of error when increasing the capacity. (theorically, it shouldn't happen, but still since it's a bool method, it should have a false return exit)
  52.     if (size > capacity)
  53.         return false;
  54.    
  55.     array[size++] = element;
  56.     return true;
  57.   }
  58.  
  59.   /*    Method returns the i-th element of the list */
  60.   public E get(int i)
  61.   {
  62.       return (E) array[i];
  63.   }
  64.  
  65.   public void swap(int i, int j)
  66.   {
  67.       Object temp = array[i];
  68.       array[i] = array[j];
  69.       array[j] = temp;
  70.   }
  71.  
  72.  
  73.   /*    Converts the so called list to array.
  74.         Basically, it just returns the array of the class */
  75.   public E[] toArray()
  76.   {
  77.       return (E[]) array;
  78.   }
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87. public class UserStore implements IUserStore {
  88.  
  89.     MyList<User> allUsers = new MyList<User>();     // all website users
  90.    
  91.     public UserStore() {
  92.    
  93.     }
  94.    
  95.     private void sortById(int left, int right)
  96.     {
  97.         int i = left;
  98.         int j = right;
  99.         int pivot = allUsers.get((left + right) / 2);
  100.        
  101.         while (i <= j)
  102.         {
  103.             while (allUsers.get(i).getId() < pivot)
  104.             {
  105.                 i++;
  106.             }
  107.             while (allUsers.get(i).getId() > pivot)
  108.             {
  109.                 j--;
  110.             }
  111.            
  112.             if (i <= j)
  113.             {
  114.                 allUsers.swap(i, j);
  115.                 i++;
  116.                 j--;
  117.             }
  118.         }      
  119.     }
  120.    
  121.     private void sortByDate(int left, int right)
  122.     {
  123.         int i = left;
  124.         int j = right;
  125.         int pivot = allUsers.get((left + right) / 2);
  126.        
  127.         while (i <= j)
  128.         {
  129.             while (allUsers.get(i).getDateJoined() < pivot)
  130.             {
  131.                 i++;
  132.             }
  133.             while (allUsers.get(i).getDateJoined() > pivot)
  134.             {
  135.                 j--;
  136.             }
  137.            
  138.             if (i <= j)
  139.             {
  140.                 allUsers.swap(i, j);
  141.                 i++;
  142.                 j--;
  143.             }
  144.         }      
  145.     }
  146.    
  147.     public int getUsrIndex(int id)
  148.     {
  149.         int left = 0;
  150.         int right = allUsers.size() - 1;
  151.         while (right >= left)
  152.         {
  153.             if (allUsers.get((left + right) / 2).getId() == id)
  154.                 return (left + right) / 2;
  155.             if (allUsers.get((left + right) / 2).getId() < id)
  156.                 left = (left + right) / 2 + 1;
  157.             if (allUsers.get((left + right) / 2).getId() > id)
  158.                 right = (left + right) / 2 - 1;
  159.         }
  160.         return -1;
  161.     }
  162.    
  163.    
  164.  
  165.     public boolean addUser(User usr)
  166.     {
  167.         sortById(0, allUsers.size() - 1);
  168.         if (getUsrIndex(usr.getId() != -1))
  169.         {
  170.             allUsers.add(usr);
  171.             return true;
  172.         }
  173.         // TODO
  174.         return false;
  175.     }
  176.  
  177.     public User getUser(int uid)
  178.     {
  179.         if (getUsrIndex(uid) != -1)
  180.                 return allUsers.get(getUsrIndex(uid));
  181.         return null;
  182.     }
  183.  
  184.     public User[] getUsers() {
  185.         sortByDate(0, allUsers.size() - 1);
  186.         return allUsers.ToArray();
  187.     }
  188.  
  189.     public User[] getUsersContaining(String query) {
  190.         MyList<User> usersContaining = new MyList<User>();
  191.         for (int i = 0; i < allUsers.size(); i++)
  192.         {
  193.             if (allUsers.get(i).getName().Contains(query))
  194.                 usersContaining.add(allUsers.get(i));
  195.         }
  196.         return null;
  197.     }
  198.  
  199.     public User[] getUsersJoinedBefore(Date dateBefore) {
  200.         sortByDate(0, allUsers.size() - 1);
  201.         MyList<User> usersBefore = new MyList<User>();
  202.         int i = 0;
  203.         while (allUsers.get(i).getDateJoined() < dateBefore)
  204.             usersBefore.add(allUsers.get(i));
  205.        
  206.         return usersBefore.ToArray();
  207.     }
  208.    
  209. }
Advertisement
Add Comment
Please, Sign In to add comment