-
Notifications
You must be signed in to change notification settings - Fork 117
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
[Zhitong] ip #110
base: master
Are you sure you want to change the base?
[Zhitong] ip #110
Changes from all commits
e5b61e1
a99766a
f17a40f
18ea098
c9f4e1c
10b1dd8
73edcfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
|
||
public class Commands { | ||
|
||
private static final int MAX_LENGTH = 100; | ||
private Task[] tasks = new Task[MAX_LENGTH]; | ||
|
||
public int length = 0; | ||
|
||
private boolean isExited = false; | ||
|
||
public Commands() { | ||
|
||
} | ||
|
||
public void accept(String nextLine) { | ||
|
||
int index = (nextLine.indexOf(' ')); | ||
String commandKeyword = (index == -1)? nextLine : nextLine.substring(0, index).trim(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be a space before the "?" ternary operator? |
||
|
||
final String commandText = (index == -1)? nextLine : nextLine.substring(index).trim(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you extract this magic number? |
||
switch (commandKeyword) { | ||
case "exit" -> this.isExited = true; | ||
case "list" -> this.list(); | ||
case "add" -> { | ||
this.tasks[length] = new Task(nextLine.substring(index)); | ||
this.echo(); | ||
length++; | ||
} | ||
case "mark" -> { | ||
this.tasks[Integer.parseInt(commandText)].setDone(true); | ||
echo(Integer.parseInt(commandText)); | ||
} | ||
case "unmark" -> { | ||
this.tasks[Integer.parseInt(commandText)].setDone(false); | ||
echo(Integer.parseInt(commandText)); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
private void list() { | ||
for (int i = 0; i < length; i++) { | ||
System.out.println( i +": "+ tasks[i]); | ||
} | ||
} | ||
|
||
private void echo() { | ||
System.out.println("added: " + tasks[length]); | ||
} | ||
private void echo( int index) { | ||
System.out.println(tasks[index]); | ||
} | ||
|
||
public boolean isAcceptingCommands () { | ||
return !isExited; | ||
} | ||
|
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
|
||
import java.util.Scanner; | ||
|
||
public class MrStripes { | ||
public static void main(String[] args) { | ||
|
||
System.out.println(Texts.GREETINGS); | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
Commands commands = new Commands(); | ||
while (commands.isAcceptingCommands()) { | ||
commands.accept(scanner.nextLine()); | ||
} | ||
|
||
System.out.println(Texts.GOODBYE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public class Task { | ||
private String line; | ||
private boolean isDone; | ||
|
||
public String getLine() { | ||
return line; | ||
} | ||
|
||
public void setLine(String line) { | ||
this.line = line; | ||
} | ||
|
||
public boolean isDone() { | ||
return isDone; | ||
} | ||
|
||
public void setDone(boolean done) { | ||
isDone = done; | ||
} | ||
|
||
public Task(String line, boolean isDone) { | ||
this.line = line; | ||
this.isDone = isDone; | ||
} | ||
|
||
public Task(String line) { | ||
this.line = line; | ||
this.isDone = false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + (isDone? "X" : " ") + "]" | ||
+ line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be on the same line (line limit 110 characters)? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
public class Texts { | ||
public static final String GREETINGS = " hello"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like how you use all caps for constants. |
||
public static final String GOODBYE = "goodbye"; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the private attributes be grouped together?