-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndoStack.java
More file actions
executable file
·80 lines (64 loc) · 2.35 KB
/
Copy pathUndoStack.java
File metadata and controls
executable file
·80 lines (64 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hospitalmanagementsystem;
/**
*
* @author egehanhatipoglu
*/
public class UndoStack {
// INNER CLASS: StackNode
// Represents a node in the stack. We store 'Object data' alongside the operation string
// to allow saving entire objects (like a deleted Patient) for restoration later.
public class StackNode {
public String operation;
public Object data;
StackNode next;
StackNode(String operation, Object data) {
this.operation = operation;
this.data = data;
this.next = null;
}
}
private StackNode top;
public UndoStack() {
this.top = null;
}
// Overloaded push for simple operations that don't need object data
public void push(String operation) {
push(operation, null);
}
// Core Push Method: Adds a new operation to the stack.
// The 'data' parameter allows us to save the state of an object before it is modified or deleted,
// which is critical for implementing the Undo logic for "Delete" operations.
public void push(String operation, Object data) {
StackNode newNode = new StackNode(operation, data);
newNode.next = top;
top = newNode;
}
// Standard Pop: Returns only the operation string.
// Useful for simple undo logic where we only need to parse an ID (e.g., "PATIENT_ADD:5").
public String pop() {
if (isEmpty()) return null;
String operation = top.operation;
top = top.next;
return operation;
}
// Critical Method: Returns the entire node including the stored 'data' object.
// This is required when we need to restore a complex object (like adding a deleted Patient back into the system).
public StackNode popWithData() {
if (isEmpty()) return null;
StackNode node = top;
top = top.next;
return node;
}
// Peeks at the stored data object without removing the node.
public Object peekData() {
if (isEmpty()) return null;
return top.data;
}
public boolean isEmpty() {
return top == null;
}
}