Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Add a to-do item
Delete a to-do item
View the to-do items
Description:
Make sure to structure your program in a modular way. In this case, that means you would have a command-line application which uses a class that holds the to-do items internally and provides public methods to add an item, delete an item, and provide the list of to-do items.


What I have tried:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class toDoList {

    private static List<String> currentList = new ArrayList<String>();

    public static void main(String[] args) {

        int menuItem = -1;
        while (menuItem != 0) {
            menuItem = menu();
            switch (menuItem) {
            case 1:
                showList();
                break;
            case 2:
                addItem();
                break;
            case 3:
                removeItem();
                break;
            case 0:
                break;
            default:
                System.out.println("Enter a valid option");
            }
        }
    }

    public static int menu() {
        System.out.println();
        System.out.println("----------------------");
        System.out.println("Main Menu");
        System.out.println("----------------------");
        System.out.println("0. Exit the program");
        System.out.println("1. Display to-do list");
        System.out.println("2. Add item to list");
        System.out.println("3. Remove item from list");
        System.out.println();
        System.out.print("Enter choice: ");
        int choice = sc.nextInt();
        return choice;
    }

    public static void showList() {
        System.out.println();
        System.out.println("----------------------");       
        System.out.println("To-Do List");
        System.out.println("----------------------");
        int number = 0;
        for (String item : currentList) {
            System.out.println(++number + " " + item);
        }
        System.out.println("----------------------");


    }

    public static void addItem() {
        System.out.println("Add Item");
        System.out.println("----------------------");
        System.out.print("Enter an item: ");
        Scanner sc = new Scanner(System.in);
        String item = sc.nextLine();
        currentList.add(item);
        showList();
    }

    public static void removeItem() {
        System.out.println("Remove Item");
        System.out.println("----------------------");
        Scanner sc = new Scanner(System.in);
        System.out.print("What do you want to remove?");
        int index = sc.nextInt();
        if((index-1)<0 || index>currentList.size()) {
            System.out.println("Wrong index number! Please enter in range of 1 to "+currentList.size());            
        }else {
            currentList.remove(index-1);
        }
        System.out.println("----------------------");
        showList();


    }
}
Posted
Comments
Patrice T 10-Sep-22 13:31pm    
And you have a question or a problem with your code ?
OriginalGriff 10-Sep-22 16:08pm    
And? What does it do that you didn't expect, or not do that you did? What have you tried to do to find out why? Are there any error messages, and if so, where and when? What did you do to make them happen? This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 11-Sep-22 3:50am    
I think your class is not quite correct according to: "an application which uses a class that holds the to-do items internally and provides public methods to add an item, delete an item, and provide the list of to-do items.". This suggest that you should have a separate <code<todolist< code=""> class that provides those three methods, but not as static methods in your main class.

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