Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Your preamble here!
- *
- * @author: u1524027
- */
- package uk.ac.warwick.java.cs126.services;
- import uk.ac.warwick.java.cs126.models.User;
- import java.util.Date;
- class MyList<E>
- {
- private Object[] array; // size of array
- private int size; // the size of a list (in other words, count of the elements list contains)
- private int capacity; // the capacity of a list (in other words, how much elements the list could still take before rescaling)
- private int factor; // by what factor multiply the capacity when rescaling
- public MyList()
- {
- size = 0; // once created, the list should be empty
- capacity = 256; // since I'm increasing capacity by 2 factor, I decided to make base capacity a factor of 2 too.
- factor = 2; // I'll make the factor 2, since I'll be doubling the capacity every time
- array = new Object[capacity]; // the array based structure.
- }
- // Simple method that returns size
- public int size()
- {
- return size;
- }
- // Simple method that returns true if the list is empty, false otherwise.
- public boolean isEmpty()
- {
- return size == 0;
- }
- /* Adds element to the end of the array
- 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.
- Returns true if element is added, false otherwise. */
- public boolean add(E element)
- {
- if (size >= capacity)
- {
- capacity *= factor; // I chose to rescale array by multiplying by factor (2)
- Object[] temp = new Object[size];
- for (int i = 0; i < size; i++)
- temp[i] = array[i];
- this.array = new Object[capacity];
- for (int i = 0; i < size; i++)
- {
- array[i] = temp[i];
- }
- }
- // 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)
- if (size > capacity)
- return false;
- array[size++] = element; // adds element to the end of the list
- return true;
- }
- /* Method returns the i-th element of the list
- Method gets i - the index of element
- Return also converts the Object type object to E type object */
- public E get(int i)
- {
- return (E) array[i];
- }
- /* Swaps places of two elements.
- Methods gets i and j - indexes of elements to swap */
- public void swap(int i, int j)
- {
- Object temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- /* Converts the so called list to array.
- Basically, it just returns the array of the class */
- public E[] toArray()
- {
- if (!isEmpty())
- return (E[]) array;
- return null;
- }
- }
- public class UserStore implements IUserStore {
- MyList<User> allUsers; // all website users
- // Constructors
- /* Default constructor.
- Creates a new instance of MyList class */
- public UserStore()
- {
- allUsers = new MyList<User>();
- }
- /* Method that sorts user by their ID
- Method gets the list of users and the start and end of the list to sortByDate
- Method performs recursively, using quickSort algorithm
- Method is useful for the binary search. (as we sort users by IDs and only then search)*/
- private void sortById(MyList<User> l, int left, int right)
- {
- int i = left;
- int j = right;
- int pivot = l.get((left + right) / 2).getId();
- while (i <= j)
- {
- while (l.get(i).getId() < pivot)
- {
- i++;
- }
- while (l.get(i).getId() > pivot)
- {
- j--;
- }
- if (i <= j)
- {
- l.swap(i, j);
- i++;
- j--;
- }
- }
- }
- /* Method that sorts user by their ID
- Method gets the list of users and the start and end of the list to sortByDate
- Method performs recursively, using quickSort algorithm */
- private void sortByDate(MyList<User> l, int left, int right)
- {
- int i = left;
- int j = right;
- Date pivot = l.get((left + right) / 2).getDateJoined();
- while (i <= j)
- {
- while (l.get(i).getDateJoined().after(pivot))
- {
- i++;
- }
- while (l.get(i).getDateJoined().before(pivot))
- {
- j--;
- }
- if (i <= j)
- {
- l.swap(i, j);
- i++;
- j--;
- }
- }
- }
- /* Method gets user's index in the list by its ID
- Method gets the user's ID
- Method returns the index of found user, if none is found -1 is return
- NOTE: Method is also useful for the addUser(usr) method, as it can obviously also work similarly to contains() method */
- public int getUsrIndex(int id)
- {
- int left = 0;
- int right = allUsers.size() - 1;
- while (right >= left)
- {
- if (allUsers.get((left + right) / 2).getId() == id)
- return (left + right) / 2;
- if (allUsers.get((left + right) / 2).getId() < id)
- left = (left + right) / 2 + 1;
- if (allUsers.get((left + right) / 2).getId() > id)
- right = (left + right) / 2 - 1;
- }
- return -1;
- }
- /* Method checks if there is no user with such ID and add the user to the list
- Method returns true if user was added, false otherwise */
- public boolean addUser(User usr)
- {
- sortById(allUsers, 0, allUsers.size() - 1);
- if (getUsrIndex(usr.getId()) != -1)
- {
- allUsers.add(usr);
- return true;
- }
- return false;
- }
- /* Method returns user with the given ID
- Method returns null if the user with such ID was not found
- Method uses getUsrIndex(id) method*/
- public User getUser(int uid)
- {
- if (getUsrIndex(uid) != -1)
- return allUsers.get(getUsrIndex(uid));
- return null;
- }
- /* Method returns all users sorted by their date
- Method firstly sorts the list, then returns it.
- Method returns User type array*/
- public User[] getUsers()
- {
- sortByDate(allUsers, 0, allUsers.size() - 1);
- return allUsers.toArray();
- }
- /* Method returns all users, that contain stringquery in their name, sorted by their date
- Method firstly finds all the users that contain the stringquery
- Then method sorts that list
- Method returns User type array*/
- public User[] getUsersContaining(String query)
- {
- MyList<User> usersContaining = new MyList<User>();
- for (int i = 0; i < allUsers.size(); i++)
- {
- if (allUsers.get(i).getName().contains(query))
- usersContaining.add(allUsers.get(i));
- }
- sortByDate(usersContaining, 0, usersContaining.size() - 1);
- return usersContaining.toArray();
- }
- /* Method returns all users, that joined before dateBefore, sorted by their date
- Method firstly sorts all the rist by date and then just copies variables from the list,
- until dateBefore is reached
- Method returns User type array*/
- public User[] getUsersJoinedBefore(Date dateBefore) {
- sortByDate(allUsers, 0, allUsers.size() - 1);
- MyList<User> usersBefore = new MyList<User>();
- int i = 0;
- while (allUsers.get(i).getDateJoined().before(dateBefore))
- usersBefore.add(allUsers.get(i));
- return usersBefore.toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement