|
| 1 | +package com.company; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class TaskHelper |
| 8 | +{ |
| 9 | + Queue<String> tasks = new LinkedList<>(); |
| 10 | + |
| 11 | + public static void main(String[] args) |
| 12 | + { |
| 13 | + TaskHelper taskHelper = new TaskHelper(); |
| 14 | + |
| 15 | + taskHelper.run(); |
| 16 | + } |
| 17 | + public void run() |
| 18 | + { |
| 19 | + String command = ""; |
| 20 | + |
| 21 | + System.out.println("Welcome to the task manager. Here are a list of commands. At any time, enter PrintCommmands to view the commands."); |
| 22 | + printCommands(); |
| 23 | + try |
| 24 | + { |
| 25 | + do |
| 26 | + { |
| 27 | + Scanner scanner = new Scanner(System.in); |
| 28 | + |
| 29 | + String commandLine = scanner.nextLine(); |
| 30 | + |
| 31 | + String[] commands = commandLine.split(" "); |
| 32 | + command = commands[0].toUpperCase(); |
| 33 | + |
| 34 | + if (command.equals("ADD")) |
| 35 | + { |
| 36 | + if(commands.length >= 2) |
| 37 | + { |
| 38 | + addTask(commands); |
| 39 | + } |
| 40 | + |
| 41 | + else |
| 42 | + { |
| 43 | + System.out.println("I have to know what task to add. Enter some tasks this time."); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + else if (command.equals("PEEK")) |
| 48 | + { |
| 49 | + int numTasks = 0; |
| 50 | + |
| 51 | + if (commands.length == 2) |
| 52 | + { |
| 53 | + numTasks = Integer.valueOf(commands[1]); |
| 54 | + peek(numTasks, tasks); |
| 55 | + } |
| 56 | + else if(commands.length == 1) |
| 57 | + { |
| 58 | + peek(numTasks, tasks); |
| 59 | + } |
| 60 | + |
| 61 | + else |
| 62 | + { |
| 63 | + System.out.println("Invalid input. Enter Peek, then one number."); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + else if (command.equals("REMOVE")) |
| 69 | + { |
| 70 | + int numTasks = 0; |
| 71 | + |
| 72 | + if (commands.length == 2) |
| 73 | + { |
| 74 | + numTasks = Integer.valueOf(commands[1]); |
| 75 | + remove(numTasks); |
| 76 | + } |
| 77 | + else if(commands.length == 1) |
| 78 | + { |
| 79 | + remove(numTasks); |
| 80 | + } |
| 81 | + |
| 82 | + else |
| 83 | + { |
| 84 | + System.out.println("Invalid input. Enter Remove, then one number."); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + else if (command.equals("HOWMANY")) |
| 89 | + { |
| 90 | + howMany(); |
| 91 | + } |
| 92 | + |
| 93 | + else if(command.equals("PRINTCOMMANDS")) |
| 94 | + { |
| 95 | + printCommands(); |
| 96 | + } |
| 97 | + |
| 98 | + else if (command.equals("FLEE")) |
| 99 | + { |
| 100 | + flee(); |
| 101 | + } |
| 102 | + |
| 103 | + else if (command.equals("EXIT")) |
| 104 | + { |
| 105 | + exit(); |
| 106 | + } |
| 107 | + |
| 108 | + else |
| 109 | + { |
| 110 | + System.out.println("Invalid command."); |
| 111 | + } |
| 112 | + |
| 113 | + if(!command.equals("EXIT")) |
| 114 | + System.out.print("Enter another command:"); |
| 115 | + |
| 116 | + } while (!command.equals("EXIT")); |
| 117 | + } |
| 118 | + catch(Exception e) |
| 119 | + { |
| 120 | + System.out.println("Invalid input. Try another command."); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void printCommands() |
| 125 | + { |
| 126 | + System.out.println("Add <TaskName> <TaskName> ..."); |
| 127 | + System.out.println("Peek"); |
| 128 | + System.out.println("Remove"); |
| 129 | + System.out.println("HowMany"); |
| 130 | + System.out.println("Flee"); |
| 131 | + } |
| 132 | + |
| 133 | + public void addTask(String[] taskNames) |
| 134 | + { |
| 135 | + for(int i = 1;i < taskNames.length;i++) |
| 136 | + { |
| 137 | + tasks.add(taskNames[i]); |
| 138 | + System.out.println("Added " + taskNames[i] + " to the list of tasks."); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public void peek(int numTasks, Queue<String> holder) |
| 143 | + { |
| 144 | + if(tasks.size() == 1 && numTasks == 0) |
| 145 | + System.out.println("The next task is " + tasks.peek()); |
| 146 | + |
| 147 | + else if(tasks.size() == 0) |
| 148 | + { |
| 149 | + System.out.println("There are no tasks in the list."); |
| 150 | + } |
| 151 | + |
| 152 | + else if(tasks.size() > 1 && numTasks <= tasks.size()) |
| 153 | + { |
| 154 | + holder = new LinkedList<>(); |
| 155 | + |
| 156 | + for(String task : tasks) |
| 157 | + { |
| 158 | + holder.add(task); |
| 159 | + } |
| 160 | + System.out.println("The next " + numTasks + " tasks are: "); |
| 161 | + |
| 162 | + for(int i = 0;i < numTasks;i++) |
| 163 | + { |
| 164 | + System.out.println(holder.poll()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + else if(numTasks > tasks.size()) |
| 169 | + { |
| 170 | + System.out.println("There aren't that many tasks in the list."); |
| 171 | + } |
| 172 | + else |
| 173 | + System.out.println("There are no tasks left in the list."); |
| 174 | + } |
| 175 | + |
| 176 | + public void remove(int numTasks) |
| 177 | + { |
| 178 | + if(tasks.size() == 1) |
| 179 | + { |
| 180 | + System.out.println("Removed " + tasks.peek() + " from the list."); |
| 181 | + tasks.remove(); |
| 182 | + } |
| 183 | + else if(tasks.size() > 1 && numTasks <= tasks.size()) |
| 184 | + { |
| 185 | + System.out.println("Removed the following tasks: "); |
| 186 | + |
| 187 | + for(int i = 0;i < numTasks;i++) |
| 188 | + { |
| 189 | + System.out.println(tasks.remove()); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + else if(numTasks > tasks.size()) |
| 194 | + { |
| 195 | + System.out.println("There aren't that many tasks in the list."); |
| 196 | + } |
| 197 | + else |
| 198 | + System.out.println("There are no tasks left in the list."); |
| 199 | + } |
| 200 | + |
| 201 | + public void howMany() |
| 202 | + { |
| 203 | + System.out.println("Tasks left to be completed: " + tasks.size()); |
| 204 | + } |
| 205 | + |
| 206 | + public void flee() |
| 207 | + { |
| 208 | + tasks.clear(); |
| 209 | + System.out.println("All tasks have been cleared from the list."); |
| 210 | + } |
| 211 | + |
| 212 | + public void exit() |
| 213 | + { |
| 214 | + System.out.println("Thank you for using this task manager. Goodbye."); |
| 215 | + } |
| 216 | +} |
0 commit comments