Toggle 输出中的完整和日期问题
Toggle Complete and date problems in output
我有两个 classes,我的程序在其中应该像任务管理器一样工作。我可以添加项目、删除项目、设置每个项目的优先级、到期时、将其切换为完成或不完成,并在方框“[]”/“[x]”中使用 "x" 标记,并且我可以打印我所有的项目。我 运行 我的程序,但每次我添加一个设置了截止日期的项目时,它会将我项目中的所有日期变成我输入的最后一个截止日期。此外,如果我的项目已经有一个 "x" 标记,并且我将其切换为不完整,我应该能够取消标记 "x" 从“[x]”到“[]”但是它不那样做。我做错了什么?
这是我的输出:
----jGRASP exec: java MyList
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Run
Enter Date (MM/dd/YYYY): 11/27/1993
Enter priority (Low/Medium/High): high
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/27/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Jump
Enter Date (MM/dd/YYYY): 11/28/1889
Enter priority (Low/Medium/High): medium
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/28/1889)
1. [ ] Jump -2- (11/28/1889)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Walk
Enter Date (MM/dd/YYYY): 11/19/1993
Enter priority (Low/Medium/High): low
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/19/1993)
1. [ ] Jump -2- (11/19/1993)
2. [ ] Walk -3- (11/19/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Jog
Enter Date (MM/dd/YYYY): 11/23/1993
Enter priority (Low/Medium/High): medium
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
t
Enter index of item to toggle complete: 0
0. [X] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
t
Enter index of item to toggle complete: 0
0. [X] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
q
----jGRASP: operation complete.
以下是我对每个 class 的代码:
我的列表class:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
import java.text.*;
import java.util.Collections;
public class MyList {
public static ArrayList<ToDoItem> toDoItems = new ArrayList<>();
private static Scanner k = new Scanner(System.in);
public static void main(String[] args) throws ParseException {
while(true) {
printMenu();
processInput();
}
}
public static void printMenu() {
System.out.println("[a]dd an item");
System.out.println("[d]elete an item");
System.out.println("[t]oggle complete");
System.out.println("[p]rint all");
System.out.println("[q]uit");
}
private static void processInput() throws ParseException {
Scanner s = new Scanner(System.in);
String input = s.next();
if(input.equals("a")) {
addToDoItem();
}
else if(input.equals("d")) {
deleteToDoItem();
}
else if(input.equals("t")) {
toggleComplete();
}
else if(input.equals("p")) {
printAll();
}
else if(input.equals("q")) {
System.exit(0);
}
}
private static void addToDoItem() throws ParseException {
System.out.print("Enter an item to add to list: ");
String desc = k.nextLine();
System.out.print("Enter Date (MM/dd/YYYY): ");
String dueDate = k.nextLine();
ToDoItem.setDueDate(dueDate);
System.out.print("Enter priority (Low/Medium/High): ");
String prior = k.nextLine();
toDoItems.add(new ToDoItem(desc, prior, dueDate));
}
public static void printAll() {
Collections.sort(toDoItems, new Comparator<ToDoItem>() {
@Override
public int compare(ToDoItem o1, ToDoItem o2) {
return o1.getPriority().getValue() - o2.getPriority().getValue();
}
});
for (int index = 0; index < toDoItems.size(); index++)
System.out.println(index + ". [ ] " + toDoItems.get(index));
}
public static void deleteToDoItem() {
int index = 0;
System.out.print("Enter index of item to delete: ");
int delete = k.nextInt();
toDoItems.remove(index);
}
public static void toggleComplete() {
System.out.print("Enter index of item to toggle complete: ");
int toggle = k.nextInt();
for (int index = 0; index < toDoItems.size(); index++) {
if(toggle == index) {
System.out.println(index + ". [X] " + toDoItems.get(index));
}
else {
System.out.println(index + ". [ ] " + toDoItems.get(index));
}
}
}
}
待办事项class:
import java.text.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class ToDoItem {
private String description;
private static Date dueDate;
private Priority priority;
private static DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
public ToDoItem() {
}
public ToDoItem(String desc) {
description = desc;
dueDate = null;
priority = priority.HIGH;
}
public ToDoItem(String desccription, String d) throws ParseException{
this.description = description;
dueDate = df.parse(d);
}
public ToDoItem(String description, String p, String d) throws ParseException{
this.description = description;
this.priority = Priority.valueOf(p.toUpperCase());
dueDate = df.parse(d);
}
public String toString() {
return description + " -"+priority.getValue()+"- (" + df.format(dueDate) + ")";
}
public static void setDueDate(String s) {
try {
dueDate = df.parse(s);
} catch(Exception ex) {
System.out.println(ex);
}
}
public String getDescription() {
return description;
}
public String getDueDate() {
return df.format(dueDate);
}
public Priority getPriority() {
return priority;
}
}
enum Priority {
HIGH(1), MEDIUM(2), LOW(3);
private int value;
Priority(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
要设置截止日期,请从 private static Date dueDate;
中删除 static
。 static
这样一来,您正在创建的对象只有一个共享实例,这意味着当您更改一次它时,每隔一次调用它也会反映该更改。
至于取消切换您的索引,在它进入条件 toggle == index
后,您不会检查它是否已经切换。因此,如果您输入 t
,然后输入 1
作为 toggle/un-toggle 的索引,只有该项目会被切换,其余的将为空。
因为你有一个 toDoItems 作为一个 class 私有变量,我建议在 ToDoItem
class 中添加 isToggled
布尔变量,这样你就可以检查是否该特定项目是否已切换。
所以在 ToDoItem
class:
private String description;
private Date dueDate;
private Priority priority;
private boolean isToggled;
....
public ToDoItem(String desc) {
description = desc;
dueDate = null;
priority = priority.HIGH;
isToggled = false;
}
....
public void setToggle(boolean toggled) {
isToggled = toggled;
}
public boolean isToggled() {
return isToggled;
}
并且在 MyList
class:
public static void toggleComplete() {
System.out.print("Enter index of item to toggle complete: ");
int toggle = k.nextInt();
toDoItems.get(toggle).setToggle(!toDoItems.get(toggle).isToggled());
// No need for a for-loop since `ArrayList` has the `get` method to do this for you
//for (int index = 0; index < toDoItems.size(); index++) {
// if(toggle == index) {
// System.out.println(index + ". [X] " + toDoItems.get(index));
// }
// else {
// System.out.println(index + ". [ ] " + toDoItems.get(index));
// }
//}
}
我有两个 classes,我的程序在其中应该像任务管理器一样工作。我可以添加项目、删除项目、设置每个项目的优先级、到期时、将其切换为完成或不完成,并在方框“[]”/“[x]”中使用 "x" 标记,并且我可以打印我所有的项目。我 运行 我的程序,但每次我添加一个设置了截止日期的项目时,它会将我项目中的所有日期变成我输入的最后一个截止日期。此外,如果我的项目已经有一个 "x" 标记,并且我将其切换为不完整,我应该能够取消标记 "x" 从“[x]”到“[]”但是它不那样做。我做错了什么?
这是我的输出:
----jGRASP exec: java MyList
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Run
Enter Date (MM/dd/YYYY): 11/27/1993
Enter priority (Low/Medium/High): high
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/27/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Jump
Enter Date (MM/dd/YYYY): 11/28/1889
Enter priority (Low/Medium/High): medium
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/28/1889)
1. [ ] Jump -2- (11/28/1889)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Walk
Enter Date (MM/dd/YYYY): 11/19/1993
Enter priority (Low/Medium/High): low
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/19/1993)
1. [ ] Jump -2- (11/19/1993)
2. [ ] Walk -3- (11/19/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Jog
Enter Date (MM/dd/YYYY): 11/23/1993
Enter priority (Low/Medium/High): medium
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
t
Enter index of item to toggle complete: 0
0. [X] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
t
Enter index of item to toggle complete: 0
0. [X] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
q
----jGRASP: operation complete.
以下是我对每个 class 的代码:
我的列表class:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
import java.text.*;
import java.util.Collections;
public class MyList {
public static ArrayList<ToDoItem> toDoItems = new ArrayList<>();
private static Scanner k = new Scanner(System.in);
public static void main(String[] args) throws ParseException {
while(true) {
printMenu();
processInput();
}
}
public static void printMenu() {
System.out.println("[a]dd an item");
System.out.println("[d]elete an item");
System.out.println("[t]oggle complete");
System.out.println("[p]rint all");
System.out.println("[q]uit");
}
private static void processInput() throws ParseException {
Scanner s = new Scanner(System.in);
String input = s.next();
if(input.equals("a")) {
addToDoItem();
}
else if(input.equals("d")) {
deleteToDoItem();
}
else if(input.equals("t")) {
toggleComplete();
}
else if(input.equals("p")) {
printAll();
}
else if(input.equals("q")) {
System.exit(0);
}
}
private static void addToDoItem() throws ParseException {
System.out.print("Enter an item to add to list: ");
String desc = k.nextLine();
System.out.print("Enter Date (MM/dd/YYYY): ");
String dueDate = k.nextLine();
ToDoItem.setDueDate(dueDate);
System.out.print("Enter priority (Low/Medium/High): ");
String prior = k.nextLine();
toDoItems.add(new ToDoItem(desc, prior, dueDate));
}
public static void printAll() {
Collections.sort(toDoItems, new Comparator<ToDoItem>() {
@Override
public int compare(ToDoItem o1, ToDoItem o2) {
return o1.getPriority().getValue() - o2.getPriority().getValue();
}
});
for (int index = 0; index < toDoItems.size(); index++)
System.out.println(index + ". [ ] " + toDoItems.get(index));
}
public static void deleteToDoItem() {
int index = 0;
System.out.print("Enter index of item to delete: ");
int delete = k.nextInt();
toDoItems.remove(index);
}
public static void toggleComplete() {
System.out.print("Enter index of item to toggle complete: ");
int toggle = k.nextInt();
for (int index = 0; index < toDoItems.size(); index++) {
if(toggle == index) {
System.out.println(index + ". [X] " + toDoItems.get(index));
}
else {
System.out.println(index + ". [ ] " + toDoItems.get(index));
}
}
}
}
待办事项class:
import java.text.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class ToDoItem {
private String description;
private static Date dueDate;
private Priority priority;
private static DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
public ToDoItem() {
}
public ToDoItem(String desc) {
description = desc;
dueDate = null;
priority = priority.HIGH;
}
public ToDoItem(String desccription, String d) throws ParseException{
this.description = description;
dueDate = df.parse(d);
}
public ToDoItem(String description, String p, String d) throws ParseException{
this.description = description;
this.priority = Priority.valueOf(p.toUpperCase());
dueDate = df.parse(d);
}
public String toString() {
return description + " -"+priority.getValue()+"- (" + df.format(dueDate) + ")";
}
public static void setDueDate(String s) {
try {
dueDate = df.parse(s);
} catch(Exception ex) {
System.out.println(ex);
}
}
public String getDescription() {
return description;
}
public String getDueDate() {
return df.format(dueDate);
}
public Priority getPriority() {
return priority;
}
}
enum Priority {
HIGH(1), MEDIUM(2), LOW(3);
private int value;
Priority(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
要设置截止日期,请从 private static Date dueDate;
中删除 static
。 static
这样一来,您正在创建的对象只有一个共享实例,这意味着当您更改一次它时,每隔一次调用它也会反映该更改。
至于取消切换您的索引,在它进入条件 toggle == index
后,您不会检查它是否已经切换。因此,如果您输入 t
,然后输入 1
作为 toggle/un-toggle 的索引,只有该项目会被切换,其余的将为空。
因为你有一个 toDoItems 作为一个 class 私有变量,我建议在 ToDoItem
class 中添加 isToggled
布尔变量,这样你就可以检查是否该特定项目是否已切换。
所以在 ToDoItem
class:
private String description;
private Date dueDate;
private Priority priority;
private boolean isToggled;
....
public ToDoItem(String desc) {
description = desc;
dueDate = null;
priority = priority.HIGH;
isToggled = false;
}
....
public void setToggle(boolean toggled) {
isToggled = toggled;
}
public boolean isToggled() {
return isToggled;
}
并且在 MyList
class:
public static void toggleComplete() {
System.out.print("Enter index of item to toggle complete: ");
int toggle = k.nextInt();
toDoItems.get(toggle).setToggle(!toDoItems.get(toggle).isToggled());
// No need for a for-loop since `ArrayList` has the `get` method to do this for you
//for (int index = 0; index < toDoItems.size(); index++) {
// if(toggle == index) {
// System.out.println(index + ". [X] " + toDoItems.get(index));
// }
// else {
// System.out.println(index + ". [ ] " + toDoItems.get(index));
// }
//}
}