Advertisement
Raizekas

Untitled

Mar 14th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 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. public class MyList<E> {   
  15.   private E[] array;            // size of array
  16.   private long size;                // the size of a list (in other words, count of the elements list contains)  (I chose long, assuming there could be high number of users)
  17.   private long capacity;            // the capacity of a list (in other words, how much elements the list could still take before rescaling) (I chose long, assuming there could be high number of users)
  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 E[capacity];
  26.   }
  27.  
  28.   // Simple method that returns size
  29.   public 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 increasing by factor (2) (?)
  42.         E[] temp;
  43.         temp = new E[capacity];
  44.         this.array = temp;
  45.         array[size++] = element;
  46.         return true;
  47.     }
  48.     else
  49.     {
  50.         array[size++] = element;
  51.         return true;
  52.     }
  53.     return false;
  54.   }
  55.  
  56.   /*    Method returns the i-th element of the list */
  57.   public E get(int i)
  58.   {
  59.       return array[i];
  60.   }
  61.  
  62.   public swap(int i, int j)
  63.   {
  64.       E temp = array[i];
  65.       array[i] = array[j];
  66.       array[j] = temp;
  67.   }
  68.  
  69.  
  70.   /*    Converts the so called list to array.
  71.         Basically, it just returns the array of the class */
  72.   public E[] toArray()
  73.   {
  74.       return array;
  75.   }
  76.  
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84. public class UserStore implements IUserStore {
  85.  
  86.     MyList<User> allUsers;      // all website users
  87.    
  88.     public UserStore() {
  89.    
  90.     }
  91.    
  92.     private void sortById(int left, int right)
  93.     {
  94.         int i = left;
  95.         int j = right;
  96.         int pivot = allUsers.get((left + right) / 2);
  97.        
  98.         while (i <= j)
  99.         {
  100.             while (allUsers.get(i).getId() < pivot)
  101.             {
  102.                 i++;
  103.             }
  104.             while (allUsers.get(i).getId() > pivot)
  105.             {
  106.                 j--;
  107.             }
  108.            
  109.             if (i <= j)
  110.             {
  111.                 allUsers.swap(i, j);
  112.                 i++;
  113.                 j--;
  114.             }
  115.         }      
  116.     }
  117.    
  118.     private void sortByDate(int left, int right)
  119.     {
  120.         int i = left;
  121.         int j = right;
  122.         int pivot = allUsers.get((left + right) / 2);
  123.        
  124.         while (i <= j)
  125.         {
  126.             while (allUsers.get(i).getDateJoined() < pivot)
  127.             {
  128.                 i++;
  129.             }
  130.             while (allUsers.get(i).getDateJoined() > pivot)
  131.             {
  132.                 j--;
  133.             }
  134.            
  135.             if (i <= j)
  136.             {
  137.                 allUsers.swap(i, j);
  138.                 i++;
  139.                 j--;
  140.             }
  141.         }      
  142.     }
  143.    
  144.     public int getUsrIndex(int id)
  145.     {
  146.         int left = 0;
  147.         int right = allUsers.size() - 1;
  148.         while (right >= left)
  149.         {
  150.             if (allUsers.get((left + right) / 2).getId() == id)
  151.                 return (left + right) / 2;
  152.             if (allUsers.get((left + right) / 2).getId() < id)
  153.                 left = (left + right) / 2 + 1;
  154.             if (allUsers.get((left + right) / 2).getId() > id)
  155.                 right = (left + right) / 2 - 1;
  156.         }
  157.         return -1;
  158.     }
  159.    
  160.    
  161.  
  162.     public boolean addUser(User usr)
  163.     {
  164.         sortById(0, allUsers.size() - 1);
  165.         if (getUsrIndex(usr.getId() != -1)
  166.         {
  167.             allUsers.add(usr);
  168.             return true;
  169.         }
  170.         // TODO
  171.         return false;
  172.     }
  173.  
  174.     public User getUser(int uid) {
  175.         if (getUsrIndex(usr.getId() != -1)
  176.             return allUsers.get(getUsrIndex(uid));
  177.         return null;
  178.     }
  179.  
  180.     public User[] getUsers() {
  181.         sortByDate(0, allUsers.size() - 1);
  182.         return allUsers.ToArray();
  183.     }
  184.  
  185.     public User[] getUsersContaining(String query) {
  186.         MyList<User> usersContaining;
  187.        
  188.         return null;
  189.     }
  190.  
  191.     public User[] getUsersJoinedBefore(Date dateBefore) {
  192.         sortByDate(0, allUsers.size() - 1);
  193.         return null;
  194.     }
  195.    
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement