I am implementing the favourite student system, I have created an interface called SortedListInterface that contain some basic operation that a list can do. it is implemented by an ADT called SortedArrayList that defines all the basic operations in the interface. I also created 3 entity class which is User, Admin and Canditate, and Admin and Candidate is inherited from the User class. I wanted to write a simple driver that can Add, Delete, View, and update the student. I have successfully done the Add function into an array, but when I want to implement the delete function it keeps an error or even nothing happens. Here are my Source Code
Interface
package adt; /** * * @author Alvin * */ public interface SortedListInterface<T> { public boolean add(T newEntry); public boolean remove(T anEntry); public boolean contains(T anEntry); public T get(int givenPosition); public int getNumberOfEntries(); void doubleArray(); void makeRoom(int newPosition); void removeGap(int givenPosition); boolean isArrayFull(); boolean isEmpty(); }
ADT
package adt; /** * * @author Alvin * @param <T> * */ public class SortedArrayList<T extends Comparable<T>> implements SortedListInterface<T> { private T[] array; private int numberOfEntries; private static final int DEFAULT_CAPACITY = 25; public SortedArrayList() { this(DEFAULT_CAPACITY); } public SortedArrayList(int initialCapacity) { numberOfEntries = 0; array = (T[]) new Comparable[initialCapacity]; } @Override public boolean add(T newEntry) { int i = 0; while (i < numberOfEntries && newEntry.compareTo(array[i]) > 0) { i++; } makeRoom(i + 1); array[i] = newEntry; numberOfEntries++; return true; } @Override public boolean remove(T anEntry) { if (numberOfEntries == 0) { return false; } else { int index = 0; while (index < numberOfEntries && array[index].compareTo(anEntry) < 0) { index++; } if (array[index].equals(anEntry)) { removeGap(index + 1); numberOfEntries--; return true; } } return false; } public void clear() { numberOfEntries = 0; } @Override public boolean contains(T anEntry) { boolean found = false; for (int index = 0; !found && (index < numberOfEntries); index++) { if (anEntry.equals(array[index])) { found = true; } } return found; } @Override public int getNumberOfEntries() { return numberOfEntries; } @Override public boolean isEmpty() { return numberOfEntries == 0; } @Override public String toString() { String outputStr = ""; for (int index = 0; index < numberOfEntries; ++index) { outputStr += array[index] + "\n"; } return outputStr; } @Override public boolean isArrayFull() { return numberOfEntries == array.length; } @Override public void doubleArray() { T[] oldList = array; int oldSize = oldList.length; array = (T[]) new Object[2 * oldSize]; for (int index = 0; index < oldSize; index++) { array[index] = oldList[index]; } } @Override public void makeRoom(int newPosition) { int newIndex = newPosition - 1; int lastIndex = numberOfEntries - 1; for (int index = lastIndex; index >= newIndex; index--) { array[index + 1] = array[index]; } } @Override public void removeGap(int givenPosition) { int removedIndex = givenPosition - 1; int lastIndex = numberOfEntries - 1; for (int index = removedIndex; index < lastIndex; index++) { array[index] = array[index + 1]; } } @Override public T get(int givenPosition) { T result = array[givenPosition]; return result; } }
Entity
User
package entity; /** * * @author Alvin */ public class User implements Comparable<User> { private int id; private String name; public User(int id) { this.id = id; } public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(User o) { return (int) (this.id - o.id); } }
Admin
package entity; /** * * @author Alvin */ public class Admin extends User { private String password; public Admin(int id, String name, String password) { super(id, name); this.password = password; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String getName() { return super.getName(); } @Override public int getId() { return super.getId(); } @Override public String toString() { return "Admin\n" + "ID :" + getId() + "\nName :" + getName() + "\nPassword :" + password; } }
Candidate
package entity; /** * * @author Alvin */ public class Candidate extends User { private int vote = 0; public Candidate(int id) { super(id); } public Candidate(int id, String name) { super(id, name); } public Candidate(int id, String name, int vote) { super(id, name); this.vote = vote; } public int getVote() { return vote; } public void setVote(int vote) { this.vote = vote; } @Override public String getName() { return super.getName(); } @Override public int getId() { return super.getId(); } @Override public String toString() { return "Candidate\n" + "ID :" + getId() + "\nName :" + getName() + "\nvote :" + vote + "\n"; } }
Driver
package driver; import adt.*; import entity.*; import java.util.Scanner; /** * * @author Alvin */ public class AdminDriver { Scanner input = new Scanner(System.in); SortedListInterface<User> stuList = new SortedArrayList<>(); public static void main(String[] args) { AdminDriver am = new AdminDriver(); am.menu(); } public void addStudent() { SortedListInterface<User> adminList = new SortedArrayList<>(); adminList.add(new Admin(122, "chick**strong text**", "password123")); adminList.add(new Admin(111, "chickson", "password123")); System.out.println("Enter candidate ID:"); int adminId = input.nextInt(); input.nextLine(); // consume newline character System.out.println("Enter candidate name:"); String adminName = input.nextLine(); stuList.add(new Candidate(adminId, adminName)); System.out.println("Candidate added successfully."); } public void deleteStudent() { System.out.println("Enter candidate ID:"); int adminId = input.nextInt(); input.nextLine(); // consume newline character System.out.println("Enter candidate name:"); String adminName = input.nextLine(); Candidate candidateToDelete = new Candidate(adminId, adminName); boolean success = stuList.remove(candidateToDelete); if (success) { System.out.println("Candidate removed successfully."); } else { System.out.println("Candidate not found."); } } public void menu() { int choice; do { System.out.println("\nPlease choose an option:"); System.out.println("1. Add a new student"); System.out.println("2. Delete a student"); System.out.println("3. Update a student's name"); System.out.println("4. Check if a student exists"); System.out.println("5. View a student's information"); System.out.println("6. Generate student report"); System.out.println("7. Exit"); System.out.print("Choice: "); choice = input.nextInt(); switch (choice) { case 1: addStudent(); break; case 2: deleteStudent(); break; case 3: break; case 4: break; case 5: checkExist(); break; case 6: System.out.println("Candidate \n\n" + stuList); break; case 7: System.out.println("Exiting program..."); break; default: System.out.println("Invalid choice. Please choose a number from 1 to 7."); break; } } while (choice != 7); } public void checkExist() { System.out.println("Enter candidate ID:"); int adminId = input.nextInt(); input.nextLine(); // consume newline character Candidate candidateToCheck = new Candidate(adminId); boolean success = stuList.contains(candidateToCheck); if (success) { System.out.println("Candidate found."); } else { System.out.println("Candidate not found."); } } }
I use
Candidate candidateToDelete = new Candidate(adminId, adminName);
boolean success = stuList.remove(candidateToDelete);
to pass the value to the remove in the ADT but I not showing anything
I also tried this
System.out.println("Enter candidate ID:");
int stuId = input.nextInt();
input.nextLine(); // consume newline character
stuList.remove(new Candidate(stuId));
it nothing happens also
System.out.println("Enter candidate ID:");
int stuId = input.nextInt();
input.nextLine(); // consume newline character
stuList.remove(stuId);
the error happen at the stuId it showed int cannot convert to User.
I hope that you can tell me how to solve it and what I did wrong