From ddc625338656dfe7084d80bcaaf5d25a8e8b944b Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sun, 13 Oct 2024 18:37:53 +0200 Subject: [PATCH] added lab4 ex2 --- src/lab4_command/ex2/FileAction.java | 8 +++ src/lab4_command/ex2/MenuAction.java | 5 ++ src/lab4_command/ex2/TextEditor.java | 56 +++++++++++++++++++ src/lab4_command/ex2/TextFile.java | 29 ++++++++++ src/lab4_command/ex2/actions/CloseAction.java | 15 +++++ src/lab4_command/ex2/actions/CopyAction.java | 15 +++++ src/lab4_command/ex2/actions/OpenAction.java | 15 +++++ src/lab4_command/ex2/actions/PasteAction.java | 15 +++++ src/lab4_command/ex2/actions/SaveAction.java | 15 +++++ 9 files changed, 173 insertions(+) create mode 100644 src/lab4_command/ex2/FileAction.java create mode 100644 src/lab4_command/ex2/MenuAction.java create mode 100644 src/lab4_command/ex2/TextEditor.java create mode 100644 src/lab4_command/ex2/TextFile.java create mode 100644 src/lab4_command/ex2/actions/CloseAction.java create mode 100644 src/lab4_command/ex2/actions/CopyAction.java create mode 100644 src/lab4_command/ex2/actions/OpenAction.java create mode 100644 src/lab4_command/ex2/actions/PasteAction.java create mode 100644 src/lab4_command/ex2/actions/SaveAction.java diff --git a/src/lab4_command/ex2/FileAction.java b/src/lab4_command/ex2/FileAction.java new file mode 100644 index 0000000..47964a8 --- /dev/null +++ b/src/lab4_command/ex2/FileAction.java @@ -0,0 +1,8 @@ +package lab4_command.ex2; + +public abstract class FileAction implements MenuAction { + protected TextFile file; + public FileAction(TextFile file) { + this.file = file; + } +} diff --git a/src/lab4_command/ex2/MenuAction.java b/src/lab4_command/ex2/MenuAction.java new file mode 100644 index 0000000..60b76ec --- /dev/null +++ b/src/lab4_command/ex2/MenuAction.java @@ -0,0 +1,5 @@ +package lab4_command.ex2; + +public interface MenuAction { + void execute(); +} diff --git a/src/lab4_command/ex2/TextEditor.java b/src/lab4_command/ex2/TextEditor.java new file mode 100644 index 0000000..0d52cdc --- /dev/null +++ b/src/lab4_command/ex2/TextEditor.java @@ -0,0 +1,56 @@ +package lab4_command.ex2; + +import lab4_command.ex2.actions.*; + +import java.util.ArrayList; + +public class TextEditor { + private ArrayList menu = new ArrayList<>(); + private TextFile file; + + public TextEditor(TextFile file) { + this.file = file; + + addMenuAction(new OpenAction(this.file)); + addMenuAction(new CloseAction(this.file)); + addMenuAction(new SaveAction(this.file)); + addMenuAction(new CopyAction(this.file)); + addMenuAction(new PasteAction(this.file)); + } + + public void clickMenu(int entry) { + MenuAction action = null; + try { + action = menu.get(entry); + } catch (IndexOutOfBoundsException e) { + System.out.println("No action bound to entry " + entry); + } + + if (action != null) { + action.execute(); + } + } + + public void addMenuAction(MenuAction action) { + menu.add(action); + } + + public void removeMenuAction(int i) { + menu.remove(i); + } + + public void setMenuAction(int i, MenuAction action) { + menu.set(i, action); + } + + public static void main(String[] args) { + TextFile file = new TextFile("test.txt"); + TextEditor editor = new TextEditor(file); + + editor.clickMenu(0); + editor.clickMenu(1); + editor.clickMenu(2); + editor.clickMenu(3); + editor.clickMenu(4); + } +} diff --git a/src/lab4_command/ex2/TextFile.java b/src/lab4_command/ex2/TextFile.java new file mode 100644 index 0000000..dc4504d --- /dev/null +++ b/src/lab4_command/ex2/TextFile.java @@ -0,0 +1,29 @@ +package lab4_command.ex2; + +public class TextFile { + private String name; + + public TextFile(String name) { + this.name = name; + } + + public void open() { + System.out.println("Opening file " + name); + } + + public void close() { + System.out.println("Closing file " + name); + } + + public void save() { + System.out.println("Saving file " + name); + } + + public void copy() { + System.out.println("Copying file " + name); + } + + public void paste() { + System.out.println("Pasting file " + name); + } +} diff --git a/src/lab4_command/ex2/actions/CloseAction.java b/src/lab4_command/ex2/actions/CloseAction.java new file mode 100644 index 0000000..8089202 --- /dev/null +++ b/src/lab4_command/ex2/actions/CloseAction.java @@ -0,0 +1,15 @@ +package lab4_command.ex2.actions; + +import lab4_command.ex2.FileAction; +import lab4_command.ex2.TextFile; + +public class CloseAction extends FileAction { + public CloseAction(TextFile file) { + super(file); + } + + @Override + public void execute() { + file.close(); + } +} diff --git a/src/lab4_command/ex2/actions/CopyAction.java b/src/lab4_command/ex2/actions/CopyAction.java new file mode 100644 index 0000000..89c9a39 --- /dev/null +++ b/src/lab4_command/ex2/actions/CopyAction.java @@ -0,0 +1,15 @@ +package lab4_command.ex2.actions; + +import lab4_command.ex2.FileAction; +import lab4_command.ex2.TextFile; + +public class CopyAction extends FileAction { + public CopyAction(TextFile file) { + super(file); + } + + @Override + public void execute() { + file.copy(); + } +} diff --git a/src/lab4_command/ex2/actions/OpenAction.java b/src/lab4_command/ex2/actions/OpenAction.java new file mode 100644 index 0000000..4203d0d --- /dev/null +++ b/src/lab4_command/ex2/actions/OpenAction.java @@ -0,0 +1,15 @@ +package lab4_command.ex2.actions; + +import lab4_command.ex2.FileAction; +import lab4_command.ex2.TextFile; + +public class OpenAction extends FileAction { + public OpenAction(TextFile file) { + super(file); + } + + @Override + public void execute() { + file.open(); + } +} diff --git a/src/lab4_command/ex2/actions/PasteAction.java b/src/lab4_command/ex2/actions/PasteAction.java new file mode 100644 index 0000000..52f2cc4 --- /dev/null +++ b/src/lab4_command/ex2/actions/PasteAction.java @@ -0,0 +1,15 @@ +package lab4_command.ex2.actions; + +import lab4_command.ex2.FileAction; +import lab4_command.ex2.TextFile; + +public class PasteAction extends FileAction { + public PasteAction(TextFile file) { + super(file); + } + + @Override + public void execute() { + file.paste(); + } +} diff --git a/src/lab4_command/ex2/actions/SaveAction.java b/src/lab4_command/ex2/actions/SaveAction.java new file mode 100644 index 0000000..baa7fc4 --- /dev/null +++ b/src/lab4_command/ex2/actions/SaveAction.java @@ -0,0 +1,15 @@ +package lab4_command.ex2.actions; + +import lab4_command.ex2.FileAction; +import lab4_command.ex2.TextFile; + +public class SaveAction extends FileAction { + public SaveAction(TextFile file) { + super(file); + } + + @Override + public void execute() { + file.save(); + } +}