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;
- public class MyList<E> {
- private E[] array; // size of array
- 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)
- 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)
- private int factor;
- public MyList()
- {
- size = 0;
- capacity = 256; // since I'm increasing capacity by 2 factor, I decided to make base capacity a factor of 2 too.
- factor = 2;
- array = new E[capacity];
- }
- // Simple method that returns size
- public size()
- {
- return size;
- }
- /* 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 increasing by factor (2) (?)
- E[] temp;
- temp = new E[capacity];
- this.array = temp;
- array[size++] = element;
- return true;
- }
- else
- {
- array[size++] = element;
- return true;
- }
- return false;
- }
- /* Method returns the i-th element of the list */
- public E get(int i)
- {
- return array[i];
- }
- public swap(int i, int j)
- {
- E 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()
- {
- return array;
- }
- }
- public class UserStore implements IUserStore {
- MyList<User> allUsers; // all website users
- public UserStore() {
- }
- private void sortById(int left, int right)
- {
- int i = left;
- int j = right;
- int pivot = allUsers.get((left + right) / 2);
- while (i <= j)
- {
- while (allUsers.get(i).getId() < pivot)
- {
- i++;
- }
- while (allUsers.get(i).getId() > pivot)
- {
- j--;
- }
- if (i <= j)
- {
- allUsers.swap(i, j);
- i++;
- j--;
- }
- }
- }
- private void sortByDate(int left, int right)
- {
- int i = left;
- int j = right;
- int pivot = allUsers.get((left + right) / 2);
- while (i <= j)
- {
- while (allUsers.get(i).getDateJoined() < pivot)
- {
- i++;
- }
- while (allUsers.get(i).getDateJoined() > pivot)
- {
- j--;
- }
- if (i <= j)
- {
- allUsers.swap(i, j);
- i++;
- j--;
- }
- }
- }
- 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;
- }
- public boolean addUser(User usr)
- {
- sortById(0, allUsers.size() - 1);
- if (getUsrIndex(usr.getId() != -1)
- {
- allUsers.add(usr);
- return true;
- }
- // TODO
- return false;
- }
- public User getUser(int uid) {
- if (getUsrIndex(usr.getId() != -1)
- return allUsers.get(getUsrIndex(uid));
- return null;
- }
- public User[] getUsers() {
- sortByDate(0, allUsers.size() - 1);
- return allUsers.ToArray();
- }
- public User[] getUsersContaining(String query) {
- MyList<User> usersContaining;
- return null;
- }
- public User[] getUsersJoinedBefore(Date dateBefore) {
- sortByDate(0, allUsers.size() - 1);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement