-
Notifications
You must be signed in to change notification settings - Fork 7
Reema task-2 #4
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?
Reema task-2 #4
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,102 @@ | ||||||||||||
| // TODO: Run task list app | ||||||||||||
| void main(List<String> args) { | ||||||||||||
| print('Hello World'); | ||||||||||||
| import 'dart:io'; | ||||||||||||
|
|
||||||||||||
| class Task { | ||||||||||||
| String title; | ||||||||||||
|
|
||||||||||||
| Task(this.title); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| class TaskListApp { | ||||||||||||
|
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. 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
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. 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); | ||||||||||||
|
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. Needs null checking
Suggested change
|
||||||||||||
| 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? '); | ||||||||||||
|
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. 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'); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
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.
I would recommend keeping this model in a separate file