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

Michael Ong iP #235

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3b19ba1
Add Gradle support
May 24, 2020
a75fcee
build.gradle: Update version to 8.29
Aug 29, 2020
4498a6b
test
maikongeh Jan 21, 2021
3ba23a5
level1
maikongeh Jan 21, 2021
1332167
level 2
maikongeh Jan 21, 2021
bf447d2
level 4
maikongeh Jan 21, 2021
dd958f6
level 6
maikongeh Jan 21, 2021
a4a2685
Level 7
maikongeh Jan 28, 2021
7079a47
Level-8
maikongeh Feb 4, 2021
6988915
A-MoreOOP
maikongeh Feb 4, 2021
8c6a077
A-Packages
maikongeh Feb 4, 2021
8572393
Commit before taskList
maikongeh Feb 5, 2021
9dd0f7e
Implement TaskList
maikongeh Feb 5, 2021
b2618e0
A-JavaDoc
maikongeh Feb 8, 2021
2b500b2
A-CodingStandard
maikongeh Feb 9, 2021
8cb2983
Merge branch 'Branch-A-CodingStandard'
maikongeh Feb 9, 2021
ee2df78
Level-9
maikongeh Feb 9, 2021
fbb0793
Merge branch 'branch-Level-9'
maikongeh Feb 9, 2021
d984a52
Merge branch 'add-gradle-support'
maikongeh Feb 9, 2021
94ecea4
A-Gradle
maikongeh Feb 9, 2021
80cb4ab
Merge branch 'branch-A-Gradle'
maikongeh Feb 9, 2021
be0283c
Save Before GUI
maikongeh Feb 16, 2021
57b5d4e
Level-10
maikongeh Feb 16, 2021
e178e21
Merge branch 'branch-Level-10'
maikongeh Feb 16, 2021
0080b9c
A-Assertions
maikongeh Feb 16, 2021
ec0b360
Merge branch 'branch-Assertions'
maikongeh Feb 16, 2021
b6558dc
A-UserGuide
maikongeh Feb 19, 2021
197fc2a
fix bugs found
maikongeh Feb 19, 2021
80b1fea
update final file
maikongeh Feb 19, 2021
7cf7620
update
maikongeh Feb 19, 2021
53e6063
improve codeQuality
maikongeh Feb 22, 2021
9720822
Merge branch 'branch-A-codeQuality'
maikongeh Feb 22, 2021
669c7dc
move Ui.png to ip folder
maikongeh Feb 22, 2021
687d765
Create config.yml file Set theme jekyll-theme-cayman
maikongeh Feb 27, 2021
27d8d64
Merge branch 'branch-UserGuide'
maikongeh Feb 27, 2021
2b332ac
Set theme jekyll-theme-cayman
maikongeh Feb 27, 2021
37032d1
Set theme jekyll-theme-cayman
maikongeh Feb 27, 2021
45b9fb5
Set theme jekyll-theme-cayman
maikongeh Feb 27, 2021
3993d29
Update README.md
maikongeh Feb 27, 2021
78260bb
Merge branch 'master' of https://github.com/maikongeh/ip
maikongeh Mar 15, 2021
fdd22b6
reduce long methods and deep nesting
maikongeh Mar 15, 2021
8cbcf22
Merge branch 'master' of https://github.com/maikongeh/ip
maikongeh Mar 15, 2021
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
3 changes: 3 additions & 0 deletions data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[T][✘] dknslsald
[E][✘] lfdknsdf (at: lfdsnfl)
[D][✓] saldna (by: kshdflad)
47 changes: 47 additions & 0 deletions src/main/java/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.io.File;
Copy link

Choose a reason for hiding this comment

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

Your import statements are very nicely formatted 🎉

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

public class Database {
Copy link

Choose a reason for hiding this comment

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

Consider adding a couple of class-level comments describing the class.
Might not be immediately clear otherwise what this Database class is intended for.


String name;


Copy link

Choose a reason for hiding this comment

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

You might have inadvertently added an extra blank line here:
https://se-education.org/guides/conventions/java/intermediate.html#layout

Suggested change

public Database(String name){
this.name = name;
}

public ArrayList<String> readFile() throws FileNotFoundException {
try {

Choose a reason for hiding this comment

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

I like your indentations in this class!

ArrayList<String> tasksStringName = new ArrayList<>();
File f = new File(name);
Scanner s = new Scanner(f);
while (s.hasNext()) {
tasksStringName.add(s.nextLine());
}
return tasksStringName;
} catch (FileNotFoundException e) {
throw new FileNotFoundException("File not found");
}
}

public void writeTaskToFile(List<Task> tasks){
Copy link

Choose a reason for hiding this comment

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

Be careful of the spaces 😉

Suggested change
public void writeTaskToFile(List<Task> tasks){
public void writeTaskToFile(List<Task> tasks){

String string = "";
for (Task task : tasks) {
string = string + task.toString() + "\n";
}
try {
FileWriter fileWriter = new FileWriter(name);
fileWriter.write(string);
fileWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}

}
}

14 changes: 14 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Deadline extends Task {

private String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + this.by + ")";
}
}
265 changes: 258 additions & 7 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,261 @@
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);

