-
Notifications
You must be signed in to change notification settings - Fork 315
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
[Caesar Pang] Duke Increments #350
base: master
Are you sure you want to change the base?
Changes from 23 commits
65f72a8
0112efe
cfd6da7
6e6ace1
3a7a107
68a7aea
73a6527
dbb4abc
176e935
def7477
c0f8da0
e89761d
0c6b61e
5d44998
091b79e
c69f71c
6ef4264
935d978
780cd71
3bad8f8
8fd78d7
52f5392
b325ed9
49db2a4
f3c9622
1f1162d
7e5d619
cc08ea3
3cd7815
26474d5
ca17439
987507f
31ffff6
723f40d
9dfc61a
20b7205
72dfe57
019698e
d568912
6266873
1c7277a
5782932
5666c2b
b02e389
8bed604
1e0267f
1fe1e75
7d9bd97
0a81e39
b18a292
26b0cc8
ae98f7f
d541fae
7c475c5
b5a4861
9ace65c
ca838a8
6222066
c3a2655
28874c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class Deadline extends Task { | ||
|
||
protected String by; | ||
|
||
/** | ||
* Constructor for Deadline task. | ||
* | ||
* @param description Deadline task to be added. | ||
* @param by Date of the deadline task. | ||
*/ | ||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
/** | ||
* Method to give the string that is to be | ||
* added to the list of tasks. | ||
* | ||
* @return Returns the string to be loaded into | ||
* the file and printed out. | ||
*/ | ||
@Override | ||
public String toString() { | ||
String date = formatDate(by); | ||
if (getIsCorrectFormat()) { | ||
return "[D]" + super.toString() + description + " (by: " + date + ")"; | ||
} else { | ||
return "Invalid date format!"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,47 @@ | ||
import java.io.IOException; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
private Storage storage; | ||
private TaskList tasks; | ||
private Ui ui; | ||
|
||
/** | ||
* Constructor for Duke that takes in a file to add text into. | ||
* | ||
* @param filepath File that the task is added to. | ||
* @throws IOException If the named file exists but is a directory rather than a regular file, | ||
* does not exist but cannot be created, or cannot be opened for any other reason. | ||
*/ | ||
public Duke(String filepath) throws IOException { | ||
ui = new Ui(); | ||
storage = new Storage(filepath); | ||
try { | ||
tasks = new TaskList(storage.load()); | ||
} catch (DukeException | FileNotFoundException e) { | ||
ui.showLoadingError(); | ||
tasks = new TaskList(); | ||
} | ||
} | ||
|
||
/** | ||
* Contains the methods to start the bot and | ||
* start to take in inputs for the bot | ||
*/ | ||
public void run() { | ||
ui.greeting(); | ||
ui.nextCommand(); | ||
} | ||
|
||
/** | ||
* Main method. | ||
*/ | ||
public static void main(String[] args) throws IOException{ | ||
new Duke("todo.txt").run(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class DukeException extends Exception { | ||
|
||
/** | ||
* Constructor for Duke Exceptions. | ||
* | ||
* @param message takes in the error and | ||
* prints it out to the user. | ||
*/ | ||
public DukeException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class Event extends Task { | ||
|
||
protected String when; | ||
protected boolean isValid; | ||
|
||
/** | ||
* Constructor for Event task. | ||
* | ||
* @param description Event task to be attended. | ||
* @param when Date of the event. | ||
*/ | ||
public Event(String description, String when) { | ||
super(description); | ||
this.when = when; | ||
} | ||
|
||
/** | ||
* Method to give the string that is to be | ||
* added to the list of tasks. | ||
* | ||
* @return Returns the string to be loaded into | ||
* the file and printed out. | ||
*/ | ||
@Override | ||
public String toString() { | ||
String date = formatDate(when); | ||
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. You may want to consider storing this formatted date as a variable, so you don't have to keep converting it all the time. |
||
if (getIsCorrectFormat()) { | ||
return "[E]" + super.toString() + description + " (at: " + date + ")"; | ||
} else { | ||
return "Invalid date format!"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: Duke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import java.util.Scanner; | ||
|
||
public class Parser { | ||
|
||
Scanner sc = new Scanner(System.in); | ||
|
||
/** | ||
* Constructor for Parser class. | ||
*/ | ||
public Parser() { | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import java.io.*; | ||
import java.io.BufferedReader; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
protected static ArrayList<Task> taskList = new ArrayList<Task>(); | ||
protected static String file = "todo.txt"; | ||
|
||
public Storage(String file) { | ||
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. You should consider removing this parameter, since it doesn't seem to do anything. 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. You should consider removing this parameter, since it doesn't seem to do anything. |
||
|
||
} | ||
|
||
/** | ||
* Adds tasks to the file. | ||
* | ||
* @param filepath File that the task is added to. | ||
* @param textToAdd Tasks that needs to be added. | ||
* @throws IOException If the named file exists but | ||
* is a directory rather than a regular file, | ||
* does not exist but cannot be created, or | ||
* cannot be opened for any other reason. | ||
*/ | ||
public void addToFile(String filepath, String textToAdd) throws IOException { | ||
FileWriter typer = new FileWriter(filepath, true); | ||
typer.write(textToAdd + System.lineSeparator()); | ||
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. Have to say that I didn't know about this |
||
typer.close(); | ||
} | ||
|
||
/** | ||
* Writes task to a file. | ||
* Can be used as a way to overwrite tasks in the file as well. | ||
* | ||
* @param filepath File that the task is added to. | ||
* @param textToAdd Tasks that needs to be added. | ||
* @throws IOException If the named file exists but | ||
* is a directory rather than a regular file, | ||
* does not exist but cannot be created, or | ||
* cannot be opened for any other reason. | ||
*/ | ||
public void writeToFile(String filepath, String textToAdd) throws IOException { | ||
FileWriter typer = new FileWriter(filepath); | ||
typer.write(textToAdd); | ||
typer.close(); | ||
} | ||
|
||
/** | ||
* Counts the number of tasks in the list. | ||
* | ||
* @param filename File that the tasks are in. | ||
* @return Returns the number of tasks. | ||
* @throws IOException If the named file exists but | ||
* is a directory rather than a regular file, | ||
* does not exist but cannot be created, or | ||
* cannot be opened for any other reason. | ||
*/ | ||
public static int countLines(String filename) throws IOException { | ||
try (InputStream inputs = new BufferedInputStream(new FileInputStream(filename))) { | ||
byte[] characters = new byte[1024]; | ||
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. Just curious, is there a reason you chose to use |
||
int readCharacters = inputs.read(characters); | ||
if (readCharacters == -1) { | ||
// no lines to read | ||
return 0; | ||
} | ||
int count = 0; | ||
while (readCharacters == 1024) { | ||
for (int i = 0; i < 1024; ) { | ||
if (characters[i++] == '\n') { | ||
++count; | ||
} | ||
} | ||
readCharacters = inputs.read(characters); | ||
} | ||
// count remaining characters | ||
while (readCharacters != -1) { | ||
for (int i = 0; i < readCharacters; ++i) { | ||
if (characters[i] == '\n') { | ||
++count; | ||
} | ||
} | ||
readCharacters = inputs.read(characters); | ||
} | ||
|
||
return count == 0 ? 1 : count; | ||
} | ||
} | ||
|
||
/** | ||
* Loads the task into the tasklist | ||
* in TaskList from the file. | ||
* | ||
* @return ArrayList that has been copied from the file. | ||
* @throws IOException If the named file exists but is a directory rather than a regular file, | ||
* does not exist but cannot be created, or cannot be opened for any other reason. | ||
* @throws DukeException If there is nothing in the file to be loaded, | ||
* this exception will be thrown. | ||
*/ | ||
public ArrayList<Task> load() throws IOException, DukeException { | ||
File f = new File(file); | ||
Scanner sc = new Scanner(f); | ||
ArrayList<Task> tempList; | ||
if (countLines(file) == 0) { | ||
throw new DukeException("Woahsies wavy! There is nothing in this file!"); | ||
} else { | ||
while (sc.hasNext()) { | ||
String task = sc.nextLine(); | ||
int index = task.indexOf("["); | ||
String taskType = task.substring(index + 1, index + 2); | ||
int spaceIndex = task.indexOf(" "); | ||
switch (taskType) { | ||
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. There's a specific convention to follow for switch statements! https://nus-cs2103-ay1920s1.github.io/website/coding-standards/java/intermediate.html |
||
case "T": | ||
Task toDo = new Todo(task.substring(spaceIndex)); | ||
taskList.add(toDo); | ||
break; | ||
case "D": | ||
int byIndex = task.indexOf("("); | ||
Task deadline = new Deadline(task.substring(spaceIndex, byIndex - 1), | ||
task.substring(byIndex + 4)); | ||
taskList.add(deadline); | ||
break; | ||
case "E": | ||
int atIndex = task.indexOf("("); | ||
Task event = new Event(task.substring(spaceIndex, atIndex - 1), | ||
task.substring(atIndex + 4)); | ||
taskList.add(event); | ||
break; | ||
} | ||
} | ||
Ui.printIndent(); | ||
System.out.println("Your file has been loaded! :)"); | ||
tempList = new ArrayList<Task>(taskList); | ||
return tempList; | ||
} | ||
} | ||
} |
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.
I think this is a bad practice. You should probably catch the IOException somewhere within your code, rather than just
throw
ing it from yourmain
method.