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 5 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
23 changes: 23 additions & 0 deletions src/main/java/Deadlines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Deadlines extends Task {
private String endTime;
private String taskLabel = "[D]";
public Deadlines (String input){
super(input.substring(9,input.indexOf('/') - 1)); // Sanitize input by removing "deadline" at the start
Copy link

Choose a reason for hiding this comment

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

Personally, maybe you can replace the number 9 with a constant to avoid "magic numbers"?

Like private static final int DEADLINE_LABEL = 9;?

Copy link
Author

Choose a reason for hiding this comment

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

Agreed. Thanks for the idea!

super.setTaskLabel(taskLabel);
endTime = "(" + getEndTime(input) + ")";
}
// Method of StringBuffer operation taken from
Copy link

Choose a reason for hiding this comment

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

I like the way you use StringBuffer to operate on these strings, it's really useful.

// 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(2,":");
return deadlineCorrectFormat.toString();
}

@Override
public String toString(){
return this.taskLabel + this.mark + " " + this.description + " " + this.endTime;
}
}
87 changes: 87 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import jdk.jfr.Event;

Choose a reason for hiding this comment

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

where/how is this used?


import java.util.Arrays;
import java.util.Scanner;

public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
Expand All @@ -6,5 +11,87 @@ public static void main(String[] args) {
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
greetLine();
addList();
exitLine();
}
public static void greetLine(){
System.out.println("How may I be of service?");
}
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'd want to indent the { } properly, like:
public static void echo(){

// your code
}

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.

Use K&R style brackets (as done in the exitLine method)

Task[] list = new Task[100];
String line;
Scanner in = new Scanner(System.in);
line = in.nextLine();
int numOfItems = 0;
while(!line.equals("bye")) { // condition to shut down program
if(line.equals("list")) { // users wants to know all text so far
printCurrentList(list, numOfItems);
}
else if(line.startsWith("mark")){
mark(line, list);
printCurrentList(list, numOfItems);
}
else if(line.startsWith("unmark")){
unmark(line,list);
printCurrentList(list, numOfItems);
}
else { // new tasks keyed in by user
Task newTask = new Task(line);
if(line.startsWith("todo")){
newTask = new ToDos(line);
}
else if(line.startsWith("deadline")){

Choose a reason for hiding this comment

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

change the form of the if/else statements to match the coding standard:

if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}

Choose a reason for hiding this comment

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

Yeah, you can refer to this link [https://se-education.org/guides/conventions/java/basic.html] for if-else statement coding standards.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks guys!

newTask = new Deadlines(line);
}
else{
newTask = new Events(line);
}
list[numOfItems] = newTask;
++numOfItems;
}
line = in.nextLine(); // read in next line of text
}
}

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

public static void unmark(String task, Task[] list)
{
String indexOfTask = task.substring(7);
Task taskToBeMarked = list[Integer.parseInt(indexOfTask) - 1]; // convert from 1-based to 0-based
taskToBeMarked.unmarkTask();
System.out.println("Sir, your task has been unmarked as requested.");
}

public static void printCurrentList(Task[] list, int numOfItems)

Choose a reason for hiding this comment

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

nice job making clear method and variable names!

{
Task[] subList = Arrays.copyOf(list, numOfItems);
System.out.println("Your current list of items as requested, sir.");
for(int i = 0; i < subList.length; ++i)
{
System.out.println(Integer.toString(i+1) + "." + subList[i]);
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/Events.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Events extends Task{
private String timeLine;
private String taskLabel = "[E]";
public Events (String input){
super(input.substring(6,input.indexOf('/') - 1));
super.setTaskLabel(taskLabel);
timeLine = "(" + getStartTime(input) + getEndTime(input) + ")";
}
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(4,":");
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(2,":");
return endTimeCorrectFormat.toString();
}
@Override
public String toString(){
return this.taskLabel + this.mark + " " + this.description + " " + this.timeLine;
}
}
31 changes: 31 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Task {
protected String taskLabel;

Choose a reason for hiding this comment

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

I like how you have added another variable called taskLabel.

This is actually clear while storing the data in it instead of explicitly typing [D] / [E] / [T] every time while printing the lists.

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 = "[ ]";
}
}
11 changes: 11 additions & 0 deletions src/main/java/ToDos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ToDos extends Task {
private String taskLabel = "[T]";
public ToDos (String input){
super(input.substring(5)); // for ToDos tasks, description = input
super.setTaskLabel(taskLabel);
}
@Override
public String toString(){
return this.taskLabel + this.mark + " " + this.description;
}
}
2 changes: 2 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Hello from
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

How may I be of service?
Glad I could be of help!
2 changes: 2 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
todo CS2113 assignment
bye