Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions src/task.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@



class Task {

late int ID;
late String Title;
late String Description;
late bool isCompleted;

Task(this.ID, this.Title, this.Description, this.isCompleted);

Task.create() {
ID = 0;
Title = '';
Description = '';
isCompleted = false;
}
}

Comment on lines +4 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here you should follow the naming convention for variables.

use "late" only when necessary you could replace it by making constructor required for example:

Suggested change
class Task {
late int ID;
late String Title;
late String Description;
late bool isCompleted;
Task(this.ID, this.Title, this.Description, this.isCompleted);
Task.create() {
ID = 0;
Title = '';
Description = '';
isCompleted = false;
}
}
Task({
required this.id,
required this.title,
required this.description,
required this.isCompleted,
});

you can use factory method too :

Suggested change
class Task {
late int ID;
late String Title;
late String Description;
late bool isCompleted;
Task(this.ID, this.Title, this.Description, this.isCompleted);
Task.create() {
ID = 0;
Title = '';
Description = '';
isCompleted = false;
}
}
factory Task.create() {
return Task(
id: 0,
title: '',
description: '',
isCompleted: false,
);
}




183 changes: 183 additions & 0 deletions src/task_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@

import'dart:io';
import 'task.dart';


List <Task> All_tasks = [] ;


void add(){

late Task new_task =Task.create();

new_task.ID = All_tasks.length;

stdout.write('task title: ');
new_task.Title = stdin.readLineSync()!;
Copy link
Copy Markdown

@Radwan-Albahrani Radwan-Albahrani Oct 1, 2023

Choose a reason for hiding this comment

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

Should add null safety to this

Suggested change
new_task.Title = stdin.readLineSync()!;
String? title = stdin.readLineSync() ?? ""
if(title == "")
{
stdout.write("Title Invalid")
}

Maybe add a while loop or a try catch to keep the user in this state until a title is given


stdout.write('task description: ');
new_task.Description = stdin.readLineSync()!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same here


All_tasks.add(new_task);

print('The task has been added successfully!');
main();

}

void remove(){


stdout.write('Please enter the ID of the task you want to remove: ');
int task_id = int.parse(stdin.readLineSync()!);

All_tasks.remove(All_tasks[task_id]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Check the ID before removing. Could Cause index out of bounds exception


print('The task has been removed successfully!');

main();

}

void update(){


stdout.write('Please enter the ID of the task you want to update: ');
int task_id = int.parse(stdin.readLineSync()!);

Task the_task = All_tasks[task_id];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same here


stdout.write('What do you want to update (title, description, completed)?');
String? field = stdin.readLineSync()!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Null Checking here


field.toLowerCase();

if (field == 'title')
{
stdout.write('Enter new title please: ');
the_task.Title = stdin.readLineSync()!;

}
else if (field == 'description')
{
stdout.write('Enter new description please: ');
the_task.Description = stdin.readLineSync()!;
}
else if (field == 'competed')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Mis spelled

{
the_task.isCompleted = !(the_task.isCompleted);
}
else
{
print('Please select a valid field');
}

print('The task has been updated successfully!');
main();


}

void printRow(List<String> row, List<int> columnWidths) {

for (int i = 0; i < row.length; i++) {
String cell = row[i];
String padding = ' ' * (columnWidths[i] - cell.length);
stdout.write('| $cell$padding ');
}
stdout.write('|\n');

}

void printSubheadings(List<int> columnWidths) {

stdout.write('+' + ('-' * (columnWidths.reduce((a, b) => a + b) + 3 * columnWidths.length - 1)) + '+');
stdout.write('\n');
}

void view(){

try {

List<List<String>> tasks = [];
List<String> sub_headings = ['ID', 'Title', 'Description', 'Completed' ];
tasks.add(sub_headings);

for (var i = 0; i < All_tasks.length; i++){

Task task = All_tasks[i];
List<String> single_task = [task.ID.toString() , task.Title.toString() ,task.Description.toString() , task.isCompleted.toString()];
tasks.add(single_task);
}

List<int> columnWidths = List.generate(tasks[0].length, (index) => 0);


for (var row in tasks) {
for (int i = 0; i < row.length; i++) {
if (row[i].length > columnWidths[i]) {
columnWidths[i] = row[i].length;

}
}
}

// Print the table headings
printRow(tasks[0], columnWidths);

// Print the subheadings
printSubheadings(columnWidths);

// Print the table rows
for (int i = 1; i < tasks.length; i++) {
printRow(tasks[i], columnWidths);
}

main();

}catch(e)
{
print('You dont have any tasks yet!');
main();
}

}



void main(){



print('Welcome to the task tracker app!');
stdout.write('Choose action: (Add) || (Remove) || (Update) || (View) || (Stop)\n');
String? action = stdin.readLineSync()!;

action.toLowerCase();

if (action == 'add')
{
add();
}
else if (action == 'Remove')
{
remove();
}
else if (action == 'update')
{
update();
}
else if (action == 'view')
{
view();
}
else if (action == 'stop')
{
return ;
}
else
{
print('Please select a valid action');
main();
}
}