import 'dart:io'; import 'Todolist.dart'; import 'dart:convert'; 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'); int? nrp; String? name; String? todo; bool? is_active; bool? is_done; stdout.write('\t'); do{ stdout.write(' nrp: '); String? tmpNrp = stdin.readLineSync(); if (tmpNrp != null) { nrp = int.tryParse(tmpNrp); } else { stdoutWriteErr('\t[invalid input]'); }; } while(nrp == null); stdout.write('\t'); do{ stdout.write(' nama: '); name = stdin.readLineSync(); } while(name == null); do{ stdout.write('\t todo: '); todo = stdin.readLineSync(); }while(todo == null); stdout.write('\t'); do { stdout.write(' is_aktif (y/n) (default y): '); String? tmpIsActive = stdin.readLineSync(); if (tmpIsActive != null) { switch (tmpIsActive.trim().toLowerCase()) { case '': case 'y': is_active = true; break; case 'n': is_active = false; break; default: stdoutWriteErr('\t[invalid input]'); break; }; }; } while (is_active == null); stdout.write('\t'); do{ stdout.write(' is_done (y/n) (default n): '); String? tmpIsDone = stdin.readLineSync(); if(tmpIsDone != null){ switch (tmpIsDone.trim().toLowerCase()) { case 'y': is_done = true; break; case '': case 'n': is_done = false; break; default: stdoutWriteErr('\t[invalid input]'); break; }; }; }while(is_done == null); 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 stopNrp = false; while(!stopNrp) { stdout.write(' nrp: '); String? tmpNrp = stdin.readLineSync(); if (tmpNrp != null) { if(tmpNrp.trim() != '' && int.tryParse(tmpNrp) != null) { nrp = int.parse(tmpNrp.trim()); }; stopNrp = true; continue; }; stdoutWriteErr('\t[invalid input]'); }; stdout.write('\t'); stdout.write(' nama: '); name = stdin.readLineSync(); if(name != null && name.trim() == ''){ name = null; }; stdout.write('\t todo: '); todo = stdin.readLineSync(); if(todo != null && todo.trim() == '') { todo = null; }; stdout.write('\t'); bool stopIsActive = false; while(!stopIsActive) { stdout.write(' is_aktif (y/n)(empty if not change): '); String? tmpIsActive = stdin.readLineSync(); if (tmpIsActive != null) { switch (tmpIsActive.trim().toLowerCase()) { case '': break; case 'y': is_active = true; break; case 'n': is_active = false; break; default: stdoutWriteErr('\t[invalid input]'); continue; break; }; }; stopIsActive = true; }; 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){ switch (tmpIsDone.trim().toLowerCase()) { case '': break; case 'y': is_done = true; break; case 'n': is_done = false; break; default: stdoutWriteErr('\t[invalid input]'); continue; break; }; }; stopIsDone = true; }; 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; 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); 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'); }