Skip to content

Commit 8098c55

Browse files
committed
Update code to meet coding standards
1 parent 61ae4cc commit 8098c55

File tree

7 files changed

+172
-29
lines changed

7 files changed

+172
-29
lines changed

src/main/java/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: burger.Burger
3+

src/main/java/burger/Burger.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package burger;
22

3+
import java.io.IOException;
34
import java.util.Scanner;
45

56
import burger.list.List;
67

8+
import static burger.BurgerFileClass.*;
9+
710
public class Burger {
811
static final String CHATBOT_NAME = "Burger";
912
static final String HORIZONTAL_LINE = "---------------------------------";
1013

11-
public static void main(String[] args) {
14+
public static void main(String[] args) throws IOException {
1215
welcomeMessage();
16+
List myList = getSaveFile();
1317
Scanner input = new Scanner(System.in);
14-
List myList = new List();
1518
boolean isPolling = true;
1619
while (isPolling) {
1720
String text = input.nextLine();
@@ -22,15 +25,16 @@ public static void main(String[] args) {
2225
isPolling = false;
2326
break;
2427
case "list":
28+
printLine();
2529
myList.printTaskList();
2630
break;
2731
default:
28-
wakeUp();
32+
printUnknownInputError();
2933
break;
3034
}
3135
}
3236
}
33-
goodbye();
37+
goodbye(myList);
3438
}
3539

3640
private static void welcomeMessage() {
@@ -47,13 +51,15 @@ public static void printLine() {
4751
/**
4852
* Prints error message when user provides unknown input
4953
*/
50-
private static void wakeUp() {
54+
private static void printUnknownInputError() {
5155
printLine();
5256
System.out.println("Wake Up!!! Key in something that makes sense!");
5357
printLine();
5458
}
5559

56-
public static void goodbye() {
60+
public static void goodbye(List currList) throws IOException {
61+
System.out.print("Saving file");
62+
setSaveFile(PATHNAME, currList);
5763
printLine();
5864
System.out.println("Bye. Hope to see you again soon!");
5965
printLine();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package burger;
2+
3+
import burger.list.List;
4+
5+
import java.io.File;
6+
import java.io.FileWriter;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.util.Scanner;
11+
12+
public class BurgerFileClass {
13+
static final int TDEINDEX = 0;
14+
static final int MARKINDEX = 1;
15+
16+
static final int TASKINDEX = 2;
17+
static final String PATHNAME = java.nio.file.Paths.get("data","burger.txt")
18+
.normalize().toString();
19+
20+
public static void readFromFile(String filePath, List list) throws IOException {
21+
Files.createDirectories(Paths.get("data"));
22+
File f = new File(filePath); // create a File for the given file path
23+
Scanner s = new Scanner(f); // create a Scanner using the File as the source
24+
while (s.hasNext()) {
25+
String[] currLineArray = s.nextLine().split("\\|");
26+
char currTDE = currLineArray[TDEINDEX].charAt(0);
27+
char currMark = currLineArray[MARKINDEX].charAt(0);
28+
String currTask = currLineArray[TASKINDEX];
29+
list.addFromSaveFile(currTDE, currMark, currTask);
30+
list.totalTasks++;
31+
}
32+
}
33+
34+
public static void setSaveFile(String filePath, List newList) {
35+
try {
36+
FileWriter fw = new FileWriter(filePath);
37+
int i = 0;
38+
String textToWrite;
39+
while (i < newList.totalTasks) {
40+
textToWrite = newList.getTask(i).getTDE() + "|" + newList.getTask(i).getTick() + "|" + newList.getTask(i).getName();
41+
fw.write(textToWrite + System.lineSeparator());
42+
System.out.print(".");
43+
i++;
44+
}
45+
fw.close();
46+
System.out.println();
47+
System.out.println("File successfully saved!");
48+
} catch (IOException e) {
49+
System.out.println();
50+
System.out.println("OH NO! File not saved!");
51+
}
52+
53+
}
54+
55+
public static List getSaveFile() {
56+
List newList = new List();
57+
try {
58+
readFromFile(PATHNAME, newList);
59+
System.out.println("File is found!");
60+
System.out.println("Current List: ");
61+
newList.printTaskList();
62+
} catch (IOException e) {
63+
System.out.println("File is not found! But we will create one for you!");
64+
}
65+
return newList;
66+
}
67+
}

src/main/java/burger/list/List.java

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
public class List {
1515

16-
private final ArrayList<Task> taskList;
16+
final int COMMANDIDX = 0;
17+
private ArrayList<Task> taskList;
1718

1819
public int totalTasks;
1920
public List() {
2021
taskList = new ArrayList<>();
2122
totalTasks = 0;
2223
}
24+
2325
/**
2426
* Returns boolean value base on whether the input matches one of the commands.
2527
*
@@ -30,14 +32,15 @@ public List() {
3032
*/
3133
public boolean isValidCommand(String[] textArray) {
3234
boolean isValid = true;
35+
int idx = 0;
3336
try {
34-
String command = textArray[0];
37+
String command = textArray[COMMANDIDX];
3538
switch (command) {
3639
case "mark":
3740
// fallthrough
3841
case "unmark":
39-
int idx = Integer.parseInt(textArray[1]) - 1;
40-
getMark(idx, command);
42+
idx = getIdx(textArray);
43+
markTask(idx, command);
4144
break;
4245
case "todo":
4346
addTodo(textArray);
@@ -48,16 +51,52 @@ public boolean isValidCommand(String[] textArray) {
4851
case "event":
4952
addEvent(textArray);
5053
break;
54+
case "delete":
55+
idx = getIdx(textArray);
56+
deleteTask(idx);
57+
break;
5158
default:
5259
isValid = false;
5360
break;
5461
}
5562
return isValid;
5663
} catch (ArrayIndexOutOfBoundsException e) {
5764
printEmptyDescription();
58-
return isValid;
65+
return false;
66+
}
67+
}
68+
69+
public void addFromSaveFile(char tde, char mark, String task) {
70+
Task currTask = new Task(task, tde);
71+
if (mark == 'X') {
72+
currTask.markDone();
5973
}
74+
taskList.add(currTask);
75+
}
76+
77+
private static int getIdx(String[] textArray) {
78+
StringBuilder idx = new StringBuilder();
79+
for (int i = 1; i < textArray.length; i++) {
80+
idx.append(textArray[i]);
81+
}
82+
return Integer.parseInt(idx.toString()) - 1;
83+
}
6084

85+
private void deleteTask(int idx) {
86+
try {
87+
if (idx < 0 || idx >= totalTasks) {
88+
throw new BurgerException();
89+
}
90+
System.out.println("Noted. I've removed this task:");
91+
taskList.get(idx).printTask();
92+
taskList.remove(idx);
93+
totalTasks--;
94+
System.out.println();
95+
printTotalTasks();
96+
printLine();
97+
} catch (BurgerException e) {
98+
printOutOfIndexMessage();
99+
}
61100
}
62101

63102
/**
@@ -75,6 +114,7 @@ public Task getTask(int idx) {
75114
return null;
76115
}
77116
}
117+
78118
/**
79119
* Adds a new todo to the list.
80120
*
@@ -99,20 +139,20 @@ public void addEvent(String[] event) {
99139
try {
100140
StringBuilder eventText = new StringBuilder(event[1]);
101141
int i = 2;
102-
while (!event[i].startsWith("/from")) {
142+
while (!event[i].startsWith("/from")) { // appends the name of event task
103143
eventText.append(' ').append(event[i]);
104144
i++;
105145
}
106146
eventText.append(' ').append("(from: ");
107147
i++;
108148
while (!event[i].startsWith("/to")) {
109-
eventText.append(event[i]).append(' ');
149+
eventText.append(event[i]).append(' '); // appends the "from" timing
110150
i++;
111151
}
112152
eventText.append("to: ");
113153
i++;
114154
while (i < event.length - 1) {
115-
eventText.append(event[i]).append(' ');
155+
eventText.append(event[i]).append(' '); // appends the "to" timing
116156
i++;
117157
}
118158
eventText.append(event[i]).append(')');
@@ -133,14 +173,14 @@ public void addDeadline(String[] deadline) {
133173
try {
134174
StringBuilder deadlineText = new StringBuilder(deadline[1]);
135175
int i = 2;
136-
while (!deadline[i].startsWith("/")) {
176+
while (!deadline[i].startsWith("/")) { // appends the name of deadline task
137177
deadlineText.append(' ').append(deadline[i]);
138178
i++;
139179
}
140180
deadlineText.append(' ').append("(by: ");
141181
i++;
142182
while (i < deadline.length - 1) {
143-
deadlineText.append(deadline[i]).append(' ');
183+
deadlineText.append(deadline[i]).append(' '); // appends the "by" timing
144184
i++;
145185
}
146186
deadlineText.append(deadline[i]).append(')');
@@ -163,17 +203,22 @@ public void addTask(Task task) {
163203
this.taskList.get(totalTasks).printTask();
164204
this.totalTasks++;
165205
System.out.println();
166-
System.out.println("Now you have " + totalTasks + " tasks in the list.");
206+
printTotalTasks();
167207
printLine();
168208
}
209+
210+
private void printTotalTasks() {
211+
System.out.println("Now you have " + totalTasks + " tasks in the list.");
212+
}
213+
169214
/**
170215
* Returns the mark from a task in the list.
171216
*
172217
* @param idx the index of the task.
173218
* @param command Command to "mark" or "unmark" the task.
174219
* @throws BurgerException if index input is out of the range of list of tasks.
175220
*/
176-
public void getMark(int idx, String command) {
221+
public void markTask(int idx, String command) {
177222
try {
178223
Task currTask = getTask(idx);
179224
if (currTask == null) {
@@ -208,12 +253,13 @@ public void printTaskWithLine(int idx) {
208253
* Prints task list.
209254
*/
210255
public void printTaskList() {
211-
printLine();
212-
System.out.println("Here are the tasks in your list:");
213256
if (taskList.isEmpty()) {
214257
System.out.println("Don't be lazy... start doing something");
258+
printLine();
259+
return;
215260
}
216-
for (int i = 0; i < taskList.size(); i++) {
261+
System.out.println("These are the tasks in your save file:");
262+
for (int i = 0; i < totalTasks; i++) {
217263
System.out.print(i+1 + ". ");
218264
taskList.get(i).printTask();
219265
System.out.println();

src/main/java/burger/task/Task.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ public Task(String task, char tde) {
1616
* Prints task in the required format [][] {Task Name}.
1717
*/
1818
public void printTask() {
19-
String tde = getTDE() ;
19+
char tde = getTDE() ;
2020
String tick = getTick();
2121
String name = getName();
22-
System.out.print(tde);
23-
System.out.print(tick);
22+
System.out.print("[" + tde + "]");
23+
System.out.print("[" + tick + "] ");
2424
System.out.print(name);
2525
}
2626

2727
public String getName() {
2828
return this.name;
2929
}
3030
public String getTick() {
31-
return this.hasDone ? "[X] " : "[ ] ";
31+
return this.hasDone ? "X" : " ";
3232
}
33-
public String getTDE() {
34-
return "[" + this.tde + "]";
33+
public char getTDE() {
34+
return this.tde;
3535
}
3636
public void markDone() {
3737
this.hasDone = true;

text-ui-test/EXPECTED.TXT

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Now you have 2 tasks in the list.
1818
---------------------------------
1919
---------------------------------
2020
Got it. I've added this task:
21-
[E][ ] project meeting (from: Mon 2pm to: 4pm)
21+
[E][ ] project meeting (from: Mon 2pm to: 4pm)
2222
Now you have 3 tasks in the list.
2323
---------------------------------
2424
---------------------------------
@@ -30,9 +30,26 @@ Now you have 4 tasks in the list.
3030
Here are the tasks in your list:
3131
1. [T][ ] borrow book
3232
2. [D][ ] return book (by: Sunday)
33-
3. [E][ ] project meeting (from: Mon 2pm to: 4pm)
33+
3. [E][ ] project meeting (from: Mon 2pm to: 4pm)
3434
4. [D][ ] do homework (by: no idea :-p)
3535
---------------------------------
3636
---------------------------------
37+
UwU~ Where is your description?
38+
Format: {command} {description}
39+
---------------------------------
40+
---------------------------------
41+
Wake Up!!! Key in something that makes sense!
42+
---------------------------------
43+
Noted. I've removed this task:
44+
[E][ ] project meeting (from: Mon 2pm to: 4pm)
45+
Now you have 3 tasks in the list.
46+
---------------------------------
47+
---------------------------------
48+
Here are the tasks in your list:
49+
1. [T][ ] borrow book
50+
2. [D][ ] return book (by: Sunday)
51+
3. [D][ ] do homework (by: no idea :-p)
52+
---------------------------------
53+
---------------------------------
3754
Bye. Hope to see you again soon!
3855
---------------------------------

text-ui-test/input.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ deadline return book /by Sunday
44
event project meeting /from Mon 2pm /to 4pm
55
deadline do homework /by no idea :-p
66
list
7+
todo
8+
blah
9+
delete 3
10+
list
711
bye

0 commit comments

Comments
 (0)