import 'dart:io'; import 'Todolist.dart'; void main(List args) { Todolist todolist = new Todolist(); bool tryAgain = true; while (tryAgain) { List all = todolist.getAll(); print('=========='); print('Todo List:'); String headerTable = 'No'.padLeft(3, ' ') + '. ' + 'NRP'.padLeft(15, ' ') + ' ' + 'Nama'.padRight(30, ' ') + ' ' + 'Todo'.padRight(30, ' ') + ' ' + 'Aktif' + ' ' + 'Done'; print(headerTable); print(''.padLeft(headerTable.length, '=')); all.asMap().forEach((key, val) { print((key + 1).toString().padLeft(3, ' ') + '. ' + val['nrp'].toString().padLeft(15, ' ') + ' ' + val['name'].padRight(30, ' ') + ' ' + val['todo'].padRight(30, ' ') + ' ' + (val['is_active'] == true ? 'y' : 'n').padRight(5, ' ') + ' ' + (val['is_done'] == true ? 'y' : 'n')); }); print('Menu'); print('1. Tambah todo'); print('2. Edit todo'); print('3. Delete todo'); print('0. Keluar'); stdout.write('Masukan menu (0 - 4): '); String? selected = stdin.readLineSync(); switch (selected) { case '0': tryAgain = false; continue; case '1': print('\t1. Tambah todo'); // memberi nilai bebas. karena kalau tidak diberi nilai akan error int nrp = 0; String name = ''; String todo = ''; bool is_active = false; bool is_done = false; stdout.write('\t'); bool stop_nrp = false; while (!stop_nrp) { stdout.write(' nrp: '); String? tmpNrp = stdin.readLineSync(); if (tmpNrp != null && int.tryParse(tmpNrp) != null) { nrp = int.parse(tmpNrp); stop_nrp = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; stdout.write('\t'); bool stop_name = false; while (!stop_name) { stdout.write(' nama: '); String? tmpName = stdin.readLineSync(); if (tmpName != null && tmpName.trim() != '') { name = tmpName.trim(); stop_name = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; stdout.write('\t'); bool stop_todo = false; while (!stop_todo) { stdout.write(' todo: '); String? tmpTodo = stdin.readLineSync(); if (tmpTodo != null && tmpTodo.trim() != '') { todo = tmpTodo.trim(); stop_todo = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; stdout.write('\t'); bool stop_isActive = false; while (!stop_isActive) { stdout.write(' is_aktif (y/n) (default y): '); String? tmpIsActive = stdin.readLineSync(); if (tmpIsActive != null && ['', 'y', 'n'].contains(tmpIsActive.trim().toLowerCase())) { switch (tmpIsActive.trim().toLowerCase()) { case '': case 'y': is_active = true; break; case 'n': is_active = false; break; } ; stop_isActive = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; stdout.write('\t'); bool stop_isDone = false; while (!stop_isDone) { stdout.write(' is_done (y/n) (default n): '); String? tmpIsDone = stdin.readLineSync(); if (tmpIsDone != null && ['', 'y', 'n'].contains(tmpIsDone.trim().toLowerCase())) { switch (tmpIsDone.trim().toLowerCase()) { case 'y': is_done = true; break; case '': case 'n': is_done = false; break; } ; stop_isDone = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; bool isSuccess = todolist.add( nrp: nrp, name: name, todo: todo, is_active: is_active, is_done: is_done); isSuccess ? stdoutWriteSucc('[Add Success]\n') : stdoutWriteErr('[Add Failed]\n'); break; case '2': int? noUrut; stdout.write('\t'); do { stdout.write('no urut todo: '); String? tmpNoUrut = stdin.readLineSync(); if (tmpNoUrut != null && int.tryParse(tmpNoUrut) != null && todolist.getByNoUrut(int.parse(tmpNoUrut)) != null) { noUrut = int.parse(tmpNoUrut); } else { stdoutWriteErr('\t[invalid input] '); } ; } while (noUrut == null); print('\t2. Edit todo No Urut ' + noUrut.toString()); int? nrp; String? name; String? todo; bool? is_active; bool? is_done; stdout.write('\t'); bool stop_nrp = false; while (!stop_nrp) { stdout.write(' nrp (empty for not change): '); String? tmpNrp = stdin.readLineSync(); if (tmpNrp != null) { if (tmpNrp.trim() == '') { stop_nrp = true; continue; } else if (int.tryParse(tmpNrp) != null) { nrp = int.parse(tmpNrp); stop_nrp = true; continue; } ; } ; stdoutWriteErr('\t[invalid input]'); } ; stdout.write('\t nama (empty for not change): '); name = stdin.readLineSync(); if (name != null && name.trim() == '') { name = null; } ; stdout.write('\t todo (empty for not change): '); todo = stdin.readLineSync(); if (todo != null && todo.trim() == '') { todo = null; } ; stdout.write('\t'); bool stop_isActive = false; while (!stop_isActive) { stdout.write(' is_aktif (y/n) (empty if not change): '); String? tmpIsActive = stdin.readLineSync(); if (tmpIsActive != null && ['', 'y', 'n'].contains(tmpIsActive.trim().toLowerCase())) { switch (tmpIsActive.trim().toLowerCase()) { case 'y': is_active = true; break; case 'n': is_active = false; break; } ; stop_isActive = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; stdout.write('\t'); bool stopIsDone = false; while (!stopIsDone) { stdout.write(' is_done (y/n) (empty if not change): '); String? tmpIsDone = stdin.readLineSync(); if (tmpIsDone != null && ['', 'y', 'n'].contains(tmpIsDone.trim().toLowerCase())) { switch (tmpIsDone.trim().toLowerCase()) { case 'y': is_done = true; break; case 'n': is_done = false; break; } ; stopIsDone = true; continue; } ; stdoutWriteErr('\t[invalid input]'); } ; bool isSuccess = todolist.edit(noUrut, nrp: nrp, name: name, todo: todo, is_active: is_active, is_done: is_done); isSuccess ? stdoutWriteSucc('[Edit Success]\n') : stdoutWriteErr('[Edit Failed]\n'); break; case '3': stdout.write('\t'); int? noUrut; while (noUrut == null) { stdout.write(' no urut todo: '); String? tmpNoUrut = stdin.readLineSync(); if (tmpNoUrut != null && int.tryParse(tmpNoUrut) != null && todolist.getByNoUrut(int.parse(tmpNoUrut)) != null) { noUrut = int.parse(tmpNoUrut); continue; } ; stdoutWriteErr('\t[invalid input]'); } ; bool isSuccess = todolist.deleteByNoUrut(noUrut); isSuccess ? stdoutWriteSucc('[Delete Success]\n') : stdoutWriteErr('[Delete Failed]\n'); break; default: stdoutWriteErr('[invalid input]\n'); break; } ; } ; stdout.write('Keluar dari program...\n'); } void stdoutWriteSucc(String text) { stdout.write('\x1B[32m' + text + '\x1B[0m'); } void stdoutWriteErr(String text) { stdout.write('\x1B[31m' + text + '\x1B[0m'); }