private bool studentExists(int id) { /* * pre cond: int id * post cond: true - if student id is found in stack * false - otherwise * * checks if a student id was already used */ Student student; Stack newStack = new Stack(this.repo.getStackCapacity()); bool ok = false; while (!ok && this.repo.getStackSize() > -1) { student = this.repo.getStudent(); if (student.getId() == id) ok = true; newStack.push(student); } while (newStack.getStackSize() > -1) this.repo.addStudent(newStack.pop()); if (ok) return true; else return false; }
public String deleteStudentsUntilGrade(int grade) { /* * pre cond: int grade (the limit grade) * post cond: specific string * * deletes all students until 'grade' is found */ try { Stack newStack = new Stack(this.repo.getStackCapacity()); Student student; while (this.repo.getStackSize() > -1) { student = this.repo.getStudent(); if (student.getGrade() == 10) { this.repo.addStudent(student); return "Students deleted successfully!"; } newStack.push(student); } return "No students with the grade 10 found; all students have been deleted!"; } catch (StudentException ex) { return ex.getMessage(); } }
public String getAllStudents() { /* * pre cond: - * post cond: a string with all the students * or a message if no students have been added * */ String studentList = ""; Student student; if (repo.getStackSize() < 0) return "No students!"; Stack newStack = new Stack(this.repo.getStackCapacity()); while (this.repo.getStackSize() > -1) { student = this.repo.getStudent(); studentList += student.toString() + "\n"; newStack.push(student); } while (newStack.getStackSize() > -1) this.repo.addStudent(newStack.pop()); return studentList; }
public StudentRepository(int capacity) { this.capacity = capacity; this.stack = new Stack(capacity); }