Created
March 1, 2023 04:56
-
-
Save ashraf267/35fe94b6d06950c5182ed871e5c429f0 to your computer and use it in GitHub Desktop.
first-come-first-serve. This works!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| // function call | |
| fcfs([ | |
| // then this, secondly | |
| Task( | |
| 't1', 10, | |
| ), | |
| // by rule, this should exec. first | |
| Task( | |
| 't2', 0, | |
| ), | |
| // then this, lastly | |
| Task( | |
| 't3', 15, | |
| ), | |
| ]); | |
| } | |
| class Task { | |
| String taskName; | |
| int arrivalTime; | |
| Task(this.taskName, this.arrivalTime); | |
| // doTask function | |
| void doTask(int taskId) { | |
| // TODO: find ways to automate this potentially long if-else statements | |
| if (taskId == 10) { | |
| print("I am task 1"); | |
| } else if(taskId == 0) { | |
| print("I am task 2"); | |
| } else { | |
| // task 3 | |
| print("I am task 3"); | |
| } | |
| } | |
| } | |
| void fcfs(List<Task> tasks) { | |
| // sort | |
| tasks.sort((a, b) => a.arrivalTime.compareTo(b.arrivalTime)); | |
| // todo | |
| for(Task myTask in tasks) { | |
| // print(myTask.arrivalTime); | |
| print(myTask.taskName); | |
| // call myTask.doTask and pass arg | |
| int i = myTask.arrivalTime; | |
| myTask.doTask(i); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment