Almost full implementation of Scanner UI. Two tests not passing, but safety commit
This commit is contained in:
@@ -1,24 +1,234 @@
|
||||
package hhn.temp.project;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class UserCommands implements Runnable {
|
||||
private Scanner scanner;
|
||||
private AssignmentManager manager;
|
||||
private boolean inTaskSelection = false;
|
||||
private boolean inWorkerSelection = false;
|
||||
private Integer selectedTaskId = null;
|
||||
private Integer selectedWorkerId = null;
|
||||
private AtomicBoolean running = new AtomicBoolean(false);
|
||||
private Thread cliThread;
|
||||
|
||||
public UserCommands(AssignmentManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
scanner = new Scanner(System.in);
|
||||
running.set(true);
|
||||
|
||||
while (running.get()) {
|
||||
printPrompt();
|
||||
String input = scanner.nextLine().trim();
|
||||
|
||||
if ("exit".equalsIgnoreCase(input)) {
|
||||
stop();
|
||||
continue;
|
||||
}
|
||||
|
||||
handleInput(input);
|
||||
}
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
public class UserCommands {
|
||||
Scanner scanner;
|
||||
public void start() {
|
||||
boolean close = false;
|
||||
// scanner = new Scanner(System.in);
|
||||
// while (!close) {
|
||||
// System.out.println("Type '?' or 'help' for a list of commands");
|
||||
// System.out.println("Please enter a command:");
|
||||
// String input = scanner.nextLine();
|
||||
// handleInput(input);
|
||||
// }
|
||||
// scanner.close();
|
||||
if (cliThread == null || !cliThread.isAlive()) {
|
||||
cliThread = new Thread(this, "CLI-Thread");
|
||||
cliThread.start();
|
||||
}
|
||||
}
|
||||
public void handleInput(String input) {
|
||||
|
||||
public void stop() {
|
||||
running.set(false);
|
||||
if (cliThread != null) {
|
||||
cliThread.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private void printPrompt() {
|
||||
if (!running.get()) return;
|
||||
|
||||
if (inTaskSelection && selectedTaskId != null) {
|
||||
System.out.println("Task " + selectedTaskId + " selected. Commands: finish, unfinish, remove, edit, back");
|
||||
} else if (inWorkerSelection && selectedWorkerId != null) {
|
||||
System.out.println("Worker " + selectedWorkerId + " selected. Commands: remove, back");
|
||||
} else {
|
||||
System.out.println("Type '?' or 'help' for a list of commands");
|
||||
}
|
||||
System.out.print("> ");
|
||||
}
|
||||
|
||||
public void handleInput(String input) {
|
||||
if (input == null || input.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ("?".equals(input) || "help".equalsIgnoreCase(input)) {
|
||||
showHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (inTaskSelection) {
|
||||
handleTaskSelectionInput(input);
|
||||
} else if (inWorkerSelection) {
|
||||
handleWorkerSelectionInput(input);
|
||||
} else {
|
||||
handleMainMenuInput(input);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void showHelp() {
|
||||
if (inTaskSelection && selectedTaskId != null) {
|
||||
System.out.println("Task commands:");
|
||||
System.out.println(" finish - Mark task as finished");
|
||||
System.out.println(" unfinish - Mark task as unfinished");
|
||||
System.out.println(" remove - Remove this task");
|
||||
System.out.println(" edit - Edit task (prompts for name;description)");
|
||||
System.out.println(" back - Return to main menu");
|
||||
} else if (inWorkerSelection && selectedWorkerId != null) {
|
||||
System.out.println("Worker commands:");
|
||||
System.out.println(" remove - Remove this worker");
|
||||
System.out.println(" back - Return to main menu");
|
||||
} else {
|
||||
System.out.println("Available commands:");
|
||||
System.out.println(" createWorker - Create a new worker");
|
||||
System.out.println(" createTask - Create a new task");
|
||||
System.out.println(" selectTask - Select a task to work with");
|
||||
System.out.println(" listWorkers - List all workers");
|
||||
System.out.println(" help/? - Show this help");
|
||||
System.out.println(" exit - Exit the program");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMainMenuInput(String input) {
|
||||
switch (input.toLowerCase()) {
|
||||
case "createworker":
|
||||
System.out.print("Enter worker name: ");
|
||||
String workerName = scanner.nextLine().trim();
|
||||
int workerId = manager.createWorker(workerName);
|
||||
System.out.println("Created worker with ID: " + workerId);
|
||||
break;
|
||||
|
||||
case "createtask":
|
||||
System.out.print("Enter worker ID: ");
|
||||
int workerIdForTask = Integer.parseInt(scanner.nextLine().trim());
|
||||
System.out.print("Enter task name: ");
|
||||
String taskName = scanner.nextLine().trim();
|
||||
System.out.print("Enter task description: ");
|
||||
String taskDesc = scanner.nextLine().trim();
|
||||
int taskId = manager.addTask(workerIdForTask, taskName, taskDesc);
|
||||
System.out.println("Created task with ID: " + taskId);
|
||||
break;
|
||||
|
||||
case "selecttask":
|
||||
inTaskSelection = true;
|
||||
System.out.print("Enter task ID: ");
|
||||
String taskIdInput = scanner.nextLine().trim();
|
||||
selectedTaskId = Integer.parseInt(taskIdInput);
|
||||
break;
|
||||
|
||||
case "listworkers":
|
||||
inWorkerSelection = true;
|
||||
System.out.print("Enter worker ID: ");
|
||||
String workerIdInput = scanner.nextLine().trim();
|
||||
selectedWorkerId = Integer.parseInt(workerIdInput);
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Unknown command. Type 'help' for available commands.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleTaskSelectionInput(String input) {
|
||||
switch (input.toLowerCase()) {
|
||||
case "finish":
|
||||
manager.finishTask(manager.getTask(selectedTaskId).getWorkerId(), selectedTaskId);
|
||||
System.out.println("Task marked as finished");
|
||||
break;
|
||||
|
||||
case "unfinish":
|
||||
manager.unfinishTask(manager.getTask(selectedTaskId).getWorkerId(), selectedTaskId);
|
||||
System.out.println("Task marked as unfinished");
|
||||
break;
|
||||
|
||||
case "remove":
|
||||
manager.removeTask(selectedTaskId);
|
||||
System.out.println("Task removed");
|
||||
resetSelection();
|
||||
break;
|
||||
|
||||
case "edit":
|
||||
System.out.print("Enter new name;description: ");
|
||||
String editInput = scanner.nextLine().trim();
|
||||
String[] parts = editInput.split(";", 2);
|
||||
if (parts.length == 2) {
|
||||
manager.editTask(manager.getTask(selectedTaskId).getWorkerId(),selectedTaskId,parts[0],parts[1]);
|
||||
System.out.println("Task edited");
|
||||
} else {
|
||||
System.out.println("Invalid format. Use: name;description");
|
||||
}
|
||||
break;
|
||||
|
||||
case "back":
|
||||
resetSelection();
|
||||
break;
|
||||
|
||||
default:
|
||||
try {
|
||||
selectedTaskId = Integer.parseInt(input);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Unknown command. Type 'help' for available commands.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleWorkerSelectionInput(String input) {
|
||||
switch (input.toLowerCase()) {
|
||||
case "remove":
|
||||
manager.removeWorker(selectedWorkerId);
|
||||
System.out.println("Worker removed");
|
||||
resetSelection();
|
||||
break;
|
||||
|
||||
case "back":
|
||||
resetSelection();
|
||||
break;
|
||||
|
||||
default:
|
||||
try {
|
||||
selectedWorkerId = Integer.parseInt(input);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Unknown command. Type 'help' for available commands.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void resetSelection() {
|
||||
inTaskSelection = false;
|
||||
inWorkerSelection = false;
|
||||
selectedTaskId = null;
|
||||
selectedWorkerId = null;
|
||||
}
|
||||
|
||||
public Scanner getScanner() {
|
||||
return scanner;
|
||||
}
|
||||
|
||||
// For testing - allows setting scanner
|
||||
public void setScanner(Scanner scanner) {
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return running.get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user