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
Changes from 4 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
77 changes: 77 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.Arrays;
import java.util.Scanner;

public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
Expand All @@ -6,5 +9,79 @@ 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)

String[] list = new String[100];
String line;
Scanner in = new Scanner(System.in);
line = in.nextLine();
int numOfItems = 0;
while(!line.equals("bye")) {
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 { // text from user
String item = (numOfItems + 1) + ".[ ] " + line; // convert to 1-based
list[numOfItems] = item;
++numOfItems;
}
line = in.nextLine(); // read in next line of text
}
}

public static void mark(String task, String[] list)
{
String locationOfTask = task.substring(5); // get the number of task to be marked
String taskToBeMarked = list[Integer.parseInt(locationOfTask) - 1];
String taskMarked = taskToBeMarked.replace("[ ]", "[X]");
list[Integer.parseInt(locationOfTask) - 1] = taskMarked;
System.out.println("Sir, your task has been marked as completed.");
}

public static void unmark(String task, String[] list)
{
String locationOfTask = task.substring(7);
String taskToBeUnmarked = list[Integer.parseInt(locationOfTask) - 1];
String taskUnmarked = taskToBeUnmarked.replace("[X]", "[ ]");
list[Integer.parseInt(locationOfTask) - 1] = taskUnmarked;
System.out.println("Sir, your task has been unmarked as requested.");
}

public static void printCurrentList(String[] list, int numOfItems)
{
String[] subList = Arrays.copyOf(list, numOfItems);
for(int i = 0; i < subList.length; ++i)
{
System.out.println(subList[i]);
}
}
}