Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Todo list in java

Status
Not open for further replies.

eman6

Programmer
Dec 7, 2004
578
CH
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
 
Hi

More details, please :
[ul]
[li]Do you need a complete application ?[ul]
[li]Do you need a CLI application ?[/li]
[li]Do you need a GUI application ?[/li][/ul][/li]
[li]Do you need just one class runnable in BlueJ's object bench ?[/li]
[/ul]
( 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
 
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
 
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
 
OK, perhaps a more concrete question could have a better chance. Here is what I have done:

[tt]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);
}
}

}[/tt]

When I compile, it highlights .size and says: Cannot find symbol - method size()
int sizeOfList=TodoList[highlight #CC0000].size[/highlight]();


______________________________________
Eman
Technical User
 
Hi

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] ( or at least [tt][ignore][pre][/ignore][/tt] and [tt][ignore][/pre][/ignore][/tt] ) TGML tags. Between [tt][ignore][tt][/ignore][/tt] and [tt][ignore][/tt][/ignore][/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 [tt]String[/tt] 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:
[b]import[/b] java.util.ArrayList;

[b]public[/b] [b]class[/b] TodoList {

  [b]private[/b] ArrayList<Task> theList;

  [b]public[/b] [b]static[/b] [b]void[/b] main(String[] args)
  {

    TodoList myList = [b]new[/b] TodoList();

    myList.add([green][i]"get dressed"[/i][/green]);
    myList.add([green][i]"brush teeth"[/i][/green]);
    myList.add([green][i]"...ehm..."[/i][/green]);
    myList.add([green][i]"shower"[/i][/green]);
    myList.add([green][i]"eat"[/i][/green]);
    myList.add([green][i]"drink"[/i][/green]);
    myList.add([green][i]"buy ticket"[/i][/green]);

    myList.done([green][i]"brush teeth"[/i][/green]);
    myList.done([green][i]"shower"[/i][/green]);

    myList.todolistAusgeben();

  }

  [b]public[/b] TodoList()
  {
    theList = [b]new[/b] ArrayList<Task>();
  }

  [b]public[/b] [b]void[/b] add(String activity)
  {
    theList.add([b]new[/b] Task(activity));
  }

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

    [b]return[/b] null;
  }

  [b]public[/b] [b]void[/b] done(String activity)
  {
    Task task = get(activity);

    [b]if[/b] (task != null)
      task.done();
  }

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

   [b]private[/b] [b]class[/b] Task {

    [b]private[/b] String name;
    [b]private[/b] boolean status;

    [b]public[/b] Task(String name)
    {
      [b]this[/b].name = name;
      [b]this[/b].status = false;
    }

    [b]public[/b] boolean isCalled(String name)
    {
      [b]return[/b] [b]this[/b].name.equals(name);
    }

    [b]public[/b] [b]void[/b] done()
    {
      status = true;
    }

    [b]public[/b] String toString()
    {
      [b]return[/b] name + [green][i]" : "[/i][/green] + (status ? [green][i]"DONE"[/i][/green] : [green][i]"Waiting..."[/i][/green]);
    }

  }

}

Feherke.
feherke.ga
 
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
 
Hi

Eman said:
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:
[b]import[/b] java.util.ArrayList;

[b]public[/b] [b]class[/b] TodoList {

  [b]private[/b] ArrayList<String> theList;

  [b]public[/b] [b]static[/b] [b]void[/b] main(String[] args)
  {

    TodoList myList = [b]new[/b] TodoList();

    myList.add([green][i]"get dressed"[/i][/green]);
    myList.add([green][i]"brush teeth"[/i][/green]);
    myList.add([green][i]"shower"[/i][/green]);
    myList.add([green][i]"eat"[/i][/green]);

    myList.done([green][i]"brush teeth"[/i][/green]);
    myList.done([green][i]"shower"[/i][/green]);

    myList.leave();

    myList.done([green][i]"get dressed"[/i][/green]);
    myList.done([green][i]"eat"[/i][/green]);

    myList.leave();

  }

  [b]public[/b] TodoList()
  {
    theList = [b]new[/b] ArrayList<String>();
  }

  [b]public[/b] [b]void[/b] add(String activity)
  {
    theList.add(activity);
  }

  [b]public[/b] [b]void[/b] done(String activity)
  {
    theList.remove(activity);
  }

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

  [b]public[/b] [b]void[/b] todolistAusgeben()
  {
    [b]for[/b] (String sTodo : theList)
      System.out.println([green][i]" - "[/i][/green] + sTodo);
  }

}

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


Feherke.
feherke.ga
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top