diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index e6a79a8..85da512 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -51,6 +51,7 @@ 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()); commands.put("time", new TimeCommand()); commands.put("date", new DateCommand()); } 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