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
103 changes: 100 additions & 3 deletions src/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,105 @@
// TODO: Run task list app
import 'dart:io';

void main(List<String> args) {
print('Hello World');
List<TaskListApp> tasks = [];
int taskId = 0;

while (true) {
print('What would you like to do?');
print('1. List all tasks');
print('2. Add a task');
print('3. Update a task');
print('4. Remove a task');
print('5. Exit');

String? choice = stdin.readLineSync();

switch (choice) {
case '1':
// List all tasks
for (TaskListApp task in tasks) {
print(task);
}

break;
case '2':
// Add a task
print('Enter task name:');
String name = stdin.readLineSync()!;

Choose a reason for hiding this comment

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

Suggested change
String name = stdin.readLineSync()!;
String name = stdin.readLineSync()!;
if(name.isNotEmpty || name == "")
{
print("Please enter a valid title")
}

print('Enter task description:');
String description = stdin.readLineSync()!;
tasks.add(
TaskListApp(id: ++taskId, name: name, description: description));

Choose a reason for hiding this comment

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

Before adding a task, null check. If i press enter and its empty it shouldnt let me continue

break;
Comment on lines +26 to +33

Choose a reason for hiding this comment

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

Make the cases all functions instead

case '3':
// Update a task
try {
print('Enter task ID:');
int id = int.parse(stdin.readLineSync()!);
TaskListApp? taskToUpdate = tasks.firstWhere((task) => task.id == id);
print('Enter new task name (leave blank to keep current name):');
String? name = stdin.readLineSync();
print(
'Enter new task description (leave blank to keep current description):');
String? description = stdin.readLineSync();
print(
'Would you like to mark this task as completed? (1 for yes, 0 for no)');
String? completed = stdin.readLineSync();
if (completed == '1') {
taskToUpdate.markAsCompleted();
}
if (completed == '0') {
taskToUpdate.markAsIncomplete();
}
if (name != null && name.isNotEmpty) {
taskToUpdate.name = name;
}

if (description != null && description.isNotEmpty) {
taskToUpdate.description = description;
}
} catch (e) {
print('Task not found, please enter a valid task ID.');
}
break;
Comment on lines +36 to +64

Choose a reason for hiding this comment

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

Same thing, pull to a function instead

case '4':
// Remove a task
print('Enter task ID:');
int id = int.parse(stdin.readLineSync()!);
tasks.removeWhere((task) => task.id == id);

Choose a reason for hiding this comment

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

very good use of removeWhere. Maybe add a check where if the list is still the same length then ID is wrong

break;
case '5':
// Exit
return;
default:
print('Invalid choice');
break;
}
}
}

class TaskListApp {
// TODO: Implement Task list app
int id;
String name;
String description;
bool isCompleted;

TaskListApp(
{required this.id,
required this.name,
required this.description,
this.isCompleted = false});

void markAsCompleted() {
isCompleted = true;
}

void markAsIncomplete() {
isCompleted = false;
}

@override
String toString() {
return 'Task ID: $id\nTask: $name\nDescription: $description\nCompleted: $isCompleted\n';
}
}
Comment on lines 81 to 105

Choose a reason for hiding this comment

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

I would recommend code separation here. Put it in a different file