Methode addTask und removeTask

Euler123

Mitglied
Die Klasse TaskList erweitert die Klasse Task und beinhaltet folgende Methoden:
  • public TaskList(String title, int capacity)
  • public void lock()
  • public void unlock()
  • public boolean isLocked()
  • public int getCount()
  • public boolean addTask(Task task)
  • public Task removeTask(String title)
Mein momentaner Code:
Java:
public class TaskList extends Task {

    final Task[] tasks; // NOT private!
    private int count;
    private boolean locked;

    public TaskList(String title, int capacity) {
        super(title);
        tasks = new Task[Math.max(0, capacity)];
        count = 0;
        locked = false;
    }
    
    /**
     * Locks the list against additions and removal from tasks.
     *
     * After this method is called and until the unlock method
     * is called, no task additions or removals may succeed
     */
    public void lock() {
        locked = true;
    }

    /**
     * Unlocks the list. Task additions and removals may succeed
     * until lock() is called.
     */
    public void unlock() {
        locked = false;
    }

    /**
     * @return true if list is locked, false otherwise
     */
    public boolean isLocked() {
        return locked;
    }

    public int getCount() {
        int actualCount = 0;
        for (Task task : tasks) {
            if (task != null) {
                actualCount++;
                if (task instanceof TaskList) {
                    actualCount += ((TaskList) task).getCount(); // Zählt auch die in verschachtelten Listen enthaltenen Aufgaben
                }
            }
        }
        return actualCount;
    }

   /**
     * Same as in A09 (and template code), plus:
     *
     * @param task to be added
     * @return false and does not add task if:
     * - this list is locked or if parameter task is the same as this list
     * - another task with same title exists in the task array of this list
     * The operation succeeds if another task with same title exists in a nested list (but not in this list).
     * If parameter task refers to a task list and it is added successfully, then that list becomes locked.
     */
    public boolean addTask(Task task) {
        if (!locked && task != null && task.getTitle() != null && !this.equals(task) && !contains(task.getTitle())) {
        for (int i = 0; i < tasks.length; i++) {
            if (tasks[i] == null) {
                tasks[i] = task;
                if (task instanceof TaskList) {
                    ((TaskList) task).lock();
                    count += 1 + ((TaskList) task).getCount(); // Aktualisiere count nur, wenn es sich um eine TaskList handelt
                } else {
                    count++;
                }
                return true;
            }
        }
    }
    return false;
    }

    /**
     * Same as in A09 (and template code), plus:
     *
     * @param title
     * @return null and does not remove if list is locked
     * If the removed tasks is a list, then that list becomes unlocked
     */
    public Task removeTask(String title) {
        if (!locked) {
        for (int i = 0; i < tasks.length; i++) {
            Task t = tasks[i];
            if (t != null && t.getTitle().equals(title)) {
                tasks[i] = null;
                count--;
                if (t instanceof TaskList) {
                    ((TaskList) t).unlock();
                    count -= ((TaskList) t).getCount(); // Aktualisiere count für die entfernte TaskList
                }
                return t;
            }
        }
    }
    return null;
    }
        
    /** Helper */
    private boolean contains(String title) {
        for (Task t : tasks) {
            if (t != null && title.equals(t.getTitle()))
                return true;
        }
        return false;
    }

Dabei bekomme ich folgenden Fehler:
1705862498774.png

Hier wird also die Anzahl der Aufgaben nicht richtig aktualisiert?? - Mir ist leider noch nicht ganz klar, was ich konkret falsch mache bzw. wie ich dies beheben kann??

Danke für Hilfe!
LG Euler
 

Marinek

Bekanntes Mitglied
Wie lauten denn die Anforderungen für die Methode getCount()?

Es liegt nahe, dass die Tasks in den Sublisten nicht gezählt werden sollen.
 

Marinek

Bekanntes Mitglied
Java:
public int getCount() {
        int actualCount = 0;
        for (Task task : tasks) {
            if (task != null) {
                if (task instanceof TaskList) {
                    actualCount += ((TaskList) task).getCount(); // Zählt auch die in verschachtelten Listen enthaltenen Aufgaben
                } else {
                    actualCount++;    // Wenn es eine TaskList ist, dann soll nicht gezählt werden!
                }
            }
        }
        return actualCount;
    }

Liegt daran, dass du die TaskList Objekte auch zählst. Sollst du aber nicht.
 

Euler123

Mitglied
Java:
public int getCount() {
        int actualCount = 0;
        for (Task task : tasks) {
            if (task != null) {
                if (task instanceof TaskList) {
                    actualCount += ((TaskList) task).getCount(); // Zählt auch die in verschachtelten Listen enthaltenen Aufgaben
                } else {
                    actualCount++;    // Wenn es eine TaskList ist, dann soll nicht gezählt werden!
                }
            }
        }
        return actualCount;
    }

Liegt daran, dass du die TaskList Objekte auch zählst. Sollst du aber nicht.
Das hat mich schon mal weiter gebracht, danke dir!

Java:
public int getCount() {
    int actualCount = 0;
    for (Task task : tasks) {
        if (task != null && !(task instanceof TaskList && ((TaskList) task).isLocked())) {
            actualCount++;
            if (task instanceof TaskList) {
                actualCount += ((TaskList) task).getCount();
            }
        }
    }
    return actualCount;
    }


Jetzt lande ich bei diesem Fehler:
1705868563348.png

Hierbei liegt jetzt noch der Fehler in der addTask bzw. removeTask Methode??? (aber was sagt mir das genau)???
 

Marinek

Bekanntes Mitglied
Das bedeutet, dass wenn die Methode addTask den Wert "true" zurück gibt, dass sich der Wert anschließend von getCount() ändern sollte.

Was logisch ist, denn true von add gibt vermutlich an "erfolreich hinzugefügt" aber dann müsste sich die Anzahl ja ändern, tut sie aber nicht.
 

Neue Themen


Oben