| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package me.blackphreak.CommandHandling.Handlers;
- import me.blackphreak.CommandHandling.AbstractCommandHandler;
- import me.blackphreak.Database;
- import me.blackphreak.Debug.Debug;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import static me.blackphreak.Lib.promptQuestion;
- import static me.blackphreak.Lib.tryParseUInt;
- public class EditStudentCommand extends AbstractCommandHandler {
- private String lastCmd;
- private List<String> selectedStudentID;
- private String selectedColumn;
-
- public EditStudentCommand(String desc) {
- super(desc);
- }
-
- @Override
- public boolean handle() {
- boolean nextFlag;
- // reset
- selectedStudentID = new ArrayList<>();
- lastCmd = null;
- selectedColumn = null;
-
- do
- {
- nextFlag = selectStudentByID();
-
- // if lastCmd is set.
- if (lastCmd != null) {
- // in this case, lastCmd should be either "exit" nor "end".
- return lastCmd.equalsIgnoreCase("exit");
- }
- } while (!nextFlag);
-
- do {
- nextFlag = selectColumnToBeModified();
-
- if (lastCmd != null && lastCmd.equalsIgnoreCase("exit")) {
- return true;
- }
- } while (!nextFlag);
-
- doEdit();
-
- return true;
- }
-
- /**
- * Ask user to provide student ID before editing the targeted student(s).
- * @return true when selected at least one student, otherwise, false.
- */
- private boolean selectStudentByID() {
- do {
- System.out.println("\n** type \"next\" to stop providing StudentID **");
- System.out.println("** type \"exit\" to back to main menu **");
- System.out.println("\nSelected StudentID(#"+selectedStudentID.size()+"): " + Arrays.toString(selectedStudentID.toArray()));
- var inp = promptQuestion("Please provide StudentID: ", false);
-
- if (inp.equalsIgnoreCase("next"))
- {
- if (selectedStudentID.size() <= 0)
- System.out.println("Please provide at least one student ID before edit.");
- else
- return true;
- }
- else if (inp.equalsIgnoreCase("exit"))
- {
- lastCmd = "exit"; // back to main menu
- return false;
- }
- else
- {
- // query database to confirm that student is exist.
- try {
- if (Database.getInstance()
- .query("SELECT COUNT(*) FROM Student WHERE StudentID = \""+inp+"\";")
- .getInt(1) != 1)
- {
- System.out.println("Invalid StudentID, please try again. [Failed to select (!=1)]");
- continue;
- }
- } catch (SQLException e) {
- Debug.err("Failed to find student. (Database Error)");
- e.printStackTrace();
- lastCmd = "end"; // end the program
- return false;
- }
-
- selectedStudentID.add(inp);
- }
- } while (true);
- }
-
- private boolean selectColumnToBeModified() {
- do {
- System.out.println("\nColumns:");
- for (int i = 0; i < SearchStudentCommand.unmodColNames.size(); i++) {
- System.out.println(String.format(
- " %2s. %-15s",
- (i+1) + "",
- SearchStudentCommand.unmodColNames.get(i)
- ));
- }
-
- // exit option
- System.out.println(String.format(
- " %2s. %-15s",
- "99",
- "Back to main menu"
- ));
-
- var inp = promptQuestion("Please select one column to edit: ");
- var selected = tryParseUInt(inp, "Invalid column index");
- if (selected == -1)
- continue;
-
- if (selected == 99)
- {
- lastCmd = "exit";
- return false;
- }
-
- if (selected <= 0
- || selected > SearchStudentCommand.unmodColNames.size())
- {
- System.out.println("Invalid column index. [Out of range]");
- continue;
- }
-
- selectedColumn = SearchStudentCommand.unmodColNames.get(selected-1);
- return true;
-
- } while (true);
- }
-
- private void doEdit() {
- var newValue = promptQuestion("The value that you want it to be: ");
- var ids = String.join("\",\"", selectedStudentID);
- var updated = Database.getInstance().update("UPDATE Student SET " + selectedColumn + " = \""+newValue+"\" WHERE StudentID IN (\""+ids+"\");");
-
- if (updated <= 0)
- System.out.println("No changes have been made.");
- else
- System.out.println("Update successfully made to "+updated+" student(s).");
- }
- }
|