Skip to content

Commit f99e013

Browse files
committed
Lab5 - done
1 parent 39859cd commit f99e013

30 files changed

+85
-13
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

lab05/command/.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

lab05/command/src/main/java/command/AddTransactionCommand.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ public AddTransactionCommand(Transaction transactionToAdd, Account account) {
1414
}
1515
@Override
1616
public void execute() {
17-
account.addTransaction(transactionToAdd);
17+
this.account.addTransaction(transactionToAdd);
1818
}
1919

2020
@Override
2121
public String getName() {
2222
return "New transaction: " + transactionToAdd.toString();
2323
}
24+
25+
@Override
26+
public void undo() {
27+
this.account.removeTransaction(this.transactionToAdd);
28+
}
29+
30+
@Override
31+
public void redo() {
32+
this.account.addTransaction(this.transactionToAdd);
33+
}
2434
}

lab05/command/src/main/java/command/Command.java

+4
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ public interface Command {
55
void execute();
66

77
String getName();
8+
9+
public void undo();
10+
11+
public void redo();
812
}

lab05/command/src/main/java/command/CommandRegistry.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import javafx.collections.FXCollections;
44
import javafx.collections.ObservableList;
55

6+
import java.util.concurrent.ConcurrentMap;
7+
68
public class CommandRegistry {
79

8-
private ObservableList<Command> commandStack = FXCollections
10+
private final ObservableList<Command> commandStack = FXCollections
11+
.observableArrayList();
12+
13+
private final ObservableList<Command> undoneCommandStack = FXCollections
914
.observableArrayList();
1015

1116
public void executeCommand(Command command) {
@@ -14,14 +19,26 @@ public void executeCommand(Command command) {
1419
}
1520

1621
public void redo() {
17-
22+
if(!this.undoneCommandStack.isEmpty()) {
23+
Command latestUndoneCommand = this.undoneCommandStack.remove(this.undoneCommandStack.size() - 1);
24+
latestUndoneCommand.redo();
25+
this.commandStack.add(latestUndoneCommand);
26+
}
1827
}
1928

2029
public void undo() {
21-
30+
if (!this.commandStack.isEmpty()) {
31+
Command latestCommand = this.commandStack.remove(this.commandStack.size() - 1);
32+
latestCommand.undo();
33+
this.undoneCommandStack.add(latestCommand);
34+
}
2235
}
2336

2437
public ObservableList<Command> getCommandStack() {
2538
return commandStack;
2639
}
40+
41+
public void clearUndoneCommandStack() {
42+
this.undoneCommandStack.clear();
43+
}
2744
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package command;
2+
3+
import model.Account;
4+
import model.Transaction;
5+
6+
public class RemoveTransactionCommand implements Command {
7+
private final Transaction transactionToRemove;
8+
9+
private final Account account;
10+
11+
public RemoveTransactionCommand(Transaction transactionToRemove, Account account) {
12+
this.transactionToRemove = transactionToRemove;
13+
this.account = account;
14+
}
15+
16+
@Override
17+
public void execute() {
18+
this.account.removeTransaction(transactionToRemove);
19+
}
20+
21+
@Override
22+
public String getName() {
23+
return "Removed transaction: " + transactionToRemove.toString();
24+
}
25+
26+
@Override
27+
public void undo() {
28+
this.account.addTransaction(this.transactionToRemove);
29+
}
30+
31+
@Override
32+
public void redo() {
33+
this.account.removeTransaction(this.transactionToRemove);
34+
}
35+
}

lab05/command/src/main/java/controller/AccountAppController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void initRootLayout() {
3535
// set initial data into controller
3636
AccountOverviewController controller = loader.getController();
3737
controller.setAppController(this);
38-
controller.setData(DataGenerator.generateAccountData());
38+
controller.setAccount(DataGenerator.generateAccountData());
3939
controller.setCommandRegistry(commandRegistry);
4040

4141
// add layout to a scene and show them all

lab05/command/src/main/java/controller/AccountOverviewController.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.LocalDate;
55

66
import command.AddTransactionCommand;
7+
import command.RemoveTransactionCommand;
78
import javafx.beans.binding.Bindings;
89
import javafx.event.ActionEvent;
910
import javafx.fxml.FXML;
@@ -21,7 +22,7 @@
2122

2223
public class AccountOverviewController {
2324

24-
private Account data;
25+
private Account account;
2526

2627
private AccountAppController appController;
2728

@@ -96,8 +97,13 @@ private void initialize() {
9697
private void handleDeleteAction(ActionEvent event) {
9798
for (Transaction transaction : transactionsTable.getSelectionModel()
9899
.getSelectedItems()) {
99-
data.removeTransaction(transaction);
100+
101+
RemoveTransactionCommand removeTransactionCommand = new RemoveTransactionCommand(transaction,
102+
this.account);
103+
this.commandRegistry.executeCommand(removeTransactionCommand);
100104
}
105+
106+
commandRegistry.clearUndoneCommandStack();
101107
}
102108

103109
@FXML
@@ -114,7 +120,7 @@ private void handleAddAction(ActionEvent event) {
114120
Transaction transaction = Transaction.newTransaction();
115121

116122
if (appController.showTransactionEditDialog(transaction)) {
117-
AddTransactionCommand addTransactionCommand = new AddTransactionCommand(transaction, data);
123+
AddTransactionCommand addTransactionCommand = new AddTransactionCommand(transaction, account);
118124
commandRegistry.executeCommand(addTransactionCommand);
119125
}
120126
}
@@ -129,9 +135,9 @@ private void handleRedoAction(ActionEvent event) {
129135
commandRegistry.redo();
130136
}
131137

132-
public void setData(Account acccount) {
133-
this.data = acccount;
134-
transactionsTable.setItems(data.getTransactions());
138+
public void setAccount(Account acccount) {
139+
this.account = acccount;
140+
transactionsTable.setItems(account.getTransactions());
135141
}
136142

137143
public void setAppController(AccountAppController appController) {
Binary file not shown.
Binary file not shown.
1.12 KB
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

lab05/mvp/.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

0 commit comments

Comments
 (0)