EditStudentCommand.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package me.blackphreak.CommandHandling.Handlers;
  2. import me.blackphreak.CommandHandling.AbstractCommandHandler;
  3. import me.blackphreak.Database;
  4. import me.blackphreak.Debug.Debug;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import static me.blackphreak.Lib.promptQuestion;
  10. import static me.blackphreak.Lib.tryParseUInt;
  11. public class EditStudentCommand extends AbstractCommandHandler {
  12. private String lastCmd;
  13. private List<String> selectedStudentID;
  14. private String selectedColumn;
  15. public EditStudentCommand(String desc) {
  16. super(desc);
  17. }
  18. @Override
  19. public boolean handle() {
  20. boolean nextFlag;
  21. // reset
  22. selectedStudentID = new ArrayList<>();
  23. lastCmd = null;
  24. selectedColumn = null;
  25. do
  26. {
  27. nextFlag = selectStudentByID();
  28. // if lastCmd is set.
  29. if (lastCmd != null) {
  30. // in this case, lastCmd should be either "exit" nor "end".
  31. return lastCmd.equalsIgnoreCase("exit");
  32. }
  33. } while (!nextFlag);
  34. do {
  35. nextFlag = selectColumnToBeModified();
  36. if (lastCmd != null && lastCmd.equalsIgnoreCase("exit")) {
  37. return true;
  38. }
  39. } while (!nextFlag);
  40. doEdit();
  41. return true;
  42. }
  43. /**
  44. * Ask user to provide student ID before editing the targeted student(s).
  45. * @return true when selected at least one student, otherwise, false.
  46. */
  47. private boolean selectStudentByID() {
  48. do {
  49. System.out.println("\n** type \"next\" to stop providing StudentID **");
  50. System.out.println("** type \"exit\" to back to main menu **");
  51. System.out.println("\nSelected StudentID(#"+selectedStudentID.size()+"): " + Arrays.toString(selectedStudentID.toArray()));
  52. var inp = promptQuestion("Please provide StudentID: ", false);
  53. if (inp.equalsIgnoreCase("next"))
  54. {
  55. if (selectedStudentID.size() <= 0)
  56. System.out.println("Please provide at least one student ID before edit.");
  57. else
  58. return true;
  59. }
  60. else if (inp.equalsIgnoreCase("exit"))
  61. {
  62. lastCmd = "exit"; // back to main menu
  63. return false;
  64. }
  65. else
  66. {
  67. // query database to confirm that student is exist.
  68. try {
  69. if (Database.getInstance()
  70. .query("SELECT COUNT(*) FROM Student WHERE StudentID = \""+inp+"\";")
  71. .getInt(1) != 1)
  72. {
  73. System.out.println("Invalid StudentID, please try again. [Failed to select (!=1)]");
  74. continue;
  75. }
  76. } catch (SQLException e) {
  77. Debug.err("Failed to find student. (Database Error)");
  78. e.printStackTrace();
  79. lastCmd = "end"; // end the program
  80. return false;
  81. }
  82. selectedStudentID.add(inp);
  83. }
  84. } while (true);
  85. }
  86. private boolean selectColumnToBeModified() {
  87. do {
  88. System.out.println("\nColumns:");
  89. for (int i = 0; i < SearchStudentCommand.unmodColNames.size(); i++) {
  90. System.out.println(String.format(
  91. " %2s. %-15s",
  92. (i+1) + "",
  93. SearchStudentCommand.unmodColNames.get(i)
  94. ));
  95. }
  96. // exit option
  97. System.out.println(String.format(
  98. " %2s. %-15s",
  99. "99",
  100. "Back to main menu"
  101. ));
  102. var inp = promptQuestion("Please select one column to edit: ");
  103. var selected = tryParseUInt(inp, "Invalid column index");
  104. if (selected == -1)
  105. continue;
  106. if (selected == 99)
  107. {
  108. lastCmd = "exit";
  109. return false;
  110. }
  111. if (selected <= 0
  112. || selected > SearchStudentCommand.unmodColNames.size())
  113. {
  114. System.out.println("Invalid column index. [Out of range]");
  115. continue;
  116. }
  117. selectedColumn = SearchStudentCommand.unmodColNames.get(selected-1);
  118. return true;
  119. } while (true);
  120. }
  121. private void doEdit() {
  122. var newValue = promptQuestion("The value that you want it to be: ");
  123. var ids = String.join("\",\"", selectedStudentID);
  124. var updated = Database.getInstance().update("UPDATE Student SET " + selectedColumn + " = \""+newValue+"\" WHERE StudentID IN (\""+ids+"\");");
  125. if (updated <= 0)
  126. System.out.println("No changes have been made.");
  127. else
  128. System.out.println("Update successfully made to "+updated+" student(s).");
  129. }
  130. }