Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Caleb Chan] iP #191

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c5b2f77
Rename chatbot to Nano and add greeting and exit message
calebcjl Jan 19, 2023
8cb1426
Add echo user input feature and exit condition
calebcjl Jan 25, 2023
5690fae
Change echo feature to store user input in a list
calebcjl Jan 26, 2023
daafab5
Add task class and feature to mark tasks
calebcjl Jan 31, 2023
d9c167a
Tweak to coding standard and display number of completed tasks in list
calebcjl Jan 31, 2023
b3d9e39
Refactor code to improve quality
calebcjl Feb 1, 2023
57b1f39
Improve code based on peer reviews
calebcjl Feb 4, 2023
a8bd85d
Add new classes to represent different types of Task
calebcjl Feb 4, 2023
4c0bc00
Add exceptions to detect incorrect input
calebcjl Feb 8, 2023
0dd2ccb
Organise classes into packages
calebcjl Feb 8, 2023
0072420
Add /help command to list out all possible commands
calebcjl Feb 8, 2023
5ff0543
Merge branch 'branch-A-Package'
calebcjl Feb 8, 2023
3e3cce1
Merge branch 'branch-Level-5'
calebcjl Feb 8, 2023
2db7774
Create new package for Nano exceptions
calebcjl Feb 8, 2023
d7f2d9e
Use ArrayList to store tasks instead of fixed array
calebcjl Feb 16, 2023
8d667e9
Add delete feature
calebcjl Feb 16, 2023
8cf3a5d
Add feature to save and retrieve user task list data
calebcjl Mar 3, 2023
d4c1c33
Merge branch 'branch-Level-6'
calebcjl Mar 3, 2023
da6e71f
Merge branch 'branch-Level-7'
calebcjl Mar 3, 2023
b3afe13
Refactor code to include new classes and packages for more OOP
calebcjl Mar 3, 2023
f44d88a
Add feature to find and list tasks containing a keyword
calebcjl Mar 3, 2023
2a52cb3
Merge pull request #1 from calebcjl/branch-Level-9
calebcjl Mar 3, 2023
4939e2c
Merge branch 'master' of https://github.com/calebcjl/ip
calebcjl Mar 3, 2023
d7094b0
Add JavaDoc comments to code
calebcjl Mar 3, 2023
f9280aa
Add user guide for Nano
calebcjl Mar 3, 2023
0d5f844
Update the correct README for user guide
calebcjl Mar 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

178 changes: 178 additions & 0 deletions src/main/java/Nano.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import java.util.Scanner;
calebcjl marked this conversation as resolved.
Show resolved Hide resolved