public static void main(String[] args) throws DukeException, FileNotFoundException {
Database database = new Database( "data.txt");
ArrayList<String> listOfTasks;
ArrayList<Task> myList;
try {
Copy link

Choose a reason for hiding this comment

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

The indentation is a little off here: should be in line with the block.

Suggested change
try {
try {

listOfTasks = database.readFile();
myList = readInput(listOfTasks);
}
catch (FileNotFoundException e){
Copy link

Choose a reason for hiding this comment

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

The catch keyword should be in line with the closing brace.

Suggested change
}
catch (FileNotFoundException e){
} catch (FileNotFoundException e){

throw new FileNotFoundException("No File Detected");
}
System.out.println("____________________________________");

Choose a reason for hiding this comment

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

Would it be better to have this line format as a constant since it is repeated several times in the class?

System.out.println("Hello! I'm Duke\nWhat can I do for you?");
System.out.println("____________________________________");

Scanner input = new Scanner(System.in);
while(input.hasNextLine()) {

Choose a reason for hiding this comment

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

Maybe it would be better if you avoid deep nesting to improve code quality

Copy link

Choose a reason for hiding this comment

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

Another space missed 😉

Suggested change
while(input.hasNextLine()) {
while (input.hasNextLine()) {

String s = input.next();
switch (s){
case "todo":
System.out.println("____________________________________");
Copy link

Choose a reason for hiding this comment

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

The case statement should be in line with the switch, and the case blocks one indentation lower.

Suggested change
switch (s){
case "todo":
System.out.println("____________________________________");
switch (s){
case "todo":
System.out.println("____________________________________");

try {
String s1 = input.nextLine();
Copy link

Choose a reason for hiding this comment

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

You might have missed this: variable names could do with a more specific name, especially since the scope isn't small. Perhaps s1 can be renamed as userInputLine.

if (s1.equals("")) {
throw new DukeException("Enter a valid todo");
} else {
char[] chars = s1.toCharArray();
if(chars[1] == ' '){
Copy link

Choose a reason for hiding this comment

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

Another missing space 😉

throw new DukeException("Enter valid todo");
} else {
try{
Copy link

Choose a reason for hiding this comment

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

And another 😉

Copy link

Choose a reason for hiding this comment

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

Applies to a couple other lines below.

System.out.println("Got it. I've added this todo:");
String desc = s1.substring(1);
Todo newTodo = new Todo(desc);
System.out.println(" " + newTodo.toString());
myList.add(newTodo);
System.out.println("Now you have " + myList.size() + " tasks in the list.");
database.writeTaskToFile(myList);
} catch (Exception e){
System.out.println("Enter valid todo");
}

}
}
} catch (DukeException e){
System.out.println(e.getMessage());
}
System.out.println("____________________________________");
break;

case "deadline":

System.out.println("____________________________________");
try{
String s1 = input.nextLine();
if(s1.equals("")){
throw new DukeException("Enter valid deadline task.");
} else {
try {
String wholeString = s1.substring(1);
String[] parts = wholeString.split(" /by ");
String deadlineDesc = parts[0];
if(parts.length == 1){
throw new DukeException("Please adhere to convention:\n(task /by deadline timing)");
} else {
String deadline = parts[1];
System.out.println("Got it. I've added this deadline:");
Deadline newDeadline = new Deadline(deadlineDesc, deadline);
System.out.println(" " + newDeadline.toString());
myList.add(newDeadline);
System.out.println("Now you have " + myList.size() + " tasks in the list.");
database.writeTaskToFile(myList);

}
} catch(DukeException e) {
Copy link

Choose a reason for hiding this comment

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

Not a coding standard, but figured I could chip in. Since the catch block is the same as that below in line 86, you can consider removing the try-except block here.

System.out.println(e.getMessage());
}
}

} catch (DukeException e){
System.out.println(e.getMessage());
}
System.out.println("____________________________________");
break;

case "event":

System.out.println("____________________________________");
try{
String s1 = input.nextLine();
if(s1.equals("")){
throw new DukeException("Enter valid Event.");
} else {
try {
String wholeString = s1.substring(1);
String[] parts = wholeString.split(" /at ");
String eventDesc = parts[0];
if(parts.length == 1){
throw new DukeException("Please adhere to convention:\n(Event /at event details)");
} else {
String eventDetails = parts[1];
System.out.println("Got it. I've added this Event:");
Event newEvent = new Event(eventDesc, eventDetails);
System.out.println(" " + newEvent.toString());
myList.add(newEvent);
System.out.println("Now you have " + myList.size() + " tasks in the list.");
database.writeTaskToFile(myList);
}
} catch(DukeException e) {
System.out.println(e.getMessage());
}
}

} catch (DukeException e){
System.out.println(e.getMessage());
}
System.out.println("____________________________________");
break;

case "list":
System.out.println("____________________________________");
System.out.println("Here are the tasks in your list:");
for(int i=1; i< myList.size()+1; i++){
Copy link

Choose a reason for hiding this comment

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

Note the spaces here as well:

Suggested change
for(int i=1; i< myList.size()+1; i++){
for (int i = 1; i < myList.size() + 1; i++) {

System.out.println(i +". " + myList.get(i-1).toString());
}
System.out.println("____________________________________");

break;

case "bye":
System.out.println("____________________________________");
System.out.println(" Bye. Hope to see you again soon!");
System.out.println("____________________________________");
System.exit(0);

break;

case "done":

System.out.println("____________________________________");
try{
String s1 = input.nextLine();
if(s1.equals("")){
throw new DukeException("Please specify what task is done");
} else {
String parts[] = s1.split(" ");
if(parts.length> 2){
throw new DukeException("Please insert valid index to mark as done");
} else {
try {
String indexString = parts[1];
int index =Integer.parseInt(indexString);
Task t = myList.get(index - 1);
t.markAsDone();
System.out.println("Nice! I've marked this task as done");
System.out.println(t.toString());
database.writeTaskToFile(myList);
} catch (Exception e){
System.out.println("Please enter a valid index");
}
}
}

} catch(DukeException e){
System.out.println(e.getMessage());
}
System.out.println("____________________________________");

break;

case "delete":
//
System.out.println("____________________________________");
try{
String s1 = input.nextLine();
if(s1.equals("")){
throw new DukeException("Please specify which task to delete");
} else {
String parts[] = s1.split(" ");
if(parts.length> 2){
throw new DukeException("Please insert valid index to delete");
} else {
try {
String indexString = parts[1];
int index =Integer.parseInt(indexString);
Task t = myList.get(index-1);
System.out.println("Noted i have removed this task");
System.out.println(myList.get(index-1));
myList.remove(index-1);
System.out.println("Now you have " + myList.size() + " tasks in the list");
database.writeTaskToFile(myList);

} catch (Exception e){
System.out.println("Please enter a valid index");
}
}
}

} catch(DukeException e){
System.out.println(e.getMessage());
}
System.out.println("____________________________________");


break;

default:
//clear the buffer
input.nextLine();
System.out.println("____________________________________");
System.out.println("Please insert a valid command");
System.out.println("____________________________________");

break;


}

}
}

private static ArrayList<Task> readInput(ArrayList<String> strings) {
ArrayList<Task> tasks = new ArrayList<>();
for(String str: strings) {
char identifier = str.charAt(1);
switch(identifier) {
case 'D':

Choose a reason for hiding this comment

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

Maybe it would be better to leave out the indentation for case clauses

String subString = str.substring(7);
String[] inputs = subString.split("by: ");
String name = inputs[0].substring(0, inputs[0].length()-2);
String deadline = inputs[1].substring(0, inputs[1].length()-1);
Deadline deadline1 = new Deadline(name, deadline);
tasks.add(deadline1);
break;
case 'T':
String subString1 = str.substring(7);
Todo todo1 = new Todo(subString1);
tasks.add(todo1);
break;
case 'E':
String subString2 = str.substring(7);
String[] inputs1 = subString2.split("at: ");
String desc = inputs1[0].substring(0, inputs1[0].length()-2);
String at = inputs1[1].substring(0, inputs1[1].length()-1);
Event event1 = new Event(desc, at);
tasks.add(event1);

break;
}
}
return tasks;
}


}
6 changes: 6 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class DukeException extends Exception {

public DukeException(String errorMessage) {
super(errorMessage);
}
}
16 changes: 16 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Event extends Task {

private String at;

public Event(String description, String at) {
super(description);
this.at = at;
}

@Override
public String toString(){
return "[E]" + super.toString() + " (at: " + this.at + ")";

}

}
22 changes: 22 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Task {
protected String description;
protected boolean isDone;

//constructor takes in a String that describes the task
public Task(String description) {
this.description = description;
this.isDone = false;
}

public String getStatusIcon() {
return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols
}

public void markAsDone() {
this.isDone = true;
}

public String toString() {
return "[" +this.getStatusIcon() + "] " + this.description;
}
}
11 changes: 11 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Todo extends Task {
public Todo(String description) {
super(description);
}

@Override
public String toString() {
return "[T]" + super.toString();
}

}
Loading