Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Created March 1, 2023 04:56
Show Gist options
  • Select an option

  • Save ashraf267/35fe94b6d06950c5182ed871e5c429f0 to your computer and use it in GitHub Desktop.

Select an option

Save ashraf267/35fe94b6d06950c5182ed871e5c429f0 to your computer and use it in GitHub Desktop.
first-come-first-serve. This works!
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