Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As a user I should be able to print my name on the screen

As a user I should be able to list at least 5 tasks for the day.

As a user I should be able to see all the tasks in increasing and decreasing order.

As a user I should be able to see the repeated tasks.

What I have tried:

So, I'm using doWhile Loop to display the menu at least once. Then I'm using switch case for each menu item. But how do I write the exact methodfor eac menu item so as to enter a task, remove a task, update a task. What concept do I use to get the desired results?

Here's, my Code:
Java
package Todo_Manager;
import java.util.Scanner;

public class Todo_Manager {

	public static void main(String[] args) {

		System.out.println("Welcome to your Todo Manager!\n");
		System.out.println("Please Enter your Name: ");
		Scanner in = new Scanner(System.in);
		String name = in.next();
		System.out.println("Hello "+name);
		
		String choice = null;
		String[] task = new String[10];
		Scanner input = new Scanner(System.in);
		
		do {
			//Display the Menu
			System.out.println("Please choose an option\n");
			System.out.println("1. Enter a task ");
			System.out.println("2. Remove a task ");
			System.out.println("3. Update a task ");
			System.out.println("4. List all tasks ");
			System.out.println("5. Exit");
			choice = input.nextLine();
			
			switch(choice) {
			case "1":        
				System.out.println("Enter number of tasks: ");
				task = input.nextInt();
				System.out.println("Enter a task ");
				choice = input.nextLine();
				break;
			case "2":
				System.out.println("Remove a task ");
				choice = input.nextLine();
				break;
			case "3":
				System.out.println("Remove a task ");
				choice = input.nextLine();
				break;
			case "4":
				System.out.println("Remove a task ");
				choice = input.nextLine();
				break;
			case "5":
				System.out.println("Remove a task ");
				choice = input.nextLine();
				break;
			}
		} while ( Integer.parseInt(choice)>0);		
	}
}
Posted
Updated 21-Jun-23 1:13am
v4
Comments
Richard MacCutchan 22-Feb-22 6:06am    
Please edit your question and add proper <pre> tags around your code.
Richard MacCutchan 22-Feb-22 6:08am    
task = input.nextInt();

But task is declared as an array of Strings.

FIrst off, indent and space yoru code correctly - double spacing it doesn't make it look "bigger", "better", or "more complete" it just makes it harder to read...
And indentation that suddenly goes flat to the left makes it again harder to read - and look like you couldn't be bothered as well, as most IDE's do a damn good job of indenting for you as you go along ...

But "What I have tried" shows exactly what *you* have tried: nothing at all so far and that you haven't even understood the basic framework code your teacher gave you ... stop throwing random code together and hoping that it'll work: that won't happen. Instead, sit down and think about the problem you have been set.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
MOHAMMAD ZAIGAM 22-Feb-22 3:54am    
Alright man, I understand. I'll be more precise next time.
Quote:
How to create a todo list in java?

First, you need to define what is a task, what information is needed in each task. Do you need to manage sub-tasks ?
You need a way to manage the list of tasks: Load/save on disk, management in memory, database ...

starting coding from scatch without those is a bad idea.

Have a look at ToDoList 8.0 - An Effective and Flexible Way to Keep on Top of Your Tasks[^] to see how others have done.
 
Share this answer
 
You need a container for storing the tasks. I give you a working, though largely incomplete code sample, using a TreeMap:
Java
public class Todo_Manager
{

  public static void main(String[] args)
  {

    System.out.println("Welcome to your Todo Manager!\n");
    System.out.println("Please Enter your Name: ");
    Scanner in = new Scanner(System.in);
    String name = in.next();
    System.out.println("Hello "+name);

    int choice;

    TreeMap <Integer, String> taskmap = new TreeMap<Integer, String>();

    do
    {
      //Display the Menu
      System.out.println("Please choose an option\n");
      System.out.println("1. Enter a task ");
      System.out.println("2. Remove a task ");
      System.out.println("3. Update a task ");
      System.out.println("4. List all tasks ");
      System.out.println("5. Exit");
      choice = in.nextInt();


      switch(choice)
      {
      case 1:
        System.out.println("Enter number of the task: ");
        Integer no_of_task = Integer.valueOf(in.nextInt());
        in.nextLine();
        System.out.println("Enter the task");
        String task = in.nextLine();
        taskmap.put( no_of_task, task);
        break;
      case 2:
        System.out.println("Remove a task ");
        // TODO: implement it
        break;
      case 3:
        System.out.println("Update a task ");
        // TODO: implement it
        break;
      case 4:
        System.out.println("List all tasks ");
        list_all(taskmap);
        break;
      default:
        // do nothing
      }
    } while ( choice != 5);
  }

  public static void list_all( TreeMap<Integer, String> tm)
  {
    for (Map.Entry<Integer, String> item : tm.entrySet())
    {
      System.out.print(item.getKey()); System.out.print(" "); System.out.println(item.getValue());
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900