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

[Wang Silang] iP #214

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|


```

wang silang
61 changes: 61 additions & 0 deletions src/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

177 changes: 177 additions & 0 deletions src/main/java/Duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package Duke;

import Duke.Exception.EmptyDeadlineException;
import Duke.Exception.EmptyEventsException;
import Duke.Exception.EmptyToDoException;
import Duke.Exception.NullCommandException;
import Duke.Task.Deadlines;
import Duke.Task.Events;
import Duke.Task.Task;
import Duke.Task.ToDos;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileNotFoundException;


public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
try {
printFileContents("dukeData.txt");
} catch (FileNotFoundException e){
System.out.println("File not found");
}
greetLine();
addList();
exitLine();
}

private static final int MARK_INDEX = 5;
private static final int UNMARK_INDEX = 7;
private static final int DELETE_INDEX = 7;

public static void greetLine() {
System.out.println("How may I be of service today?");
}

public static void echo() {
String line;
Scanner in = new Scanner(System.in);
line = in.nextLine();
while (!line.equals("bye")) {
System.out.println(line);
line = in.nextLine();
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove methods that are not being used to reduce redundant code and improve readability.


public static void exitLine() {
System.out.println("Glad I could be of help!");
}

public static void addList() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do remember to provide appropriate names to methods. The method below is not just adding a list or adding to a list.

ArrayList<Task> list = new ArrayList<>();
String line;
Scanner in = new Scanner(System.in);
line = in.nextLine();
while (!line.equals("bye")) { // condition to shut down program
if (line.equals("list")) { // users wants to know all text so far
printCurrentList(list);
} else if (line.startsWith("mark")) {
mark(line, list);
dukeDataStorage(arraylistToStringConverter(list));
} else if (line.startsWith("unmark")) {
unmark(line, list);
dukeDataStorage(arraylistToStringConverter(list));
} else if (line.startsWith("delete")) {
delete(line, list);
dukeDataStorage(arraylistToStringConverter(list));
} else {// new tasks keyed in by user
try {
Task newTask = new Task(line);
if (line.startsWith("todo")) {
newTask = new ToDos(line);
list.add(newTask);
} else if (line.startsWith("deadline")) {
newTask = new Deadlines(line);
list.add(newTask);
} else if (line.startsWith("event")) {
newTask = new Events(line);
list.add(newTask);
} else {
throw new NullCommandException();
}
dukeDataStorage(arraylistToStringConverter(list));
} catch (EmptyToDoException e) {
System.out.println("Sire, you have yet to tell me what is it you want to do.");
} catch (EmptyDeadlineException e) {
System.out.println("Sire, what is it that is due your specified time?");
} catch (EmptyEventsException e) {
System.out.println("Sire, your event is unclear. Please specify.");
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Don't hold back Sire. I am here to serve.");
} catch (NullCommandException e) {
System.out.println("Sire, I am not trained to understand gibberish.");
}
}
line = in.nextLine(); // read in next line of text
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to have less that 30 LOC for any method. You can split the contents of this method into multiple methods.


public static void mark(String task, ArrayList<Task> list) {
String indexOfTask = task.substring(MARK_INDEX); // get the number of task to be marked
Task taskToBeMarked = list.get(Integer.parseInt(indexOfTask) - 1); // convert from 1-based to 0-based
taskToBeMarked.markTask();
System.out.println("Sir, your task has been marked as completed.");
System.out.println(list.get(Integer.parseInt(indexOfTask) - 1));
}

public static void unmark(String task, ArrayList<Task> list) {
String indexOfTask = task.substring(UNMARK_INDEX);
Task taskToBeUnmarked = list.get(Integer.parseInt(indexOfTask) - 1);
taskToBeUnmarked.unmarkTask();
System.out.println("Sir, your task has been unmarked as requested.");
System.out.println(list.get(Integer.parseInt(indexOfTask) - 1));
}

public static void printCurrentList(ArrayList<Task> list) {
System.out.println("Your current list of items as requested, sir.");
for (int i = 0; i < list.size(); ++i) {
System.out.println((i + 1) + "." + list.get(i));
}
}

public static void delete(String task, ArrayList<Task> list) {
String indexOfTask = task.substring(DELETE_INDEX);
System.out.println("Sire, I have removed this task from your schedule");
System.out.println(list.get(Integer.parseInt(indexOfTask) - 1));
list.remove(Integer.parseInt(indexOfTask) - 1);
System.out.println("You now have " + list.size() + " items left");
}

public static void dukeDataStorage(String taskToBeStored) {
File f = new File("dukeData.txt");
try {
if (!f.exists()) {
f.createNewFile();
}
writeToFile("dukeData.txt", taskToBeStored);
} catch (IOException e) {
System.out.println("File creation of writing invalid");
}
}

private static void writeToFile(String filePath, String textToAdd) throws IOException {
FileWriter fw = new FileWriter(filePath);
fw.write(textToAdd);
fw.close();
}

private static void printFileContents(String filePath) throws FileNotFoundException {
File f = new File(filePath); // create a File for the given file path
if (!f.exists()) { // for first log in, there is no file
return;
}
System.out.println("Good day sire, I have listed down your current plan below for you:");
Scanner s = new Scanner(f); // create a Scanner using the File as the source
while (s.hasNext()) {
System.out.println(s.nextLine());
}
}

private static String arraylistToStringConverter(ArrayList<Task> list) {
String output = "";
for (Task t : list) {
output = output + t + System.lineSeparator();
}
return output;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job refactoring the code!

4 changes: 4 additions & 0 deletions src/main/java/Duke/Exception/EmptyDeadlineException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package Duke.Exception;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names representing packages should be in all lower case. Look at Java coding standards for more details.


public class EmptyDeadlineException extends Exception {
}
4 changes: 4 additions & 0 deletions src/main/java/Duke/Exception/EmptyEventsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package Duke.Exception;

public class EmptyEventsException extends Exception {
}
4 changes: 4 additions & 0 deletions src/main/java/Duke/Exception/EmptyToDoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package Duke.Exception;

public class EmptyToDoException extends Exception {
}
4 changes: 4 additions & 0 deletions src/main/java/Duke/Exception/NullCommandException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package Duke.Exception;

public class NullCommandException extends Exception {
}
35 changes: 35 additions & 0 deletions src/main/java/Duke/Task/Deadlines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Duke.Task;

import Duke.Exception.EmptyDeadlineException;
import Duke.Task.Task;

public class Deadlines extends Task {
private static final int DEADLINE_INDEX = 9;
private static final int COLON_INDEX = 2;
private String endTime;
private String taskLabel = "[D]";

public Deadlines(String input) throws EmptyDeadlineException {
super(input.substring(DEADLINE_INDEX, input.indexOf('/') - 1)); // Sanitize input by removing "deadline" at the start
super.setTaskLabel(taskLabel);
endTime = "(" + getEndTime(input) + ")";
if (input.substring(DEADLINE_INDEX, input.indexOf('/') - 1).isEmpty()) {
throw new EmptyDeadlineException();
}
}

// Method of StringBuffer operation taken from
// https://www.geeksforgeeks.org/insert-a-string-into-another-string-in-java/
@Override
public String getEndTime(String input) {
String deadline = input.substring(input.indexOf('/') + 1); // endTime of task is the string after '/'
StringBuffer deadlineCorrectFormat = new StringBuffer(deadline); // convert to StringBuffer for inserting ':'
deadlineCorrectFormat.insert(COLON_INDEX, ":");
return deadlineCorrectFormat.toString();
}

@Override
public String toString() {
return this.taskLabel + this.mark + " " + this.description + " " + this.endTime;
}
}
47 changes: 47 additions & 0 deletions src/main/java/Duke/Task/Events.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package Duke.Task;

import Duke.Exception.EmptyEventsException;

public class Events extends Task {
private static final int EVENT_INDEX = 6;
private static final int FIRST_COLON_INDEX = 4;
private static final int SECOND_COLON_INDEX = 2;
private String timeLine;
private String taskLabel = "[E]";
public Events(String input) throws EmptyEventsException {
super(input.substring(EVENT_INDEX, input.indexOf('/') - 1));
super.setTaskLabel(taskLabel);
timeLine = "(" + getStartTime(input) + getEndTime(input) + ")";
if (input.substring(EVENT_INDEX, input.indexOf('/') - 1).isEmpty()) {
throw new EmptyEventsException();
}
}

public String[] splitInput(String input) {
String[] inputAfterSplit = input.split("/", 3); // split twice to generate three strings
return inputAfterSplit;
}

@Override
public String getStartTime(String input) {
String[] inputAfterSplit = splitInput(input);
String startTime = inputAfterSplit[1];
StringBuffer startTimeCorrectFormat = new StringBuffer(startTime);
startTimeCorrectFormat.insert(FIRST_COLON_INDEX, ":");
return startTimeCorrectFormat.toString();
}

@Override
public String getEndTime(String input) {
String[] inputAfterSplit = splitInput(input);
String startTime = inputAfterSplit[2];
StringBuffer endTimeCorrectFormat = new StringBuffer(startTime); // convert to StringBuffer for inserting ':'
endTimeCorrectFormat.insert(SECOND_COLON_INDEX, ":");
return endTimeCorrectFormat.toString();
}

@Override
public String toString() {
return this.taskLabel + this.mark + " " + this.description + " " + this.timeLine;
}
}
33 changes: 33 additions & 0 deletions src/main/java/Duke/Task/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Duke.Task;

public class Task {
protected String taskLabel;
protected String description;
protected String mark;
public Task(String input) {
this.description = input;
this.taskLabel = "To be replaced by labels";
this.mark = "[ ]";
}
public String getTaskLabel() {
return taskLabel;
}
public String getDescription() {
return description;
}
public String getStartTime(String input){
return "To be overridden by subclass' methods";
}
public String getEndTime(String input){
return "To be overridden by subclass' methods";
}
public void setTaskLabel(String taskLabel) {
this.taskLabel = taskLabel;
}
public void markTask(){
this.mark = "[X]";
}
public void unmarkTask(){
this.mark = "[ ]";
}
}
Loading