소스 검색

Added delete option

BlackPhreak 5 년 전
부모
커밋
c925ecc170
1개의 변경된 파일31개의 추가작업 그리고 2개의 파일을 삭제
  1. 31 2
      src/me/blackphreak/CommandHandling/Handlers/EditStudentCommand.java

+ 31 - 2
src/me/blackphreak/CommandHandling/Handlers/EditStudentCommand.java

@@ -110,6 +110,13 @@ public class EditStudentCommand extends AbstractCommandHandler {
 				));
 			}
 			
+			// delete option
+			System.out.println(String.format(
+					"  %2s. %-15s",
+					"98",
+					"!! Delete Selected Student(s) !!"
+			));
+			
 			// exit option
 			System.out.println(String.format(
 					"  %2s. %-15s",
@@ -122,6 +129,24 @@ public class EditStudentCommand extends AbstractCommandHandler {
 			if (selected == -1)
 				continue;
 			
+			if (selected == 98)
+			{
+				System.out.println("Are you sure to DELETE selected student(s)? This process is IRREVERSIBLE!");
+				System.out.println("** DEFAULT action is back to main menu **");
+				System.out.println("** type \"confirm\" to delete **");
+				var confirmation = promptQuestion("confirm deletion? > ", false);
+				if (confirmation.equalsIgnoreCase("confirm")) {
+					var updated = Database.getInstance().update("DELETE FROM Student WHERE StudentID IN (\""
+							+ selectedIDs() + "\");");
+					
+					System.out.println("Deleted " + updated + " student(s).");
+					System.out.println("Deleted student(s): " + String.join(", ", selectedStudentID));
+				}
+				
+				lastCmd = "exit";  // back to main menu
+				return false;
+			}
+			
 			if (selected == 99)
 			{
 				lastCmd = "exit";
@@ -143,12 +168,16 @@ public class EditStudentCommand extends AbstractCommandHandler {
 	
 	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+"\");");
+		var updated = Database.getInstance().update("UPDATE Student SET " + selectedColumn
+				+ " = \""+newValue+"\" WHERE StudentID IN (\"" + selectedIDs() + "\");");
 		
 		if (updated <= 0)
 			System.out.println("No changes have been made.");
 		else
 			System.out.println("Update successfully made to "+updated+" student(s).");
 	}
+	
+	private String selectedIDs() {
+		return String.join("\",\"", selectedStudentID);
+	}
 }