-
Notifications
You must be signed in to change notification settings - Fork 187
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
[Huang Maodian] iP #190
base: master
Are you sure you want to change the base?
[Huang Maodian] iP #190
Changes from 10 commits
070e1ef
708a584
19f083f
d23eea2
ddc3476
ce45d70
fb1d2b4
fe304ab
dc155ec
7007250
5bba707
a8a99b1
73d4f84
5cbefdc
ad834e9
ff9c386
eb072c6
f8a7df6
28951cb
df262ed
b4f967b
d370e77
65c9c27
16fbedf
dc722b0
2bb5550
38f1745
669853c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package huan.main; | ||
|
||
import huan.task.*; | ||
|
||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
public class Huan { | ||
private static List<Task> tasks = new ArrayList<>(); | ||
|
||
public static Boolean isIndexValid(int index) { | ||
return index >= 1 && index <= tasks.size(); | ||
} | ||
public static void addTask(Task newTask) { | ||
tasks.add(newTask); | ||
} | ||
|
||
public static void listTasks() { | ||
int cnt = 0; | ||
System.out.println("You have a total of " + tasks.size() + " tasks."); | ||
for (Task task : tasks) { | ||
cnt += 1; | ||
System.out.printf(cnt + ". "); | ||
|
||
switch (task.getTaskType()) { | ||
case (1): | ||
TodoTask todoTask = (TodoTask)task; | ||
todoTask.printTask(); | ||
break; | ||
case (2): | ||
EventTask eventTask = (EventTask)task; | ||
eventTask.printTask(); | ||
break; | ||
case (3): | ||
DeadlineTask deadlineTask = (DeadlineTask)task; | ||
deadlineTask.printTask(); | ||
break; | ||
default: | ||
task.printTask(); | ||
break; | ||
} | ||
} | ||
} | ||
public static void readProcessCommand() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very long method, maybe try to abstract each command? |
||
|
||
Scanner scanner = new Scanner(System.in); | ||
while(true) { | ||
System.out.println("-------------------------"); | ||
String inputCommand = scanner.nextLine(); | ||
String[] words = inputCommand.split(" "); | ||
String firstWord = words[0]; | ||
String suffixWord; | ||
if(words.length > 1) { | ||
suffixWord = inputCommand.substring(words[0].length() + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiple usage of Magic numbers, try to assign the number with a meaningful variable name for easy understand of what the number is for. |
||
} | ||
else { | ||
suffixWord = ""; | ||
} | ||
switch (firstWord) { | ||
case ("bye"): | ||
if(!suffixWord.isEmpty()) { | ||
System.out.println("Invalid format! Should be 'bye'"); | ||
break; | ||
} | ||
System.out.println("Bye! See ya!"); | ||
return; | ||
case ("list"): | ||
if(!suffixWord.isEmpty()) { | ||
System.out.println("Invalid format! Should be 'list'."); | ||
break; | ||
} | ||
listTasks(); | ||
break; | ||
case ("mark"): | ||
try { | ||
int markIndex = Integer.parseInt(suffixWord); | ||
if (!isIndexValid(markIndex)) { | ||
System.out.println("Invalid task index!"); | ||
} else { | ||
tasks.get(markIndex - 1).setIsDone(true); | ||
System.out.println("Set task number " + markIndex + ": " + tasks.get(markIndex - 1).getName() + " as done."); | ||
} | ||
} catch (Exception e){ | ||
System.out.println("Incorrect format! Should be 'mark *n', where n is the index of the task you wish to mark as finished."); | ||
} | ||
break; | ||
case ("unmark"): | ||
try { | ||
int unmarkIndex = Integer.parseInt(suffixWord); | ||
if (!isIndexValid(unmarkIndex)) { | ||
System.out.println("Invalid task index!"); | ||
} else { | ||
tasks.get(unmarkIndex - 1).setIsDone(false); | ||
System.out.println("Set task number " + unmarkIndex + ": " + tasks.get(unmarkIndex - 1).getName() + " as not done."); | ||
} | ||
} catch (Exception e){ | ||
System.out.println("Incorrect format! Should be 'unmark *n', where n is the index of the task you wish to mark as unfinished."); | ||
} | ||
break; | ||
case ("todo"): | ||
if(suffixWord.isEmpty()) { | ||
System.out.println("Incorrect format! Should be 'todo *task_name'."); | ||
break; | ||
} | ||
TodoTask todoTask = new TodoTask(suffixWord, false); | ||
addTask(todoTask); | ||
System.out.println("Added todo type task with name: " + todoTask.getName()); | ||
break; | ||
case ("event"): | ||
try { | ||
EventTask eventTask = new EventTask(suffixWord, false); | ||
addTask(eventTask); | ||
System.out.println("Added event type task with name: " + eventTask.getName()); | ||
} catch (Exception e) { | ||
System.out.println("Incorrect format! Should be 'event *event_name /from *start_time /to *end_time'."); | ||
} | ||
break; | ||
case ("deadline"): | ||
try { | ||
DeadlineTask deadlineTask = new DeadlineTask(suffixWord, false); | ||
addTask(deadlineTask); | ||
System.out.println("Added deadline type task with name: " + deadlineTask.getName()); | ||
} catch (Exception e) { | ||
System.out.println("Incorrect format! Should be 'deadline *task_name /by *deadline_time'"); | ||
} | ||
break; | ||
|
||
default: | ||
System.out.println("Unrecognized command, please try again!"); | ||
break; | ||
} | ||
|
||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
String botName = "Huan"; | ||
System.out.println("Hello! I'm " + botName + ", a chat bot"); | ||
|
||
readProcessCommand(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package huan.task; | ||
|
||
import java.util.Objects; | ||
|
||
public class DeadlineTask extends Task{ | ||
private String ddlTime; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe can consider calling it deadlineTime, as ddlTime can be hard to understand what it means |
||
public DeadlineTask(String nameWithDate, Boolean isDone) throws Exception{ | ||
StringBuilder ddlTime = new StringBuilder(); | ||
StringBuilder name = new StringBuilder(); | ||
String[] words = nameWithDate.split(" "); | ||
/* | ||
state: | ||
0 means currently concatenating name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can consider using ENUM instead of 0,1 for the state |
||
1 means currently concatenating ddlTime | ||
*/ | ||
int state = 0; | ||
for(String word : words) { | ||
if (Objects.equals(word, "/by")) { | ||
state += 1; | ||
} | ||
else { | ||
switch (state) { | ||
case (0): | ||
name.append((name.length() == 0) ? "" : " ").append(word); | ||
break; | ||
case (1): | ||
ddlTime.append((ddlTime.length() == 0 ? "" : " ")).append(word); | ||
break; | ||
} | ||
} | ||
} | ||
if(state != 1 || name.toString().isEmpty() || isDone.toString().isEmpty()) { | ||
throw new Exception(); | ||
} | ||
setName(name.toString()); | ||
setIsDone(isDone); | ||
setTaskType(3); | ||
this.ddlTime = ddlTime.toString(); | ||
} | ||
|
||
public void printTask() { | ||
System.out.println("[D][" + (getIsDone() ? "X" : " ") + "] " + getName() + " (by: " + ddlTime + ")"); | ||
} | ||
|
||
public String getDdlTime() { | ||
return ddlTime; | ||
} | ||
|
||
public void setDdlTime(String ddlTime) { | ||
this.ddlTime = ddlTime; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package huan.task; | ||
|
||
import java.util.Objects; | ||
|
||
public class EventTask extends Task{ | ||
private String startTime, endTime; | ||
public EventTask(String nameWithDates, Boolean isDone) throws Exception { | ||
StringBuilder startTime = new StringBuilder(); | ||
StringBuilder endTime = new StringBuilder(); | ||
StringBuilder name = new StringBuilder(); | ||
String[] words = nameWithDates.split(" "); | ||
/* | ||
state: | ||
0 means currently concatenating name | ||
1 means currently concatenating startTime | ||
2 means currently concatenating endTime | ||
*/ | ||
int state = 0; | ||
for(String word : words) { | ||
if (Objects.equals(word, "/from")) { | ||
state += 1; | ||
} | ||
else if (Objects.equals(word, "/to")) { | ||
state += 1; | ||
} | ||
else { | ||
switch (state) { | ||
case (0): | ||
name.append((name.length() == 0) ? "" : " ").append(word); | ||
break; | ||
case (1): | ||
startTime.append((startTime.length() == 0 ? "" : " ")).append(word); | ||
break; | ||
case (2): | ||
endTime.append((endTime.length() == 0 ? "" : " ")).append(word); | ||
break; | ||
} | ||
} | ||
} | ||
if(state != 2 || name.toString().isEmpty() || startTime.toString().isEmpty() || endTime.toString().isEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very long and complicated statement, can consider splitting it up |
||
throw new Exception(); | ||
} | ||
setName(name.toString()); | ||
setIsDone(isDone); | ||
setTaskType(2); | ||
this.startTime = startTime.toString(); | ||
this.endTime = endTime.toString(); | ||
} | ||
public void printTask() { | ||
System.out.println("[E][" + (getIsDone() ? "X" : " ") + "] " + getName() + " (from: " + startTime + " to: " + endTime + ")"); | ||
} | ||
|
||
public void setStartTime(String startTime) { | ||
this.startTime = startTime; | ||
} | ||
|
||
public String getStartTime() { | ||
return startTime; | ||
} | ||
|
||
public void setEndTime(String endTime) { | ||
this.endTime = endTime; | ||
} | ||
|
||
public String getEndTime() { | ||
return endTime; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package huan.task; | ||
public class Task { | ||
private String name; | ||
private Boolean isDone; | ||
/** | ||
* used for casting back types | ||
* 0 for no specific types | ||
* 1 for Todo types | ||
* 2 for Event types | ||
* 3 for Deadline types | ||
*/ | ||
private int taskType; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setIsDone(Boolean isDone) { | ||
this.isDone = isDone; | ||
} | ||
|
||
public void setTaskType(int taskType) { | ||
this.taskType = taskType; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Boolean getIsDone() { | ||
return isDone; | ||
} | ||
|
||
public int getTaskType() { | ||
return taskType; | ||
} | ||
|
||
public void printTask() { | ||
System.out.println("[" + (isDone ? "X" : " ") + "] " + name); | ||
} | ||
public Task() { | ||
setName("task"); | ||
setIsDone(false); | ||
taskType = 0; | ||
} | ||
|
||
public Task(String name, Boolean isDone) { | ||
setName(name); | ||
setIsDone(isDone); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package huan.task; | ||
|
||
public class TodoTask extends Task{ | ||
public TodoTask(String name, Boolean isDone) { | ||
super(name, isDone); | ||
setTaskType(1); | ||
} | ||
public void printTask() { | ||
System.out.println("[T][" + (getIsDone() ? "X" : " ") + "] " + getName()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Hello! I'm Huan, a chat bot | ||
------------------------- | ||
Added todo type task with name: read book | ||
------------------------- | ||
Added deadline type task with name: return book | ||
------------------------- | ||
Added event type task with name: project meeting | ||
------------------------- | ||
Set task number 1: read book as done. | ||
------------------------- | ||
Added todo type task with name: join sports club | ||
------------------------- | ||
Added todo type task with name: borrow book | ||
------------------------- | ||
Set task number 4: join sports club as done. | ||
------------------------- | ||
You have a total of 5 tasks. | ||
1. [T][X] read book | ||
2. [D][ ] return book (by: June 6th) | ||
3. [E][ ] project meeting (from: Aug 6th 2pm to: 4pm) | ||
4. [T][X] join sports club | ||
5. [T][ ] borrow book | ||
------------------------- | ||
Bye! See ya! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
todo read book | ||
deadline return book /by June 6th | ||
event project meeting /from Aug 6th 2pm /to 4pm | ||
mark 1 | ||
todo join sports club | ||
todo borrow book | ||
mark 4 | ||
list | ||
bye |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid importing *, try to explicitly list out the import classes