added lab4 ex2

This commit is contained in:
Louis Heredero 2024-10-13 18:37:53 +02:00
parent 2598af8d3a
commit ddc6253386
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
9 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package lab4_command.ex2;
public abstract class FileAction implements MenuAction {
protected TextFile file;
public FileAction(TextFile file) {
this.file = file;
}
}

View File

@ -0,0 +1,5 @@
package lab4_command.ex2;
public interface MenuAction {
void execute();
}

View File

@ -0,0 +1,56 @@
package lab4_command.ex2;
import lab4_command.ex2.actions.*;
import java.util.ArrayList;
public class TextEditor {
private ArrayList<MenuAction> 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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}