The purpose of this function is to find some students that match the ID which I entered in the console. Instead of adding some student to the list at first, I try to Update/Delete student using their ID, and then the if-statement runs, not the else- statement. Is the arraylist I passed to the function is empty at first ?
This is the function
public void updateAndDelete(ArrayList<Student> listStudent) {
String choice;
String idKey;
ArrayList<Student> keyStudent = new ArrayList<Student>();
// Add students to display
System.out.print("Enter student's ID to find: ");
idKey = sc.nextLine().trim();
for (int i = 0; i < listStudent.size(); i++) {
if (listStudent.get(i).getId().equals(idKey)) {
keyStudent.add(listStudent.get(i));
}
if (keyStudent.isEmpty()) {
System.out.println("No student matches!");
return;
}
}
display(keyStudent);
// Update - Delete
do {
System.out.println("Do you want to update (U) or delete (D) ?");
choice = sc.nextLine().trim();
if (choice.toUpperCase().equals("U")) {
for (int i = 0; i < listStudent.size(); i++) {
if (listStudent.get(i).getId().equals(idKey)) {
System.out.print("Update student name: ");
listStudent.get(i).setName(sc.nextLine().trim());
System.out.print("Update student address: ");
listStudent.get(i).setAddress(sc.nextLine().trim());
if (listStudent.get(i) instanceof IT) {
// Validate Java score
String input;
do {
System.out.print("Update Java score: ");
input = sc.nextLine().trim();
if (!input.matches(dv.scorePattern)) {
System.out.println("Wrong input!");
}
} while (!input.matches(dv.scorePattern));
((IT) listStudent.get(i)).setJavaScore(Double.valueOf(input));
// Validate CSS score
do {
System.out.print("Update CSS score: ");
input = sc.nextLine().trim();
if (!input.matches(dv.scorePattern)) {
System.out.println("Wrong input!");
}
} while (!input.matches(dv.scorePattern));
((IT) listStudent.get(i)).setCssScore(Double.valueOf(input));
}
if (listStudent.get(i) instanceof Biz) {
String input;
// Validate Accounting score
do {
System.out.print("Enter Accounting score: ");
input = sc.nextLine().trim();
if (!input.matches(dv.scorePattern)) {
System.out.println("Wrong input!");
}
} while (!input.matches(dv.scorePattern));
((Biz) listStudent.get(i)).setAccScore(Double.valueOf(input));
// Validate Marketing score
do {
System.out.print("Enter Marketing score: ");
input = sc.nextLine().trim();
if (!input.matches(dv.scorePattern)) {
System.out.println("Wrong input!");
}
} while (!input.matches(dv.scorePattern));
((Biz) listStudent.get(i)).setMarScore(Double.valueOf(input));
}
}
}
System.out.println("Student updated!");
return;
} else if (choice.toUpperCase().equals("D")) {
for (int i = 0; i < listStudent.size(); i++) {
if (listStudent.get(i).getId().equals(idKey)) {
listStudent.remove(i);
}
}
System.out.println("Student removed!");
return;
} else {
System.out.println("Wrong input!");
}
} while (!(choice.equals("U") || choice.equals("D")));
}
This is display function:
public void display(ArrayList<Student> listStudent) {
// Alignment
String alignment = "| %-8s | %-20s | %-12s | %-5.1f | %-5.1f | %-5.1f | %-5.1f | %-5.1f |%n";
System.out
.format("+----------+----------------------+--------------+-------+-------+-------+-------+-------+%n");
System.out
.format("| ID | Name | Address | Java | CSS | Acc. | Mar. | GPA |%n");
System.out
.format("+----------+----------------------+--------------+-------+-------+-------+-------+-------+%n");
// Loop
for (int i = 0; i < listStudent.size(); i++) {
if (listStudent.get(i) instanceof IT) {
System.out.format(alignment, listStudent.get(i).getId(), listStudent.get(i).getName(),
listStudent.get(i).getAddress(), ((IT) listStudent.get(i)).getJavaScore(),
((IT) listStudent.get(i)).getCssScore(), null, null, ((IT) listStudent.get(i)).avgScore());
}
if (listStudent.get(i) instanceof Biz) {
System.out.format(alignment, listStudent.get(i).getId(), listStudent.get(i).getName(),
listStudent.get(i).getAddress(), null, null, ((Biz) listStudent.get(i)).getAccScore(),
((Biz) listStudent.get(i)).getMarScore(), ((Biz) listStudent.get(i)).avgScore());
}
}
System.out
.format("+----------+----------------------+--------------+-------+-------+-------+-------+-------+%n");
}
Thanks for your help!