Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 98 additions & 4 deletions src/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,102 @@
// TODO: Run task list app
void main(List<String> args) {
print('Hello World');
import 'dart:io';

class Task {
String title;

Task(this.title);
}
Comment on lines +3 to 7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend keeping this model in a separate file


class TaskListApp {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This in another file as well

// TODO: Implement Task list app
List<Task> tasks = [];

//add task
void addTask(String title) {
tasks.add(Task(title));
print('Task is added');
}

//view tasks
void viewTasks() {
print('\nTasks: ');
for (int i = 0; i < tasks.length; i++) {
print('${tasks[i].title}');
}
}

//update
void updateTask(int index, String newTitle) {
if (index >= 0 && index < tasks.length) {
tasks[index].title = newTitle;
print('Task is updated');
} else {
print('Invalid index');
}
}

//delete
void deleteTask(int index) {
if (index >= 0 && index < tasks.length) {
tasks.removeAt(index);
print('Task is deleted');
} else {
print('Invalid task index.');
}
}

}

void main(List<String> args) {
TaskListApp taskList = TaskListApp();
while (true) {
print('\nTask List App Menu:');
print('1. Add Task');
print('2. View Tasks');
print('3. Update Task');
print('4. Delete Task');
print('5. Exit');
stdout.write('Enter your choice (1-6): ');
Comment on lines +51 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this into a separate function called "displayMenu()"


String choice = stdin.readLineSync() ?? '';
print('');

switch (choice) {
//adding
case '1':
stdout.write('Add a task: ');
String title = stdin.readLineSync() ?? '';
taskList.addTask(title);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs null checking

Suggested change
taskList.addTask(title);
if(title.isNotEmpty || title.trim() != "")
{
taskList.addTask(title);
}

break;

//view
case '2':
taskList.viewTasks();
break;

//updating
case '3':
taskList.viewTasks();
stdout.write('what is the number of the task to update? ');
int updateNum=int.tryParse(stdin.readLineSync() ?? '') ?? -1;
stdout.write('what is the new title? ');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great usage of tryParse

String newTitle = stdin.readLineSync() ?? '';
taskList.updateTask(updateNum - 1, newTitle);
break;

//delete
case '4':
taskList.viewTasks();
stdout.write('what is the number of the task to delete? ');
int indexToDelete = int.tryParse(stdin.readLineSync() ?? '') ?? -1;
taskList.deleteTask(indexToDelete - 1);
break;

//exit
case '5':
print('Exit');
exit(0);

default:
print('Invalid choice. Please try again');
}
}
}