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;
- 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 Object[capacity];
- }
- // Simple method that returns size
- public int 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 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;
- return true;
- }
- /* Method returns the i-th element of the list */
- public E get(int i)
- {
- return (E) array[i];
- }
- 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()
- {
- return (E[]) array;
- }
- }
- public class UserStore implements IUserStore {
- MyList<User> allUsers = new MyList<User>(); // 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(uid) != -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 = new MyList<User>();
- for (int i = 0; i < allUsers.size(); i++)
- {
- if (allUsers.get(i).getName().Contains(query))
- usersContaining.add(allUsers.get(i));
- }
- return null;
- }
- public User[] getUsersJoinedBefore(Date dateBefore) {
- sortByDate(0, allUsers.size() - 1);
- MyList<User> usersBefore = new MyList<User>();
- int i = 0;
- while (allUsers.get(i).getDateJoined() < dateBefore)
- usersBefore.add(allUsers.get(i));
- return usersBefore.ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment