TaskMan §
Main.java §
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// caleb b
// task manager
Scanner scan = new Scanner(System.in);
List<Task> tasks = new ArrayList<>();
boolean running = true;
while(running) {
clearScreen(); // clear the screen and print out the options
System.out.println("Welcome to the Task Manager.");
System.out.println("What would you like to do?");
System.out.println("1. Add a task");
System.out.println("2. Mark task as Complete");
System.out.println("3. View Task List");
System.out.println("4. Exit");
int choice = scan.nextInt();
scan.nextLine(); // clear the buffer
switch(choice) {
case 1:
// Add task
// Get name, description, and due date
System.out.println("Enter the name of the task:");
String name = scan.nextLine();
System.out.println("Enter the description of the task:");
String description = scan.nextLine();
System.out.println("Enter the due date of the task:");
String dueDate = scan.nextLine();
// Create task object and add to arraylist
Task task = new Task(name, description, dueDate);
tasks.add(task);
System.out.println("Task added!");
break;
case 2:
// Mark task as complete
// Print out list of tasks
for(int i = 0; i < tasks.size(); i++) {
String completed = tasks.get(i).isCompleted() ? "Completed" : "Incomplete";
System.out.println(i + 1 + ". " + tasks.get(i).getName() + " - " + tasks.get(i).getDueDate() + " - " + completed);
}
// Get index of task to mark complete
System.out.println("Enter the index of the task to mark complete:");
int index = scan.nextInt();
// Set task.completed to true
tasks.get(index - 1).setCompleted(true);
System.out.println("Task marked complete!");
break;
case 3:
// View task list
System.out.println("Task List:");
System.out.println("HINT: Type the index of the task to view a task's description");
// Print out list of tasks
for(int i = 0; i < tasks.size(); i++) {
String completed = tasks.get(i).isCompleted() ? "Completed" : "Incomplete";
System.out.println(i + 1 + ". " + tasks.get(i).getName() + " - due by " + tasks.get(i).getDueDate() + " - " + completed);
}
// Get index of task to view
System.out.println("Enter the index of the task to view:");
int taskIndex = scan.nextInt();
if (taskIndex < tasks.size() + 1) {
// Print out task name and description
clearScreen();
System.out.println(tasks.get(taskIndex - 1).getName() + ":");
System.out.println(tasks.get(taskIndex - 1).getDescription());
// Type anything to continue:
pause();
} else {
// Invalid index, await next input and restart loop
System.out.println("Invalid index.");
pause();
}
break;
case 4:
// Close the program
System.out.println("Goodbye!");
running = false;
break;
default:
// Invalid choice, return to beginning of loop
System.out.println("Invalid choice.");
pause();
break;
}
}
}
public static void clearScreen() {
// Function for clearing the terminal
for (int i = 0; i < 100; i++) {
System.out.println();
}
}
public static void pause() {
// Pause the program until the user presses enter
Scanner scan = new Scanner(System.in);
System.out.println("Press enter to continue");
scan.nextLine();
}
}
Task.java §
public class Task {
// Task variables
private boolean completed;
private String description;
private String name;
private String dueDate;
public Task(String name, String description, String dueDate) {
// Constructor, take in the name, description, and due date
this.name = name;
this.description = description;
this.dueDate = dueDate;
this.completed = false; // Task should be incomplete by default
}
// Getters and Setters for everything
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDueDate() {
return dueDate;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
}