Advertisement
Raizekas

Untitled

Mar 15th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.94 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. {  
  16.     private Object[] array;         // size of array
  17.     private int size;               // the size of a list (in other words, count of the elements list contains)
  18.     private int capacity;           // the capacity of a list (in other words, how much elements the list could still take before rescaling)
  19.     private int factor;             // by what factor multiply the capacity when rescaling
  20.  
  21.     public MyList()
  22.     {
  23.         size = 0;                   // once created, the list should be empty
  24.         capacity = 256;             // since I'm increasing capacity by 2 factor, I decided to make base capacity a factor of 2 too.
  25.         factor = 2;                 // I'll make the factor 2, since I'll be doubling the capacity every time  
  26.         array = new Object[capacity];   // the array based structure.
  27.     }
  28.  
  29.     // Simple method that returns size
  30.     public int size()
  31.     {
  32.         return size;
  33.     }
  34.  
  35.     // Simple method that returns true if the list is empty, false otherwise.
  36.     public boolean isEmpty()
  37.     {
  38.         return size == 0;
  39.     }
  40.    
  41.     /*  Adds element to the end of the array
  42.         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.
  43.         Returns true if element is added, false otherwise. */
  44.     public boolean add(E element)
  45.     {
  46.         if (size >= capacity)
  47.         {
  48.             capacity *= factor;     //  I chose to rescale array by multiplying by factor (2)
  49.             Object[] temp = new Object[size];
  50.             for (int i = 0; i < size; i++)
  51.                 temp[i] = array[i];
  52.             this.array = new Object[capacity];
  53.             for (int i = 0; i < size; i++)
  54.             {
  55.                 array[i] = temp[i];
  56.             }
  57.         }
  58.         // 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)
  59.         if (size > capacity)
  60.             return false;
  61.  
  62.         array[size++] = element;        // adds element to the end of the list
  63.         return true;
  64.     }
  65.  
  66.     /*  Method returns the i-th element of the list
  67.         Method gets i - the index of element
  68.         Return also converts the Object type object to E type object */
  69.     public E get(int i)
  70.     {
  71.         return (E) array[i];
  72.     }
  73.  
  74.     /*  Swaps places of two elements.
  75.         Methods gets i and j - indexes of elements to swap */
  76.     public void swap(int i, int j)
  77.     {
  78.         Object temp = array[i];
  79.         array[i] = array[j];
  80.         array[j] = temp;
  81.     }
  82.  
  83.   /*    Converts the so called list to array.
  84.         Basically, it just returns the array of the class */
  85.     public E[] toArray()
  86.     {
  87.         if (!isEmpty())
  88.             return (E[]) array;
  89.         return null;
  90.     }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. public class UserStore implements IUserStore {
  97.  
  98.     MyList<User> allUsers;      // all website users
  99.    
  100.     // Constructors
  101.     /*  Default constructor.
  102.         Creates a new instance of MyList class */
  103.     public UserStore()
  104.     {
  105.         allUsers = new MyList<User>();
  106.     }
  107.    
  108.     /*  Method that sorts user by their ID
  109.         Method gets the list of users and the start and end of the list to sortByDate
  110.         Method performs recursively, using quickSort algorithm
  111.         Method is useful for the binary search. (as we sort users by IDs and only then search)*/
  112.     private void sortById(MyList<User> l, int left, int right)
  113.     {
  114.         int i = left;
  115.         int j = right;
  116.         int pivot = l.get((left + right) / 2).getId();
  117.        
  118.         while (i <= j)
  119.         {
  120.             while (l.get(i).getId() < pivot)
  121.             {
  122.                 i++;
  123.             }
  124.             while (l.get(i).getId() > pivot)
  125.             {
  126.                 j--;
  127.             }
  128.            
  129.             if (i <= j)
  130.             {
  131.                 l.swap(i, j);
  132.                 i++;
  133.                 j--;
  134.             }
  135.         }      
  136.     }
  137.    
  138.     /*  Method that sorts user by their ID
  139.         Method gets the list of users and the start and end of the list to sortByDate
  140.         Method performs recursively, using quickSort algorithm */
  141.     private void sortByDate(MyList<User> l, int left, int right)
  142.     {
  143.         int i = left;
  144.         int j = right;
  145.         Date pivot = l.get((left + right) / 2).getDateJoined();
  146.        
  147.         while (i <= j)
  148.         {
  149.             while (l.get(i).getDateJoined().after(pivot))
  150.             {
  151.                 i++;
  152.             }
  153.             while (l.get(i).getDateJoined().before(pivot))
  154.             {
  155.                 j--;
  156.             }
  157.            
  158.             if (i <= j)
  159.             {
  160.                 l.swap(i, j);
  161.                 i++;
  162.                 j--;
  163.             }
  164.         }      
  165.     }
  166.    
  167.     /*  Method gets user's index in the list by its ID
  168.         Method gets the user's ID
  169.         Method returns the index of found user, if none is found -1 is return
  170.         NOTE: Method is also useful for the addUser(usr) method, as it can obviously also work similarly to contains() method */
  171.     public int getUsrIndex(int id)
  172.     {
  173.         int left = 0;
  174.         int right = allUsers.size() - 1;
  175.         while (right >= left)
  176.         {
  177.             if (allUsers.get((left + right) / 2).getId() == id)
  178.                 return (left + right) / 2;
  179.             if (allUsers.get((left + right) / 2).getId() < id)
  180.                 left = (left + right) / 2 + 1;
  181.             if (allUsers.get((left + right) / 2).getId() > id)
  182.                 right = (left + right) / 2 - 1;
  183.         }
  184.         return -1;
  185.     }
  186.    
  187.    
  188.     /*  Method checks if there is no user with such ID and add the user to the list
  189.         Method returns true if user was added, false otherwise */
  190.     public boolean addUser(User usr)
  191.     {
  192.         sortById(allUsers, 0, allUsers.size() - 1);
  193.         if (getUsrIndex(usr.getId()) != -1)
  194.         {
  195.             allUsers.add(usr);
  196.             return true;
  197.         }
  198.         return false;
  199.     }
  200.  
  201.     /*  Method returns user with the given ID
  202.         Method returns null if the user with such ID was not found
  203.         Method uses getUsrIndex(id) method*/
  204.     public User getUser(int uid)
  205.     {
  206.         if (getUsrIndex(uid) != -1)
  207.                 return allUsers.get(getUsrIndex(uid));
  208.         return null;
  209.     }
  210.  
  211.     /*  Method returns all users sorted by their date
  212.         Method firstly sorts the list, then returns it.
  213.         Method returns User type array*/
  214.     public User[] getUsers()
  215.     {
  216.         sortByDate(allUsers, 0, allUsers.size() - 1);
  217.         return allUsers.toArray();
  218.     }
  219.  
  220.     /*  Method returns all users, that contain stringquery in their name, sorted by their date
  221.         Method firstly finds all the users that contain the stringquery
  222.         Then method sorts that list
  223.         Method returns User type array*/
  224.     public User[] getUsersContaining(String query)
  225.     {
  226.         MyList<User> usersContaining = new MyList<User>();
  227.         for (int i = 0; i < allUsers.size(); i++)
  228.         {
  229.             if (allUsers.get(i).getName().contains(query))
  230.                 usersContaining.add(allUsers.get(i));
  231.         }
  232.         sortByDate(usersContaining, 0, usersContaining.size() - 1);
  233.         return usersContaining.toArray();
  234.     }
  235.    
  236.     /*  Method returns all users, that joined before dateBefore, sorted by their date
  237.         Method firstly sorts all the rist by date and then just copies variables from the list,
  238.         until dateBefore is reached
  239.         Method returns User type array*/
  240.     public User[] getUsersJoinedBefore(Date dateBefore) {
  241.         sortByDate(allUsers, 0, allUsers.size() - 1);
  242.         MyList<User> usersBefore = new MyList<User>();
  243.         int i = 0;
  244.         while (allUsers.get(i).getDateJoined().before(dateBefore))
  245.             usersBefore.add(allUsers.get(i));
  246.        
  247.         return usersBefore.toArray();
  248.     }
  249.    
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement