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

[SanyaS] iP #328

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke project template
# Sunpter project template

This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it.

Expand All @@ -13,7 +13,7 @@ Prerequisites: JDK 17, update Intellij to the most recent version.
1. If there are any further prompts, accept the defaults.
1. Configure the project to use **JDK 17** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).<br>
In the same dialog, set the **Project language level** field to the `SDK default` option.
1. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
1. After that, locate the `src/main/java/Sunpter.java` file, right-click it, and choose `Run Sunpter.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
```
Hello from
____ _
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke User Guide
# Sunpter User Guide

// Update the title above to match the actual product name

Expand Down
72 changes: 72 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//add support for an unknown command
public enum Command {
LIST, TODO, MARK, UNMARK, DEADLINE, EVENT, DELETE, BYE, EMPTY;
public static final Roster roster = new Roster();

public void inputCommand(String input) {
String[] inputParts = input.split("\\s+",2);
System.out.println(inputParts[0]);
String commandInput = inputParts[0].toUpperCase();
Command command = Command.valueOf(commandInput);

switch (command) {
case DELETE:
int number = Integer.parseInt(inputParts[1]);
Task task = roster.getTask(number);
roster.removeTask(number);
String removedTask = "Noted. I've removed this task:\n" +
"\n" + task.toString() + "\n" +
"Now you have " + roster.numberofTasks() + " tasks in the list.";
System.out.println(Sunpter.encapsulateInLines(removedTask));
break;
case BYE:
break;
case UNMARK:
int numberThree = Integer.parseInt(inputParts[1].trim());
roster.markTaskAsUncompleted(numberThree);
System.out.println(Sunpter.encapsulateInLines(Sunpter.taskUnDone + "\n" + roster.getTask(numberThree).toString()));
break;
case MARK:
int numberTwo = Integer.parseInt(inputParts[1].trim());
roster.markTaskAsCompleted(numberTwo);
System.out.println(Sunpter.encapsulateInLines(Sunpter.taskDone + "\n" + roster.getTask(numberTwo).toString()));
break;
case EVENT:
String[] eventParts = inputParts[1].split("/from|/to");
String event = eventParts[0]; String start = eventParts[1].trim();String end = eventParts[2].trim();
Task newEvent = new Event(event, start, end);
roster.addTask(newEvent);
String eventAdded = "Got it. I've added this task:" +
"\n" + newEvent.toString() + "\n" +
"Now you have " + roster.numberofTasks() + " tasks in the list.";
System.out.println(Sunpter.encapsulateInLines(eventAdded));
break;
case DEADLINE:
String[] deadlineParts = inputParts[1].split("/by");
String taskName = deadlineParts[0].trim(); String deadline = deadlineParts[1].trim();
Task newDeadline = new Deadline(taskName, deadline);
roster.addTask(newDeadline);
String deadlineAdded = "Got it. I've added this task:" +
"\n" + newDeadline.toString() + "\n" +
"Now you have " + roster.numberofTasks() + " tasks in the list.";
System.out.println(Sunpter.encapsulateInLines(deadlineAdded));
break;

case TODO:
if(inputParts[1].isEmpty()) {
throw new SunpterException("Empty command");
}
Task newtodo = new ToDo(inputParts[1].trim());
roster.addTask(newtodo);
String todoAdded = "Got it. I've added this task:" +
"\n" + newtodo.toString() + "\n" +
"Now you have " + roster.numberofTasks() + " tasks in the list.";
System.out.println(Sunpter.encapsulateInLines(todoAdded));
break;
case LIST:
System.out.println(Sunpter.encapsulateInLines(roster.printRoster()));
break;
default:
}
}
}
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 @@
//remove local date time for now since there is no consistent format
public class Deadline extends Task{
String deadline;

public Deadline(String name, String deadline) {
super(name);
this.deadline = deadline;
}

@Override
public String toString() {
return "[D]" + getStatusIcon() + " " + getName() + "(by: " + deadline +")";
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

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 start;
private String end;

public Event(String name, String start, String end) {
super(name);
this.start = start;
this.end = end;
}

@Override
public String toString() {
return "[E]" + getStatusIcon() + " " + getName() + "(from: " + start + " to: "+end +")";
}
}
50 changes: 50 additions & 0 deletions src/main/java/Roster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
This class helps keep encapsulate and update a task list.
*/
import java.sql.Array;
import java.util.ArrayList;
public class Roster {
protected ArrayList<Task> rosterList = new ArrayList<>(); //just use array for now

Roster() {}

//roster length
public int numberofTasks() {
return rosterList.size();
}

//read from the roster
public String printRoster() {
StringBuilder sb = new StringBuilder("Here are the tasks in your roster:\n");
int taskIndex = 1;
for (Task task : rosterList) {
sb.append("\n" + taskIndex + ". " + task.toString());
taskIndex++;
}
return sb.toString();
}
//add a task to the roster
public void addTask(Task task) {
rosterList.add(task);
}
//delete a task from the roster
public void removeTask(int index) {
rosterList.remove(index - 1);
}
//mark a task
public void markTaskAsCompleted(int num) {
Task completedTask = getTask(num);
completedTask.setCompleted();
}

//unmark a task
public void markTaskAsUncompleted(int num) {
Task completedTask = getTask(num);
completedTask.setNotCompleted();
}

//get the task
public Task getTask(int num) {
return rosterList.get(num - 1);
}
}
48 changes: 48 additions & 0 deletions src/main/java/Sunpter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.Scanner;

public class Sunpter {
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";

System.out.println(encapsulateInLines(GREETING));
Scanner scanner = new Scanner(System.in);
Command commandEnum = Command.EMPTY;
String input = "";
while (true) {
try {
input = scanner.nextLine();
if (input == null || input.trim().isEmpty()) {
throw new SunpterException("Empty input. Please enter a valid command.");
}
String[] inputParts = input.split(" ", 2);
String commandInput = inputParts[0].toUpperCase();
try {
commandEnum = Command.valueOf(commandInput);
} catch (IllegalArgumentException e) {
System.out.println("Invalid command. Please enter LIST, TODO, MARK, UNMARK, DEADLINE, EVENT, DELETE, or BYE.");
continue;
}
if (commandEnum == Command.BYE) {
break;
}
commandEnum.inputCommand(input);
} catch (SunpterException e) {
System.out.println(e.getMessage());
}
}
scanner.close();
System.out.println(encapsulateInLines(FIN));
}
public static String encapsulateInLines(String str) {
String horizontalLine = "____________________________________________________________";
return horizontalLine + "\n" + str + "\n" + horizontalLine;
}
static String GREETING = "Bonjour, je m'appelle Sunpter\n" + "Que puis-je faire pour vous?";
static String FIN = "Alors, je suis fatigué. Tu parles trop! À bientôt.";
static String taskDone = "Incroyable! Alors cette tâche est finie.";
static String taskUnDone = "Alors, cette tâche n'est pas terminée. Essaie plus fort!";
}
5 changes: 5 additions & 0 deletions src/main/java/SunpterException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class SunpterException extends IllegalArgumentException{
public SunpterException(String message) {
super(message);
}
}
35 changes: 35 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class Task {

public enum Status{
FINISHED, UNFINISHED;
}

protected String name;
protected Status status;

public Task(String name) {
this.name = name;
this.status = Status.UNFINISHED;
}

public String getStatusIcon() {
return status == status.FINISHED ? "[X]" : "[ ]";
}

public void setCompleted() {
this.status = status.FINISHED;
}

public void setNotCompleted() {
this.status = status.UNFINISHED;
}

public String getName() {
return name;
}

@Override
public String toString() {
return getStatusIcon() + " " + getName();
}
}
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 name) {
super(name);
}

@Override
public String toString() {
return "[T]" + getStatusIcon() + " " + getName();
}
}
40 changes: 34 additions & 6 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
____________________________________________________________
Bonjour, je m'apelle Sunpter
Que puis-je faire pour vous?
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[T][ ] borrow book
Now you have 1 tasks in the list.
____________________________________________________________
____________________________________________________________
Here are the tasks in your roster:

1. [T][ ] borrow book
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[D][ ] return book (by: Sunday)
Now you have 2 tasks in the list.
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[E][ ] project meeting (from: Mon 2pm to: 4pm)
Now you have 3 tasks in the list.
____________________________________________________________
____________________________________________________________
Nice! I've marked this task as done:
[D][X] return book (by: Sunday)
____________________________________________________________
____________________________________________________________
OK, I've marked this task as not done yet:
[D][ ] return book (by: Sunday)
____________________________________________________________
____________________________________________________________
Alors, je suis fatigué. Tu parles trop! À bientôt.
____________________________________________________________
7 changes: 7 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
todo borrow book
liste
deadline return book /by Sunday
event project meeting /from Mon 2pm /to 4pm
mark 2
unmark 2
Bye
2 changes: 1 addition & 1 deletion text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 (
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
java -classpath ..\bin Sunpter < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT
2 changes: 1 addition & 1 deletion text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ then
fi

# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ../bin Duke < input.txt > ACTUAL.TXT
java -classpath ../bin Sunpter < input.txt > ACTUAL.TXT

# convert to UNIX format
cp EXPECTED.TXT EXPECTED-UNIX.TXT
Expand Down