我有一個程序添加了一個活動,活動可以完成或不完整。除了切換任務的方法之外,我做了所有的事情。它應該檢查ArrayList中是否存在任務(完成),向用戶詢問ArrayList中Task的索引以切換(完成)並將isCompleted更改爲不是(未完成)。我使用getIsComplete和另一個類的setIsComplete。請幫助我,如何做到這一點。切換arraylist中的任務
以下是我有:
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.InputMismatchException;
public class ToDoListManager {
private static final int ADD_TASK = 1;
private static final int TOGGLE_TASK_COMPLETE = 2;
private static final int REMOVE_TASK = 3;
private static final int VIEW_TASKS = 4;
private static final int EXIT = 5;
private List<Task> tasks;
private Scanner input;
public ToDoListManager(){
tasks = new ArrayList<Task>();
input = new Scanner(System.in);
}
public void runToDoList(){
int option = -1;
while(option != EXIT){
try{
showMenu();
option = input.nextInt();
input.nextLine();
switch(option){
case ADD_TASK:
addTask();
break;
//case TOGGLE_TASK_COMPLETE:
//toggleTaskComplete();
//break;
case REMOVE_TASK:
removeTask();
break;
case VIEW_TASKS:
viewTasks();
break;
case EXIT:
System.out.println("Program will exit");
break;
default:
System.out.println("Unrecognized command");
}
}
catch(InputMismatchException ex){
System.out.println("Please enter only numbers");
input.nextLine(); // clear out bad data from stream
}
}
}
private void showMenu(){
System.out.println(
String.format("%d Add task", ADD_TASK));
System.out.println(
String.format("%d Toggle task", TOGGLE_TASK_COMPLETE));
System.out.println(
String.format("%d Remove task", REMOVE_TASK));
System.out.println(
String.format("%d View tasks", VIEW_TASKS));
System.out.println(
String.format("%d Exit program", EXIT));
}
private void addTask(){
try{
System.out.println("Please enter task title\n (Title cannon be empty, max 25 characters) ");
String title = input.nextLine();
System.out.println("Please enter task priority (high, medium, low) ");
String priority = input.nextLine();
tasks.add(new Task(title, priority));
}
catch(ValidationException ex){
System.out.println(ex.getMessage());
}
}
//problem here
private void toggleTaskComplete(){
try{
if(tasks.size() <= 0){
System.out.println("Nothing to toggle, tasks list is empty");
}
else{
System.out.println("Enter index of task to toggle");
int index = input.nextInt();
input.nextLine();
Task task1 = new Task();
task1.getIsComplete();
(getIsComplete)tasks.toggle(index);
}
}
catch(InputMismatchException ex){
System.out.println("Please enter only numbers");
}
catch(IndexOutOfBoundsException ex){
System.out.println("Invalid index number");
}
}
private void removeTask(){
try{
if(tasks.size() <= 0){
System.out.println("Nothing to remove, tasks list is empty");
}
else{
System.out.println("Enter index of task to remove");
int index = input.nextInt();
input.nextLine();
tasks.remove(index);
}
}
catch(InputMismatchException ex){
System.out.println("Please enter only numbers");
}
catch(IndexOutOfBoundsException ex){
System.out.println("Invalid index number");
}
}
private void viewTasks(){
if(tasks.size() > 0){
for(int index = 0; index < tasks.size(); index++){
System.out.println(
String.format("Index: %d Person: %s",
index, tasks.get(index)));
}
}
else{
System.out.println("No tasks to display");
}
}
}
類任務:
public class Task {
private String title;
private String priority;
private boolean isComplete;
public Task(){
title = "No title";
priority = "No priority";
}
public Task(String title, String priority) throws ValidationException{
setTitle(title);
setPriority(priority);
}
public String getTitle(){
return title;
}
public void setTitle(String title)throws ValidationException{
if(title == null || title.trim().length() == 0){
throw new ValidationException(
String.format("There was a problem adding a task:\n Title cannot be empty\n Please try again"));
}
else if(title.length() > 25){
throw new ValidationException(
String.format("There was a problem adding a task:\n Title cannot exceed 25 characters\n Please try again"));
}
this.title = title.trim();
}
public String getPriority(){
return priority;
}
// things to do here
public void setPriority(String priority)throws ValidationException{
if(priority.equals("high") || priority.equals("low") || priority.equals("medium")){
this.priority = priority.trim();
}
else if(priority == null || priority.trim().length() == 0){
throw new ValidationException(
String.format("There was a problem adding a task:\n Priority cannot be empty\n Please try again"));
}
else
throw new ValidationException(
String.format("There was a problem adding a task:\n Priority must be high, medium or low\n Please try again"));
}
public boolean getIsComplete(){
return isComplete;
}
public void setIsComplete(boolean isComplete){
this.isComplete = isComplete;
}
public String toString(){
return String.format("%s (%s) %s", title, priority, isComplete);
}
}
你做了什麼?你卡在哪裏?你有什麼問題?這裏有問題,還是這是工作要求?如果您遇到了特定的問題,我們可以幫助您,但我們不在這裏爲您做功課。 –
你需要在你的請求中更加明確。你錯過了一個確切的問題,這只是一個代碼轉儲與你想要的。標記關閉,除非改進。 – basic
在第一個代碼中有一個關於private void toggleTaskComplete()的問題,因爲我不知道如何將活動的狀態從完成改爲「不完整」。 – AnaF