-
Notifications
You must be signed in to change notification settings - Fork 19
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
Level 1 to 3 (Week 2) #1
base: master
Are you sure you want to change the base?
Changes from 12 commits
3b19ba1
269e3ff
0f09980
d20f84a
eeb75ba
5d75ff9
f6cdbf9
c5d61c0
9624640
a75fcee
61b84e7
fe09317
dc20b21
73953a7
1be5731
365f5a8
7a2fa8a
2ee965d
a28ce65
4e3bb94
00f14ef
1471017
4578c64
7c0bebd
02fc569
cbe13a9
a1a2066
fd6ead5
ef20fc2
9991603
bd7293b
7adebbe
0bea941
f134e03
d54b09a
673a84c
839b98c
9c940b8
859a1bc
1ec8b9a
8d1655d
0ec3d22
a7badc2
c349451
fe0fd54
1250f58
5848c81
76b918f
bb13edd
b537754
dd66300
b72459a
897efa7
1d01229
6cb4a7a
c3ebc0c
0146c6b
456113a
db1c81f
3c390c0
e0a403d
25c4f7f
d64cd84
c53b4b1
f106611
6ddfe40
52be408
bab01ec
dd338ae
378bdd5
ae407e1
6f52eee
624fc46
776906a
916bef1
282404e
d7af8a7
a948fa5
77d1975
2d62376
dc753fa
474e3c2
a879088
283a704
9e5ecac
435a4b8
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,38 @@ | ||
package duke; | ||
|
||
import duke.command.CommandHandler; | ||
import duke.input.InputHandler; | ||
import duke.input.InputParser; | ||
import duke.output.DisplayHandler; | ||
import duke.storage.ListHandler; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
|
||
// Instantiate program components | ||
DisplayHandler displayHandler = new DisplayHandler(); | ||
InputHandler inputHandler = new InputHandler(); | ||
InputParser parser = new InputParser(); | ||
ListHandler list = new ListHandler(); | ||
CommandHandler command = new CommandHandler(); | ||
|
||
// Uses a Facade to Manage Individual Modular Components | ||
displayHandler.ProgramOpening(); | ||
|
||
while(true) { | ||
|
||
// Get User input | ||
String input = inputHandler.getUserInput(); | ||
// Parse User input | ||
String keyWord = parser.extractKeyWord(input); | ||
String body = parser.extractKeyWordBody(input, keyWord); | ||
|
||
// If input is not recognised Keyword | ||
if(parser.checkIfKeyWord(keyWord) == false) | ||
displayHandler.DisplayInvalidInput(); | ||
// If input is a recognised Keyword | ||
else | ||
command.checkCommandType(keyWord, body, list); | ||
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. Is it good to have curly brackets for if-else statement |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package duke.command;//import jdk.internal.util.xml.impl.Input; | ||
|
||
import duke.output.DisplayHandler; | ||
import duke.storage.ListHandler; | ||
import duke.exception.IncorrectInputException; | ||
import duke.input.InputParser; | ||
import duke.task.Task; | ||
|
||
public class CommandHandler { | ||
|
||
DisplayHandler displayHandler = new DisplayHandler(); | ||
InputParser inputParser = new InputParser(); | ||
|
||
public void checkCommandType(String KeyWord, String Body, ListHandler List) { | ||
|
||
KEYWORD keyword = KEYWORD.valueOf(KeyWord.toUpperCase()); | ||
|
||
try{ | ||
|
||
switch(keyword){ | ||
case TODO: | ||
String todoBody = inputParser.extractTodoBody(Body); | ||
List.addToDo(todoBody); | ||
Task todoTaskAdded = (Task) List.GetList().lastElement(); | ||
displayHandler.ShowTaskAdded(List.GetList().size(), todoTaskAdded); | ||
break; | ||
|
||
case DEADLINE: | ||
String deadlineBody = inputParser.extractDeadlineBody(Body); | ||
String deadlineByDate = inputParser.extractDeadlineByDay(Body); | ||
String deadlineByTime = inputParser.extractDeadlineByTime(Body); | ||
List.addDeadline(deadlineBody, deadlineByDate, deadlineByTime); | ||
Task deadlineTaskAdded = (Task) List.GetList().lastElement(); | ||
displayHandler.ShowTaskAdded(List.GetList().size(), deadlineTaskAdded); | ||
break; | ||
|
||
case EVENT: | ||
String eventBody = inputParser.extractEventBody(Body); | ||
String eventDay = inputParser.extractEventAtDay(Body);; | ||
String eventTime = inputParser.extractEventAtTime(Body); | ||
List.addEvent(eventBody, eventDay, eventTime); | ||
Task EventTaskAdded = (Task) List.GetList().lastElement(); | ||
displayHandler.ShowTaskAdded(List.GetList().size(), EventTaskAdded); | ||
break; | ||
|
||
case LIST: | ||
displayHandler.DisplayList(List.GetList()); | ||
break; | ||
|
||
case DONE: | ||
Task task = List.UpdateListItem(Body); | ||
displayHandler.DisplayChanges(task); | ||
break; | ||
|
||
case BYE: | ||
displayHandler.ProgramEnding(); | ||
System.exit(0); | ||
break; | ||
} | ||
|
||
} catch (Exception e){ | ||
if(e instanceof IncorrectInputException){ | ||
displayHandler.DisplayCustomException(e); | ||
} | ||
else { | ||
displayHandler.DisplayInvalidInput(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package duke.command; | ||
|
||
public enum KEYWORD { | ||
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. Perhaps we can use the PascalCase here for enum ? |
||
TODO, | ||
DEADLINE, | ||
EVENT, | ||
LIST, | ||
DONE, | ||
BYE | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package duke.exception; | ||
|
||
public class IncorrectInputException extends Exception { | ||
public IncorrectInputException(String message){ | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package duke.input; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputHandler { | ||
|
||
static private Scanner fromInputStream = new Scanner(System.in); | ||
|
||
public String getUserInput(){ | ||
|
||
String command = fromInputStream.nextLine(); | ||
|
||
return command; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package duke.input; | ||
|
||
import duke.output.DisplayHandler; | ||
import duke.command.KEYWORD; | ||
import duke.exception.IncorrectInputException; | ||
|
||
public class InputParser { | ||
|
||
// Check if First Word is Keyword | ||
public boolean checkIfKeyWord(String input) { | ||
|
||
try { | ||
KEYWORD.valueOf(input.toUpperCase()); | ||
return true; | ||
|
||
} catch (IllegalArgumentException e) { | ||
return false; | ||
} | ||
} | ||
|
||
// Extract Keyword from Input | ||
public String extractKeyWord(String input) { | ||
String[] Array = input.split(" "); | ||
return Array[0]; | ||
} | ||
|
||
// Extract Body from Input | ||
public String extractKeyWordBody(String input, String keyword) { | ||
try { | ||
return input.replaceFirst(keyword, ""); | ||
|
||
} catch(ArrayIndexOutOfBoundsException e){ // May not be used anymore | ||
return null; | ||
} | ||
} | ||
|
||
// For Todos | ||
public String extractTodoBody(String todoInput) throws IncorrectInputException { | ||
if(todoInput.isEmpty()) | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_TODO); | ||
|
||
return todoInput; | ||
} | ||
|
||
// For Deadlines | ||
public String extractDeadlineBody(String deadlineInput) throws IncorrectInputException { | ||
String result = ""; | ||
try{ | ||
int indx = deadlineInput.indexOf("/by"); | ||
result = deadlineInput.substring(0, indx); | ||
|
||
} catch (Exception e){ | ||
if(result.length() < 1){ | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_DEADLINE_DESC); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public String extractDeadlineByDay(String deadlineInput) throws IncorrectInputException{ | ||
String day = ""; | ||
try{ | ||
int indx = deadlineInput.indexOf("/by"); | ||
String by = deadlineInput.substring(indx, deadlineInput.length()); | ||
String[] array = by.split(" "); | ||
day = array[1]; | ||
return day; | ||
|
||
}catch (Exception e) { | ||
if(day.length() < 1){ | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_DEADLINE_DAY); | ||
} | ||
} | ||
|
||
return day; | ||
} | ||
|
||
public String extractDeadlineByTime(String deadlineInput) throws IncorrectInputException { | ||
String time = ""; | ||
try{ | ||
int indx = deadlineInput.indexOf("/by"); | ||
String by = deadlineInput.substring(indx); | ||
String[] array = by.split(" "); | ||
time = array[2]; | ||
|
||
} catch (Exception e) { | ||
if(time.length() < 1){ | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_DEADLINE_TIME); | ||
} | ||
} | ||
|
||
return time; | ||
} | ||
|
||
// For Events | ||
public String extractEventBody(String eventInput) throws IncorrectInputException { | ||
String result = ""; | ||
|
||
try{ | ||
int indx = eventInput.indexOf("/at"); | ||
result = eventInput.substring(0, indx); | ||
|
||
}catch (Exception e){ | ||
if(result.length() < 1){ | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_EVENT_DESC); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public String extractEventAtDay(String eventInput) throws IncorrectInputException{ | ||
String day = ""; | ||
|
||
try { | ||
int indx = eventInput.indexOf("/at"); | ||
String by = eventInput.substring(indx, eventInput.length()); | ||
String[] array = by.split(" "); | ||
day = array[1]; | ||
|
||
}catch (Exception e) { | ||
if(day.length() < 1){ | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_EVENT_DAY); | ||
} | ||
} | ||
return day; | ||
} | ||
|
||
public String extractEventAtTime(String eventInput) throws IncorrectInputException { | ||
String time = ""; | ||
|
||
try{ | ||
int indx = eventInput.indexOf("/at"); | ||
String by = eventInput.substring(indx); | ||
String[] array = by.split(" "); | ||
time = array[2]; | ||
|
||
} catch (Exception e) { | ||
if(time.length() < 1){ | ||
throw new IncorrectInputException(DisplayHandler.EMPTY_EVENT_TIME); | ||
} | ||
} | ||
|
||
return time; | ||
} | ||
} |
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.
Perhaps the ordering of the import statements could be reworked to align with the instantiation and dependencies below just before the while loop and inside of it as well ?