-
Notifications
You must be signed in to change notification settings - Fork 7
Faris Alzayed - Task 2 Project #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()!; | ||
| print('Enter task description:'); | ||
| String description = stdin.readLineSync()!; | ||
| tasks.add( | ||
| TaskListApp(id: ++taskId, name: name, description: description)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend code separation here. Put it in a different file |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.