From 041fc4c29f4d6f508ddb6391e574a691cc9a48f1 Mon Sep 17 00:00:00 2001 From: Kaveesha Fernando Date: Sun, 12 Oct 2025 14:34:58 +0530 Subject: [PATCH] feat: implemented and tested touch command --- src/main/java/com/mycmd/App.java | 1 + .../java/com/mycmd/commands/TouchCommand.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/com/mycmd/commands/TouchCommand.java diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 1458f14..2a16262 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -51,5 +51,6 @@ private static void registerCommands(Map commands) { commands.put("help", new HelpCommand(commands)); commands.put("exit", new ExitCommand()); commands.put("ver", new VersionCommand()); + commands.put("touch", new TouchCommand()); } } diff --git a/src/main/java/com/mycmd/commands/TouchCommand.java b/src/main/java/com/mycmd/commands/TouchCommand.java new file mode 100644 index 0000000..6c83230 --- /dev/null +++ b/src/main/java/com/mycmd/commands/TouchCommand.java @@ -0,0 +1,25 @@ +package com.mycmd.commands; + +import com.mycmd.Command; +import com.mycmd.ShellContext; +import java.io.File; +import java.io.IOException; + +public class TouchCommand implements Command { + @Override + public void execute(String[] args, ShellContext context) throws IOException { + if (args.length < 1) { // ✅ Check for at least 1 argument + System.out.println("Usage: touch "); + return; + } + + File file = new File(context.getCurrentDir(), args[0]); // ✅ Use args[0] + if (file.createNewFile()) { + System.out.println("File created: " + args[0]); // ✅ Use args[0] + } else { + // Update timestamp + file.setLastModified(System.currentTimeMillis()); + System.out.println("File timestamp updated: " + args[0]); // ✅ Use args[0] + } + } +} \ No newline at end of file