This is a good time to put together some of the topics you have learned so far to create something useful. In this exercise you will write a small TaskManager
program that can store a list of tasks.
- It is a
CLI program. - In this version, the task list is not saved to the disk i.e., the task list does not persist between multiple runs of the program
Here is an example output:
Welcome to TaskManager-Level1!
Your task? add submit report
Tasks in the list: 1
Your task? add return library book
Tasks in the list: 2
Your task? add remind boss about meeting
Tasks in the list: 3
Your task? xyz
Unknown command! please try again
Your task? print
[1] submit report
[2] return library book
[3] remind boss about meeting
Your task? add set up meeting with boss
Tasks in the list: 4
Your task? print
[1] submit report
[2] return library book
[3] remind boss about meeting
[4] set up meeting with boss
Your task? exit
Bye!
Summary of behavior:
- Task Manager prompts for user input with the message
Your task?
add
task description
: adds thetask description
to the task listprint
: prints the tasks added so farexit
or empty input: terminates the program
If you are new to programming, we recommend you to build the program in small steps, as follows:
- Write a program that reads any user input, prints it back to the user, and exits.
- Update the program to do the above in a loop until user types
exit
. - Add greetings printed at the start and the end.
- If the first word is not
add
orexit
, print an error message. Suggestion: use aswitch
statement to choose between multiple actions. - Create a
Task
class to represent tasks and add anTask[]
to store tasks. - If the first word of the user input is
add
, add the whole line (no need to omit the first word) to the task list. - Update the code in the previous step to omit the word
add
from the task description. - Add support for the
print
command. - Add support for terminating if the user input is empty
- and so on ...
import java.util.Scanner;
public class Main {
static Scanner in = new Scanner(System.in);
static Task[] tasks = new Task[100];
static int count = 0; // to keep track of number of tasks in the list
public static void main(String[] args) {
printWelcome();
String line;
boolean isExit = false;
while (!isExit) {
line = getInput();
String command = line.split(" ")[0]; //extract the first word of the user input
switch (command) {
case "exit":
case "": // exit if user input is empty
isExit = true;
break;
case "add":
// todo: add code here
default:
printError();
}
}
exit();
}
private static void printWelcome() {
System.out.println("Welcome to TaskManager-Level1!");
}
private static void printTasks() {
for (int i = 0; i < count; i++) {
System.out.println("[" + (i + 1) + "] " + tasks[i].getDescription());
}
}
// todo: add missing methods
}