public class Nano {

private final static int HORIZONTAL_LINE_LENGTH = 100;
private final static int MAX_TASK_COUNT = 100;
private final static String NANO_LOGO = "| \\ || / \\ | \\ ||| __ |\n"
+ "||\\\\ || / _ \\ ||\\\\ ||| | | |\n"
+ "|| \\\\|| // \\\\ || \\\\||| |__| |\n"
+ "|| \\_|// \\\\|| \\_||______|\n";
private final static int COMMAND_INDEX = 0;
private final static int TASK_NAME_INDEX = 1;
public static final String TASK_COMPLETED_MARK = "[x] ";
public static final String TASK_UNCOMPLETED_MARK = "[] ";
public static final String MESSAGE_TASK_NOT_FOUND = "Task not found.";
public static final String MESSAGE_TASK_MARKED = "Great job completing ";
public static final String MESSAGE_TASK_ALREADY_DONE = " is already done";
public static final String MESSAGE_TASK_UNMARKED = " is marked as undone";
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
private static Task[] tasks;
public static void main(String[] args) {
//chatbot startup
displayWelcomeMessage();
initialiseTaskList();

//receive commands
while (true) {
String userInput = getUserInput();
executeCommand(userInput);
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static void executeCommand(String userInput) {
String[] userInputs = processInput(userInput);

switch (userInputs[COMMAND_INDEX]) {
case "list":
displayTaskList();
break;
case "add":
addTask(userInputs[TASK_NAME_INDEX]);
break;
case "mark":
markTask(userInputs[TASK_NAME_INDEX]);
break;
case "unmark":
unmarkTask(userInputs[TASK_NAME_INDEX]);
break;
case "exit":
displayExitMessage();
System.exit(0);
default:
displayHelpMessage();
break;
}
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
}

private static void unmarkTask(String taskName) {
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
int taskIndex = getTaskIndex(taskName);
if (taskIndex == -1) {
System.out.println(MESSAGE_TASK_NOT_FOUND);
} else {
if (tasks[taskIndex].getTaskCompletionStatus()) {
tasks[taskIndex].incompleteTask();
System.out.println(taskName + MESSAGE_TASK_UNMARKED);
} else {
System.out.println(taskName + MESSAGE_TASK_ALREADY_DONE);
}
}
}
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
private static void markTask(String taskName) {
int taskIndex = getTaskIndex(taskName);
if (taskIndex == -1) {
System.out.println(MESSAGE_TASK_NOT_FOUND);
} else {
if (!tasks[taskIndex].getTaskCompletionStatus()) {
tasks[taskIndex].completeTask();
System.out.println(MESSAGE_TASK_MARKED + taskName);
} else {
System.out.println(taskName + MESSAGE_TASK_ALREADY_DONE);
}
}
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
}
private static int getTaskIndex(String taskName) {
for (int i = 1; i <= Task.getTaskCount(); i += 1) {
if (tasks[i].getTaskName().equals(taskName)) {
return i;
}
}
return -1;
}
private static void addTask(String taskName) {
if (isInList(taskName)) {
System.out.println(taskName + " is already in the list!");
} else {
Task newTask = new Task(taskName);
tasks[Task.getTaskCount()] = newTask;
System.out.println("Added " + taskName);
}
}

private static boolean isInList(String taskName) {
for (int i = 1; i <= Task.getTaskCount(); i += 1) {
if (tasks[i].getTaskName().equals(taskName)) {
return true;
}
}
return false;
}

private static void displayHelpMessage() {
//TODO to be improved
System.out.println("Invalid input!");
}

private static String[] processInput(String userInput) {
userInput = userInput.replace("/", "").trim();
String[] userInputs = new String[2];
userInputs = userInput.split("\\s+", 2);
return userInputs;
}

private static void displayTaskList() {
System.out.println("You have completed " + Task.getCompletedTaskCount() + " tasks. " + Task.getUncompletedTaskCount() + " more to go!");
for (int i = 1; i <= Task.getTaskCount(); i += 1) {
System.out.print(i + ".");
printTodoMark(i);
System.out.println(tasks[i].getTaskName());
}
}

private static void printTodoMark(int i) {
if (tasks[i].getTaskCompletionStatus()) {
System.out.print(TASK_COMPLETED_MARK);
} else {
System.out.print(TASK_UNCOMPLETED_MARK);
}
}

private static boolean isValidCommand(String command) {
return command.startsWith("/");
}

private static void displayExitMessage() {
System.out.println("Sleep mode activated.");
}

//creates a list of task (1-index)
private static void initialiseTaskList() {
tasks = new Task[MAX_TASK_COUNT + 1];
}

private static String getUserInput() {
String userInput;
Scanner in = new Scanner(System.in);
userInput = in.nextLine();
return userInput;
}

private static void displayWelcomeMessage() {
printHorizontalLine();
System.out.println(NANO_LOGO);
System.out.println("Serial number: 034-4532-5893.....");
System.out.println("Activating the 7th generation Nano Machine of the Chan Corporation.....");
printHorizontalLine();
System.out.println("How may I assist you?");
printHorizontalLine();
}

//print horizontal line
public static void printHorizontalLine() {
for (int i = 0; i < HORIZONTAL_LINE_LENGTH / 10; i += 1) {
System.out.print("__________");
}
System.out.println();
}
}


42 changes: 42 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class Task {
private String name;
private boolean isCompleted;
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
private static int taskCount = 0;
private static int completedTaskCount = 0;

public Task(String name) {
this.name = name;
this.isCompleted = false;
taskCount += 1;
}

public void completeTask() {
this.isCompleted = true;
completedTaskCount += 1;
}

public void incompleteTask() {
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
this.isCompleted = false;
completedTaskCount -= 1;
}

public String getTaskName() {
return this.name;
}

public boolean getTaskCompletionStatus() {
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
calebcjl marked this conversation as resolved.
Show resolved Hide resolved
return this.isCompleted;
}

public static int getTaskCount() {
return taskCount;
}

public static int getCompletedTaskCount() {
return completedTaskCount;
}

public static int getUncompletedTaskCount() {
return taskCount - completedTaskCount;
}
}