Untitled
unknown
java
2 years ago
4.1 kB
2
Indexable
Never
/* ****************************** * Undo implemented here * ****************************** */ import java.util.ArrayList; import java.util.LinkedList; public class Student implements Borrower { private String UID; private String firstName; private String lastName; private MyDate joinDate; private String department; public int sa3aCounter; public static ArrayList<Borrower> students = new ArrayList<Borrower>(); LinkedList<Student> snapshots = new LinkedList<Student>(); LinkedList<Student> snapshotsRedo = new LinkedList<Student>(); public Student(String UID, String firstName, String lastName, MyDate joinDate, String department) { this.UID = UID; this.firstName = firstName; this.lastName = lastName; this.joinDate = joinDate; this.department = department; students.add(this); } public Student(String UID, String firstName, String lastName, MyDate joinDate, String department, String s) { this.UID = UID; this.firstName = firstName; this.lastName = lastName; this.joinDate = joinDate; this.department = department; } public void snapshot() { Student s = new Student(UID, firstName, lastName, joinDate, department, "snapshot"); snapshots.add(s); } public void undo() { snapshotsRedo.add(snapshots.pollLast()); sa3aCounter++; Student s = snapshots.peekLast(); if (s != null) { this.UID = s.UID; this.department = s.department; this.firstName = s.firstName; this.lastName = s.lastName; this.joinDate = s.joinDate; } else { System.out.println("You cannot undo, there's no previous state..."); } } public void redo() { snapshots.add(snapshotsRedo.get(sa3aCounter)); Student s = snapshots.peekLast(); if (s != null) { this.UID = s.UID; this.department = s.department; this.firstName = s.firstName; this.lastName = s.lastName; this.joinDate = s.joinDate; } else { System.out.println("You cannot redo, there's no previous state..."); } } public Student() { } public Borrower setUID(String UID) { this.UID = UID; snapshot(); return this; } public Borrower setFirstName(String firstName) { this.firstName = firstName; snapshot(); return this; } public Borrower setLastName(String lastName) { this.lastName = lastName; snapshot(); return this; } public Borrower setJoinDate(MyDate joinDate) { this.joinDate = joinDate; snapshot(); return this; } public Borrower setDepartment(String department) { this.department = department; snapshot(); return this; } public void add(Borrower s) { students.add(s); } public String getUID() { return this.UID; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public MyDate getJoinDate() { return this.joinDate; } public String getDepartment() { return this.department; } public static void printArr() { for (Borrower student : students) { System.out.println(student); } } public static boolean checkRegistered(String UID) { for (Borrower student : students) { if (UID.equals(student.getUID())) { return true; } } return false; } @Override public String toString() { return "{" + " UID='" + getUID() + "'" + ", firstName='" + getFirstName() + "'" + ", lastName='" + getLastName() + "'" + ", joinDate='" + getJoinDate() + "'" + ", department='" + getDepartment() + "'" + "}"; } }