INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Todo list in java

Todo list in java

(OP)
Hi everybody
I am very new to Java and trying to help my daughter in a school project using BlueJ.
We need to create a todo list we called TodoList.
We have a number of tasks to accomplish, such as: "eat", "drink", "shower", "dress", "take the bus", etc.
Each would have a status value either False, indicating not done, or true indicating done.
My question is: how do I do this, the simplest way?

Thanks in advance

Regards
Eman

______________________________________
Eman
Technical User

RE: Todo list in java

Hi

More details, please :
  • Do you need a complete application ?
    • Do you need a CLI application ?
    • Do you need a GUI application ?
  • Do you need just one class runnable in BlueJ's object bench ?
( If you answer "application", the next questions will be about the expected storage : file ( plain text or some structured format ) or database ( SQL or NoSQL ). If you answer "class", will suppose no persistent storage is required. )

Or another way : how old is your daughter and for how many years she learned programming before ?

Feherke.
feherke.ga

RE: Todo list in java

(OP)
Hi feherke
Actually, we have a simple application where you enter which direction you can go and what you can do there.
We decided to do the daily routine, like start in bedroom, goto the kitchen, eat, drink, goto the bathroom, brush teeth, shower, goto the office room, and so on until you leave and take the bus and go to work.
What we are trying to do is to let the user make sure to do all the necessary tasks, so we check what is done what is not.
I hope I stated it clear enough.
The user enters simple text in each step and gets what options he has. It's called zuul game.

______________________________________
Eman
Technical User

RE: Todo list in java

(OP)
Do you need a complete application ?
Do you need a CLI application ?
Do you need a GUI application ?
Do you need just one class runnable in BlueJ's object bench ?

I would say the last one please.
just one class runnable in BlueJ's object bench

Thanks

______________________________________
Eman
Technical User

RE: Todo list in java

(OP)
OK, perhaps a more concrete question could have a better chance. Here is what I have done:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;

/**
*
* @author Eman Fatih
* @version 20.03.2014
*/
public class TodoList
{
/**
* Konstruktor für die Objekte der Klasse TodoList (sorry, German is used in Switzerland)
*/
public static void main(String[] args)
{
// initialise Objekt variables

ArrayList<String> TodoList = new ArrayList<String>();

TodoList.add("get dressed");
TodoList.add("brush teeth");
TodoList.add("...ehm...");
TodoList.add("shower");
TodoList.add("eat");
TodoList.add("drink");
TodoList.add("buy ticket");

/* list the contents */
}
public void todolistAusgeben()
{
int sizeOfList=TodoList.size();
for (int i=0; i < sizeOfList; i++) {
String sTodo = TodoList.get(i);
System.out.println(sTodo);
}
}

}


When I compile, it highlights .size and says: Cannot find symbol - method size()
int sizeOfList=TodoList.size();

______________________________________
Eman
Technical User

RE: Todo list in java

Hi

Next time please post your code between [code] and [/code] ( or at least [pre] and [/pre] ) TGML tags. Between [tt] and [/tt] tags the whitespaces are not preserved and the indentation vanishes.

There you are approaching obfuscated coding : you use the same TodoList name both for your class and the storage variable. But the real problem is, you declared the storage variable inside the main() method, making it its local variable. So not accessible anywhere outside.

And String is not enough for a a task. Unless a task will have nothing but a name. But you wrote that you need statuses too, so better declare a suitable structure for that. Below I declared a Task class.

Here I tried to include what I learned about Java and OOP coding habits. ( Of course, experts in this fields will probably find reasons to criticize it... If for nothing else, then for the lack of exception handling. ) This you should be able to both test in BlueJ's object bench and run it from command line.

CODE --> TodoList.java

import java.util.ArrayList;

public class TodoList {

  private ArrayList<Task> theList;

  public static void main(String[] args)
  {

    TodoList myList = new TodoList();

    myList.add("get dressed");
    myList.add("brush teeth");
    myList.add("...ehm...");
    myList.add("shower");
    myList.add("eat");
    myList.add("drink");
    myList.add("buy ticket");

    myList.done("brush teeth");
    myList.done("shower");

    myList.todolistAusgeben();

  }

  public TodoList()
  {
    theList = new ArrayList<Task>();
  }

  public void add(String activity)
  {
    theList.add(new Task(activity));
  }

  public Task get(String activity)
  {
    for (Task task : theList)
      if (task.isCalled(activity))
        return task;

    return null;
  }

  public void done(String activity)
  {
    Task task = get(activity);

    if (task != null)
      task.done();
  }

  public void todolistAusgeben()
  {
    int sizeOfList = theList.size();
    for (int i=0; i < sizeOfList; i++) {
      Task sTodo = theList.get(i);
      System.out.println(sTodo);
    }
  }

   private class Task {

    private String name;
    private boolean status;

    public Task(String name)
    {
      this.name = name;
      this.status = false;
    }

    public boolean isCalled(String name)
    {
      return this.name.equals(name);
    }

    public void done()
    {
      status = true;
    }

    public String toString()
    {
      return name + " : " + (status ? "DONE" : "Waiting...");
    }

  }

} 

Feherke.
feherke.ga

RE: Todo list in java

(OP)
Hi
Thank you very much, that looks great. Sorry for missing the code /code thing. I have not posted here for long time.
OK, the task idea was my first idea, because I thought each item would have either True or false, as done or not done, or taken / not taken (Laptop, books, keys, etc).
But then discussing with my daughter, we thought it would be simpler to just fill/empty the list.
The list is filled initially and each time the player takes an object or accomplishes a task (eat, shower, etc), that item is simply removed from the list.
So as you get out of the appartment, we check if you missed something (to do or to take) by checking the list size, and if not empty, list the remaining items to the player.

What do you think?

______________________________________
Eman
Technical User

RE: Todo list in java

Hi

Quote (Eman)

we thought it would be simpler to just fill/empty the list.
Oops. I am afraid my head was full of my own todo application's plans, which includes priority, category, due date and similar stuff.

Trying to keep it simple :

CODE --> TodoList.java

import java.util.ArrayList;

public class TodoList {

  private ArrayList<String> theList;

  public static void main(String[] args)
  {

    TodoList myList = new TodoList();

    myList.add("get dressed");
    myList.add("brush teeth");
    myList.add("shower");
    myList.add("eat");

    myList.done("brush teeth");
    myList.done("shower");

    myList.leave();

    myList.done("get dressed");
    myList.done("eat");

    myList.leave();

  }

  public TodoList()
  {
    theList = new ArrayList<String>();
  }

  public void add(String activity)
  {
    theList.add(activity);
  }

  public void done(String activity)
  {
    theList.remove(activity);
  }

  public void leave()
  {
    if (theList.isEmpty())
      System.out.println("Good Bye !");
    else {
      System.out.println("You have " + String.valueOf(theList.size()) + " tasks to complete before you can leave :");
      todolistAusgeben();
    }
  }

  public void todolistAusgeben()
  {
    for (String sTodo : theList)
      System.out.println(" - " + sTodo);
  }

} 

Further ideas ( just the simple ones ) :
  • add new method to check whether a given task is still on the list
  • modify the add() method to avoid adding the same task twice
  • modify the done() method to yell if you try to complete a task not on the list
  • modify the leave() method to count the failed leaving attempts and output messages accordingly

Feherke.
feherke.ga

RE: Todo list in java

(OP)
Great!!
I'll discuss this with my daughter.
Thank you very much. I am sure with this base we can reach interesting results.

Have a good time, Feherke

Regards
Eman

______________________________________
Eman
Technical User

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close