Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions assignments/command_pattern/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package assignments.command_pattern;

public interface Command {
void execute();

void undo();

void redo();
}
28 changes: 28 additions & 0 deletions assignments/command_pattern/CopyCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package assignments.command_pattern;

public class CopyCommand implements Command {
private Text text;
private int start;
private int end;

public CopyCommand(Text text, int start, int end) {
this.text = text;
this.start = start;
this.end = end;
}

@Override
public void execute() {
text.copy(start, end);
}

@Override
public void undo() {
// copying normally does not require an undo function
}

@Override
public void redo() {
// copying normally does not require an redo function
}
}
29 changes: 29 additions & 0 deletions assignments/command_pattern/CutCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package assignments.command_pattern;

public class CutCommand implements Command {
private Text text;
private String removedText;
private int start;
private int end;

public CutCommand(Text text, int start, int end) {
this.text = text;
this.start = start;
this.end = end;
}

@Override
public void execute() {
removedText = text.cut(start, end);
}

@Override
public void undo() {
text.insert(start, removedText);
}

@Override
public void redo() {
execute();
}
}
33 changes: 33 additions & 0 deletions assignments/command_pattern/Keyboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package assignments.command_pattern;

import java.util.Map;
import java.util.HashMap;

public class Keyboard {
private Map<String, Command> keyBindings = new HashMap<>();
private Command lastCommand = new NoCommand();
private Command redoCommand = new NoCommand();

public void setCommand(String key, Command command) {
keyBindings.put(key, command);
}

public void pressKey(String key) {
Command command = keyBindings.getOrDefault(key, new NoCommand());
command.execute();
lastCommand = command;
redoCommand = new NoCommand();
}

public void undoKey() {
lastCommand.undo();
redoCommand = lastCommand;
lastCommand = new NoCommand();
}

public void redoKey() {
redoCommand.redo();
lastCommand = redoCommand;
redoCommand = new NoCommand();
}
}
18 changes: 18 additions & 0 deletions assignments/command_pattern/NoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package assignments.command_pattern;

public class NoCommand implements Command {
public NoCommand() {
}

@Override
public void execute() {
}

@Override
public void undo() {
}

@Override
public void redo() {
}
}
28 changes: 28 additions & 0 deletions assignments/command_pattern/PasteCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package assignments.command_pattern;

public class PasteCommand implements Command {
private Text text;
private String clipBoard;
private int start;

public PasteCommand(Text text, int start) {
this.text = text;
this.start = start;
}

@Override
public void execute() {
clipBoard = text.getClipBoard();
text.insert(start, clipBoard);
}

@Override
public void undo() {
text.delete(start, start + clipBoard.length());
}

@Override
public void redo() {
text.insert(start, clipBoard);
}
}
41 changes: 41 additions & 0 deletions assignments/command_pattern/Text.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package assignments.command_pattern;

public class Text {
private StringBuilder textBlock = new StringBuilder();
private String clipBoard = "";

public Text(String initialTextBlock) {
this.textBlock.append(initialTextBlock);
}

public String getClipBoard() {
return clipBoard;
}

public void setClipBoard(String text) {
this.clipBoard = text;
}

public String getTextBlock() {
return textBlock.toString();
}

public String copy(int start, int end) {
clipBoard = textBlock.substring(start, end);
return clipBoard;
}

public String cut(int start, int end) {
clipBoard = textBlock.substring(start, end);
textBlock.delete(start, end);
return clipBoard;
}

public void insert(int start, String word) {
textBlock.insert(start, word);
}

public void delete(int start, int end) {
textBlock.delete(start, end);
}
}
117 changes: 117 additions & 0 deletions assignments/command_pattern/TextEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package assignments.command_pattern;

import java.util.Scanner;

public class TextEditor {
public static void main(String[] args) {

// Setting up the StringBuffer element
StringBuffer currentBuffer = new StringBuffer("Hello! Welcome to my text editor program! \n");
currentBuffer.append("We are going to learn about copying, cutting, and pasting. \n");
currentBuffer.append("Are you ready to get started? \n");
currentBuffer.append("We will only be interacting with the first instance of any text! \n");
currentBuffer.append("Let's practice on this greeting!");

// Create the Text, Keyboard, clipboard, and start index elements
String currentText = currentBuffer.toString();
Text text = new Text(currentText);
Keyboard keyboard = new Keyboard();
int start = 0;
int end = 0;
int paste = 0;

// Get ready to handle user input
Scanner inputScanner = new Scanner(System.in);
String userInput;

// Display Greeting
System.out.println(currentText);

// Copy setup
System.out.println("\nPick a word in the greeting to copy!");
userInput = inputScanner.nextLine();

if (currentText.contains(userInput)) {
start = text.getTextBlock().indexOf(userInput);
end = start + userInput.length();
keyboard.setCommand("copy", new CopyCommand(text, start, end));
keyboard.pressKey("copy");
} else {
System.out.println("Could not find: " + userInput);
}

// Cut setup
System.out.println("\nPick a word in the greeting to cut!");
userInput = inputScanner.nextLine();

if (currentText.contains(userInput)) {
start = text.getTextBlock().indexOf(userInput);
end = start + userInput.length();
keyboard.setCommand("cut", new CutCommand(text, start, end));
keyboard.pressKey("cut");
} else {
System.out.println("Could not find: " + userInput);
}

// Paste setup
System.out.println("\nShould we paste our clipboard?");
userInput = inputScanner.nextLine();

if (userInput.equalsIgnoreCase("yes") || userInput.equalsIgnoreCase("y")) {
System.out.println("\nWhat index would you like to paste to?");
paste = inputScanner.nextInt();
inputScanner.nextLine();
keyboard.setCommand("paste", new PasteCommand(text, paste));
keyboard.pressKey("paste");
}

// Next action setup

while (!userInput.equalsIgnoreCase("quit")) {
System.out.println("\nWhat would you like to do now 'copy'/'cut'/'paste'/'redo'/'undo'/'quit'?");
userInput = inputScanner.nextLine();

switch (userInput.toLowerCase()) {
case "copy":
System.out.println("\nPick a word in the greeting to copy!");
userInput = inputScanner.nextLine();
start = text.getTextBlock().indexOf(userInput);
end = start + userInput.length();
keyboard.setCommand("copy", new CopyCommand(text, start, end));
keyboard.pressKey("copy");
break;
case "cut":
System.out.println("\nPick a word in the greeting to cut!");
userInput = inputScanner.nextLine();
start = text.getTextBlock().indexOf(userInput);
end = start + userInput.length();
keyboard.setCommand("cut", new CutCommand(text, start, end));
keyboard.pressKey("cut");
break;
case "paste":
System.out.println("\nWhat index would you like to paste to?");
paste = inputScanner.nextInt();
inputScanner.nextLine();
keyboard.setCommand("paste", new PasteCommand(text, paste));
keyboard.pressKey("paste");
break;
case "undo":
keyboard.undoKey();
break;
case "redo":
keyboard.redoKey();
break;
case "quit":
return;
default:
System.out.println("Unknown command");
}

// Action proof
System.out.println("\nOur clip board");
System.out.println("\"" + text.getClipBoard() + "\"");
System.out.println("\nOur current text:");
System.out.println(text.getTextBlock());
}
}
}