diff --git a/src/myArrayList.java b/src/myArrayList.java new file mode 100644 index 0000000000000000000000000000000000000000..7671a46f8f415ff09e6acce6a83c1c3322ce26fe --- /dev/null +++ b/src/myArrayList.java @@ -0,0 +1,66 @@ +public class myArrayList<T> { + + private Object[] data; + private int size; + private static final int DEFAULT_CAPACITY = 10; + public myArrayList() { + this.data = new Object[DEFAULT_CAPACITY]; + this.size = 0; + } + + public int size() { + return size; + } + + public void add(T element) { + ensureCapacity(); + data[size++] = element; + } + + public T get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + return (T) data[index]; + } + + public void remove(T element) { + for(int i = 0; i < size; i++){ + if(data[i].equals(element)){ + remove(i); + return; + } + } + } + + public void remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + + for(int i = index; i < size - 1; i++){ + data[i] = data[i + 1]; + } + data[size - 1] = null; + size--; + } + + public boolean contains(T element) { + for (int i = 0; i < size; i++) { + if (data[i] != null && data[i].equals(element)) { + return true; + } + } + return false; + } + + private void ensureCapacity() { + if (size == data.length) { + int newCapacity = data.length * 2; + Object[] newData = new Object[newCapacity]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; + } + } + } + diff --git a/src/user.java b/src/user.java index 82b63a22ebaff5c6d6b59201ef96e05ae5a636a5..ff58786b9765381d71c846fd333655da6ec66655 100644 --- a/src/user.java +++ b/src/user.java @@ -9,14 +9,14 @@ import java.util.ArrayList; public class user { private int userID; - private ArrayList<String> files; + private myArrayList<String> files; private String fullName; private int age; private String role; public user(int userID, String fullName, int age, String role) { this.userID = userID; - this.files = new ArrayList<>(); + this.files = new myArrayList<>(); this.fullName = fullName; this.age = age; this.role = role; @@ -30,11 +30,11 @@ public class user { this.userID = userID; } - public ArrayList<String> getFiles() { + public myArrayList<String> getFiles() { return files; } - public void setFiles(ArrayList<String> files) { + public void setFiles(myArrayList<String> files) { this.files = files; } @@ -79,7 +79,8 @@ public class user { public String viewFile(String fileName) throws IOException { if (files != null) { - for (String file: files) { + for (int i = 0; i < files.size();i ++) { + String file = files.get(i); if (file.equals(fileName)) { File fileObject = new File(file); if (!fileObject.canRead()) { @@ -102,7 +103,8 @@ public class user { public void deleteFile(String fileName) throws IOException { if (files != null){ File fileToDelete = null; - for (String file : files){ + for (int i = 0; i < files.size();i++){ + String file = files.get(i); if(file.equals(fileName)){ fileToDelete = new File(file); break;