2015-11-13 84 views
-2

我有一個程序添加了一個活動,活動可以完成或不完整。除了切換任務的方法之外,我做了所有的事情。它應該檢查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); 
    } 
} 
+0

你做了什麼?你卡在哪裏?你有什麼問題?這裏有問題,還是這是工作要求?如果您遇到了特定的問題,我們可以幫助您,但我們不在這裏爲您做功課。 –

+0

你需要在你的請求中更加明確。你錯過了一個確切的問題,這只是一個代碼轉儲與你想要的。標記關閉,除非改進。 – basic

+0

在第一個代碼中有一個關於private void toggleTaskComplete()的問題,因爲我不知道如何將活動的狀態從完成改爲「不完整」。 – AnaF

回答

0
  1. 獲取由用戶指定的索引處的任務
  2. 從任務獲取當前任務狀態
  3. 將任務狀態設置爲與步驟2中找到的相反的內容

您的Task類具有上述步驟所需的功能。您可以使用public boolean getIsComplete()獲取當前任務狀態,然後使用public void setIsComplete(boolean isComplete)設置新的任務狀態。

我在一行中完成了所有操作,但如果它能讓您更容易理解,則可以將其分開。要做到這一點的一條線:

tasks.get(index).setIsComplete(!tasks.get(index).getIsComplete()); 

或者將它拆分出來:

Task taskAtIndex = tasks.get(index); 
    boolean taskAtIndexStatus = taskAtIndex.getIsComplete(); 
    taskAtIndex.setIsComplete(!taskAtIndexStatus); 
    tasks.add(index, taskAtIndex); 
+0

非常感謝!現在,我的outout的檢查是「索引:0人:hw(低)真」我有1個問題,我該怎麼做才能返回「完整」儘管「真」或「不完整」儘管「假」?非常感謝你!!! – AnaF

+0

@AnaF你可以使用三元運算符。例如,輸出以下消息:'System.out.println(taskAtIndex.getIsComplete()?「Task is complete」:「Task is not complete」);'。如果你想返回值,用return語句做同樣的事情。 '返回taskAtIndex.getIsComplete()? 「任務完成」:「任務不完整」。儘管你的'toggleTaskComplete'函數不返回任何東西(它的返回類型是'void'),所以如果你想返回一些東西,你需要改變它。 –